当前位置: 首页 > 知识库问答 >
问题:

Wordpress自定义帖子类型永久链接在高级自定义字段、帖子对象字段中不起作用

山翼
2023-03-14

我在使用WordPress、高级自定义字段post object field和Timber渲染自定义永久链接时遇到了以下问题。我有帖子和自定义帖子类型的照片库,它们是相关的,并通过使用附加到故事帖子的帖子对象字段的链接设置来连接。呈现的链接显示如下:在本例中,http://example.com/photos/%locations%/bordeaux/.的< code>%locations%段应替换为< code>world/france。

使用post的永久链接进行访问时,永久链接将正确呈现(http://example.com/photos/world/france/bordeaux),附加到WordPress菜单或使用核心搜索功能导航到。

post对象设置了以下参数:

    < li >按帖子类型筛选:照片库(自定义帖子类型) < li >返回格式:发布对象

我在下面包含了我的自定义帖子类型、分类和< code>post_type_link函数。

自定义帖子类型(缩写)

function gerryfeehan_register_photos_post_type() {
  $args = [
    'label' => 'Photo Galleries',
    'labels' => [],
    'supports' => array(),
    'public' => true,
    'menu_position' => 5,
    'menu_icon' => 'dashicons-format-gallery',
    'capability_type' => 'post',
    'taxonomies' => [ 'locations', ],
    'has_archive' => true,
    'delete_with_user' => false,
    'rewrite' => [
        'slug' => 'photos/%locations%',
        'with_front' => false,
    ],
  ];
  register_post_type( 'photos', $args );
}
add_action( 'init', 'gerryfeehan_register_photos_post_type' );

自定义分类法(缩写)

function gerryfeehan_register_locations_taxonomy() {
  $args = [
    'labels' => [],
    'hierarchical' => true,
    'rewrite' => [
        'slug' => 'locations',
        'hierarchical' => true,
    ],
  ];
  register_taxonomy( 'locations', [ 'photos' ], $args );
}
add_action( 'init', 'gerryfeehan_register_locations_taxonomy' );

帖子类型链接过滤功能

add_filter( 'post_type_link', 'gerryfeehan_post_type_link', 10, 2 );
function gerryfeehan_post_type_link( $post_link ) {
  $taxonomy = 'locations';
  $terms = get_the_terms( get_the_ID(), $taxonomy );
  $slug = [];
  // Warning: Invalid argument supplied for foreach()
  foreach ( $terms as $term ) {
    if ( $term->parent == 0 ) {
      array_unshift( $slug, sanitize_title_with_dashes( $term->name ) );
    } else {
      array_push( $slug, sanitize_title_with_dashes( $term->name ) );
    }
  }
  if ( ! empty( $slug ) ) {
    return str_replace( '%' . $taxonomy . '%' , join( '/', $slug ) , $post_link );
  }
  return $post_link;  
}

木材get_field功能

{{ story.get_field( 'associated_photo_gallery' ).link }}

任何建议,为什么永久链接不能正确渲染时,使用先进的自定义领域和木材。

共有1个答案

文寒
2023-03-14

据我所见,您在< code > register _ post _ type()中的自定义永久链接结构的配置看起来不错。

您收到“为Foreach()提供的无效参数”警告的原因是因为post_type_link过滤器为每个帖子类型运行。但是位置分类法可能没有为您网站的每个帖子类型注册,如果没有术语,get_the_terms()函数返回false

要解决这个问题,您应该添加一个紧急救援,当它不是< code >照片帖子类型时,可以提前返回。为此,您可以使用< code>$post参数,它作为过滤器中的第二个参数提供。

add_filter( 'post_type_link', 'gerryfeehan_post_type_link', 10, 2 );

function gerryfeehan_post_type_link( $post_link, $post ) {
    // Bail out if not photos post type.
    if ( 'photos' !== $post->post_type ) {
        return $post_link;
    }

    $taxonomy = 'locations';
    $terms    = get_the_terms( get_the_ID(), $taxonomy );
    $slug     = [];

    foreach ( $terms as $term ) {
        if ( $term->parent == 0 ) {
            array_unshift( $slug, sanitize_title_with_dashes( $term->name ) );
        } else {
            array_push( $slug, sanitize_title_with_dashes( $term->name ) );
        }
    }

    if ( ! empty( $slug ) ) {
        $post_link = str_replace( '%' . $taxonomy . '%', join( '/', $slug ), $post_link );
    }

    return $post_link;
}

此外,你需要确保通过访问WordPress admin中的设置→永久链接页面来刷新你的永久链接。

 类似资料:
  • 我正在使用高级自定义字段插件,我试图通过分类字段过滤一些自定义帖子,修改WP_Query: 如果我尝试通过文本字段过滤一切正常,WP_Query被修改。但是当字段是一个分类法字段时,我不知道应该传递什么参数,因为它是一个对象。我尝试了分类法名称和分类法ID,但不起作用。 是否可以通过分类字段进行筛选?我应该传递的什么参数?谢谢! 更新-结构: 自定义帖子:“递归操作系统” 自定义分类Slug:'r

  • 我可以使用自定义字段值获取数据吗?我的是,字段名是。那么,如何使用获取所有数据呢? 代码如下: 此代码不起作用。

  • 我是新来的wordpress, im创建一个自定义职位与字段的custom_meta_box(位置,着装) 所以在我的自定义帖子列表中,我想查看我在custom_meta_box上创造的价值。 这是我目前的代码:

  • 我已经在WordPress的编辑帖子部分创建了一个自定义字段,并且能够通过以下帖子保存该值:Wordpress-将自定义字段添加到帖子屏幕。 我可以检索特定类别的帖子,但无法检索自定义字段的值。 如上图所示,在左上角高亮显示的位置有一个自定义的post字段。另一个突出显示的字段是显示帖子属于“公文包”类别。 下面是我用来检索“Portfolio”类别帖子的代码 我可以得到文章标题和内容的值,但不能

  • 本文向大家介绍WordPress 注册自定义帖子类型,包括了WordPress 注册自定义帖子类型的使用技巧和注意事项,需要的朋友参考一下 示例 假设您有一个图书馆网站,并且想要一个名为Books的自定义帖子类型。可以注册为 如此简单,您现在就注册了一个自定义帖子类型。 该代码段可以放在主题functions.php文件中,也可以放在插件结构中。

  • 本文向大家介绍WordPress 3.0中的自定义帖子类型,包括了WordPress 3.0中的自定义帖子类型的使用技巧和注意事项,需要的朋友参考一下 本文与WordPress 3.0相关。此处发布的许多代码在以前的版本中将不起作用,并且某些信息可能会在较新的版本中更改。 WordPress已经在系统中内置了五种不同的内容类型。 帖子 这是标准的内容类型,通常是博客安装中使用最多的内容。帖子往往会