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

发布于 2025-01-11 23:25:01 字数 4257 浏览 0 评论 0原文

我在客户的网站上设置了自定义帖子类型(照片)和分类(位置)。当使用菜单、搜索以及直接从 WordPress 管理面板中的自定义帖子类型访问时,永久链接可以正常工作,但在我的 Timber/Twig 模板中使用高级自定义字段帖子对象字段访问它们时,永久链接无法正常工作。 URL 的分类部分 (%locations%) 不会被替换。例如,http://example。 com/photos/`%locations%`/taj-mahal-and-the-ganges/%locations% 应替换为 worldindia,它们是自定义分类中的位置。

使用以下代码将帖子的自定义帖子对象字段拉入模板: {{ __('查看照片库', textdomain) }}.

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

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 行显示此警告为each() 提供的参数无效。代码如下:

$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 知识有限。

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

I have a custom post type (Photos) and a taxonomy (Locations) setup on my client's website. The permalinks work correctly when accessed using the menus, search, and directly from the custom post type in the WordPress admin panel, but not when accessing them using an Advanced Custom Fields post object field in my Timber/Twig template. The taxonomy portion (%locations%) of the URL is not being replaced. For example, http://example.com/photos/`%locations%`/taj-mahal-and-the-ganges/. The %locations% should be replaced with world and india, which are locations from the custom taxonomy.

The post's custom post object field is being pulled into the template using the following code: <a href="{{ story.meta( 'associated_photo_gallery' ).link }}" class="view-gallery">{{ __('View photo gallery', textdomain) }}</a>.

I have included my custom post type and taxonomy code below:

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

I have saved my permalinks multiple times and have flush_rewrite_rules(); at the bottom of my theme's functions file.

Update

WordPress is displaying this warning Invalid argument supplied foreach() on line 422 of the functions.php file. The code is as follows:

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

I am not sure if this could be causing the issue, but my PHP knowledge is limited.

Any tips or suggestions on this issue are greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

π浅易 2025-01-18 23:25:01

我发现这个问题与如何检索分类法的帖子 ID 有关。我查看了 get_the_terms 函数参考,发现 get_the_ID() 需要替换为以下行中的 $post->ID$terms = get_the_terms( $post->ID, $分类法);。我的更新和工作功能如下。

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

I figured out the issue was related to how the post ID was being retrieved for the taxonomies. I reviewed the get_the_terms function reference and found that get_the_ID() needed to be replaced by $post->ID in the following line: $terms = get_the_terms( $post->ID, $taxonomy );. My updated and working function is below.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文