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

问题描述

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

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

帖子对象设置了以下参数:

  • 按帖子类型过滤:照片库(自定义帖子类型)
  • 返回格式:发布对象

我在下面包含了我的自定义帖子类型、分类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',];
  register_taxonomy( 'locations',[ 'photos' ],'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;  
}

Timber get_field 函数

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

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

解决方法

据我所知,您在 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 管理员中的设置固定链接页面来刷新固定链接。 >