首页 > 解决方案 > Wordpress 自定义帖子类型永久链接在高级自定义字段帖子对象字段中不起作用

问题描述

我在使用 WordPress、高级自定义字段发布对象字段和木材呈现自定义永久链接时遇到以下问题。我有帖子和自定义帖子类型的照片画廊,它们是相关的,并通过链接设置使用附加到故事帖子的帖子对象字段进行连接。呈现的链接显示如下:http ://example.com/photos/%locations%/bordeaux/ 。在此示例中,该%locations%段应替换为。world/france

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

post 对象具有以下参数集:

我在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 }}

关于高级自定义字段和木材使用时永久链接为何无法正确呈现的任何建议。

标签: phpwordpressadvanced-custom-fieldspermalinkstimber

解决方案


据我所见,您的自定义永久链接结构的配置register_post_type()看起来不错。

您收到“为 foreach() 提供的参数无效”警告的原因是post_type_link过滤器针对每种帖子类型运行。但是locations分类法可能并未针对您网站的每种帖子类型进行注册,并且如果没有条款,该get_the_terms()函数将返回。false

要解决此问题,您应该添加一个在不是photos帖子类型时提前返回的救助。为此,您可以使用$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 管理员中的“设置” → “永久链接”页面来刷新永久链接。


推荐阅读