簡體   English   中英

自定義帖子類型分類法在 ACF 帖子 object 字段 output 中未被替換

[英]Custom post type taxonomy not being replaced in ACF post object field output

我在客戶的網站上設置了自定義帖子類型(照片)和分類法(位置)。 當使用菜單、搜索和直接從 WordPress 管理面板中的自定義帖子類型訪問時,永久鏈接正常工作,但當使用我的 Timber/Twig 模板中的高級自定義字段帖子 object 字段訪問它們時,永久鏈接無法正常工作。 URL 的分類部分 ( %locations% ) 未被替換。 例如, http://example.com/photos/`%locations%`/taj-mahal-and-the-ganges/ %locations%應替換為worldindia ,它們是自定義分類法中的位置。

使用以下代碼將帖子的自定義帖子 object 字段拉入模板: <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 知識有限。

非常感謝有關此問題的任何提示或建議。

我發現問題與如何為分類法檢索帖子 ID 有關。 我查看了get_the_terms function 參考資料,發現get_the_ID()需要替換為以下行中的$post->ID$terms = get_the_terms( $post->ID, $taxonomy ); . 我的更新和工作 function 如下。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM