ACF 帖子对象字段输出中未替换自定义帖子类型分类法

问题描述 投票:0回答:1

我在客户的网站上设置了自定义帖子类型(照片)和分类(位置)。当使用菜单、搜索以及直接从 WordPress 管理面板中的自定义帖子类型访问时,永久链接可以正常工作,但在我的 Timber/Twig 模板中使用高级自定义字段帖子对象字段访问它们时,永久链接无法正常工作。 URL 的分类部分 (

%locations%
) 不会被替换。例如,http://example.com/photos/`%locations%`/taj-mahal-and-the-ganges/
%locations%
应替换为 worldindia,它们是自定义分类中的位置。

使用以下代码将帖子的自定义帖子对象字段拉入模板:

<a href="{{ story.meta( 'associated_photo_gallery' ).link }}" class="view-gallery">{{ __('View photo gallery', textdomain) }}</a>

我在下面添加了我的自定义帖子类型和分类代码:

function textdomain_register_photos_post_type() {
  $args = [
    'label' => 'Photo Galleries',
    'labels' => [ 
        'singular_name' => _x( 'Photo Gallery', 'singular' ),
        'menu_name' => _x( 'Photo Galleries', 'admin menu' ),
        'name_admin_bar' => _x( 'Photo Galleries', 'admin bar' ),
        'add_new' => _x( 'Add New', 'add new' ),
        'add_new_item' => __( 'Add New Photo Gallery' ),
        'new_item' => __( 'New Photo Gallery' ),
        'edit_item' => __( 'Edit Photo Gallery' ),
        'view_item' => __( 'View Photo Gallery' ),
        'all_items' => __( 'All Photo Galleries' ),
        'search_items' => __( 'Search Photo Galleries' ),
        'not_found' => __( 'No photo galleries found.' ),
    ],
    'supports' => array(
        'title',
        'editor',
        'thumbnail'
    ),
    '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', 'textdomain_register_photos_post_type' );

function textdomain_register_locations_taxonomy() {
  $args = [
    'labels' => [
        'name' => _x( 'Locations', 'taxonomy general name' ),
        'singular_name' => _x( 'Location', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Locations' ),
        'all_items' => __( 'All Locations' ),
        'parent_item' => __( 'Parent Location' ),
        'parent_item_colon' => __( 'Parent Location:' ),
        'edit_item' => __( 'Edit Location' ), 
        'update_item' => __( 'Update Location' ),
        'add_new_item' => __( 'Add New Location' ),
        'new_item_name' => __( 'New Location Name' ),
        'menu_name' => __( 'Locations' ),
    ],
    'hierarchical' => true,
    'rewrite' => [
        'slug' => 'locations',
        'hierarchical' => true,
    ],
];
register_taxonomy( 'locations', [ 'photos' ], $args );
};
add_action( 'init', 'textdomain_register_locations_taxonomy' );

add_filter( 'post_type_link', 'textdomain_post_type_link', 10, 2 );
function textdomain_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;
}

我已多次保存我的永久链接,并且在主题函数文件的底部有

flush_rewrite_rules();

更新

WordPress 在functions.php 文件的第422 行显示此警告

Invalid argument supplied foreach()
。代码如下:

$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 ) );
  }
}

我不确定这是否会导致问题,但我的 PHP 知识有限。

非常感谢有关此问题的任何提示或建议。

php wordpress twig permalinks timber
1个回答
0
投票

我发现这个问题与如何检索分类法的帖子 ID 有关。我查看了 get_the_terms 函数参考,发现需要在以下行中将

get_the_ID()
替换为
$post->ID
$terms = get_the_terms( $post->ID, $taxonomy );
。我的更新和工作功能如下。

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

  $taxonomy = 'locations';
  // Replaced get_the_ID() with $post->ID
  $terms = get_the_terms( $post->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;
}
© www.soinside.com 2019 - 2024. All rights reserved.