自定义分类不显示在自定义POST循环中

时间:2021-11-08 作者:Triphys

所以我想我可能要发疯了。我有一个带有两个自定义分类法(流派和作者)的自定义帖子类型(arkiv),我尝试在一个循环中的模板页面上列出帖子,并显示帖子标题和两个分类法,但我无法让它工作。列出了文章标题,但没有列出分类法。

我的CPT如下所示:

function post_archive() {
  $labels = array(
    \'name\'                => _x( \'Arkiv & Samlingar\', \'twentytwenty\' ),
    \'singular_name\'       => _x( \'Arkiv & Samlingar\', \'twentytwenty\' ),
    \'menu_name\'           => __( \'Arkiv & Samlingar\', \'twentytwenty\' ),
    \'all_items\'           => __( \'Alla Arkiv & Samlingar\', \'twentytwenty\' ),
    \'view_item\'           => __( \'Visa Arkiv & Samlingar\', \'twentytwenty\' ),
    \'add_new_item\'        => __( \'Lägg till Arkiv & Samlingar\', \'twentytwenty\' ),
    \'add_new\'             => __( \'Lägg till Arkiv & Samlingar\', \'twentytwenty\' ),
    \'edit_item\'           => __( \'Ändra Arkiv & Samlingar\', \'twentytwenty\' ),
    \'update_item\'         => __( \'Uppdatera Arkiv & Samlingar\', \'twentytwenty\' ),
    \'search_items\'        => __( \'Sök Arkiv & Samlingar\', \'twentytwenty\' ),
    \'not_found\'           => __( \'Fann inget\', \'twentytwenty\' ),
    \'not_found_in_trash\'  => __( \'Fann inget i Papperskorgen\', \'twentytwenty\' ),
  ); 
  $args = array(
    \'label\'               => __( \'arkiv\', \'twentytwenty\' ),
    \'labels\'              => $labels,
    \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', ),
    \'hierarchical\'        => false,
    \'public\'              => true,
    \'show_ui\'             => true,
    \'show_in_menu\'        => true,
    \'show_in_nav_menus\'   => true,
    \'show_in_admin_bar\'   => true,
    \'menu_icon\'           => \'dashicons-book\',
    \'menu_position\'       => 5,
    \'can_export\'          => true,
    \'has_archive\'         => false,
    \'exclude_from_search\' => false,
    \'publicly_queryable\'  => true,
    \'capability_type\'     => \'post\',
    \'show_in_rest\'        => true,
    \'taxonomies\'          => array(\'genre\', \'author\'),
  );   
  register_post_type( \'arkiv\', $args );
}
add_action( \'init\', \'post_archive\', 0 );
我的两种分类法如下所示:

add_action( \'init\', \'genre\', 0 );
function genre() {
  $labels = array(
    \'name\' => _x( \'Subjects\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Subject\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search Subjects\' ),
    \'all_items\' => __( \'All Subjects\' ),
    \'parent_item\' => __( \'Parent Subject\' ),
    \'parent_item_colon\' => __( \'Parent Subject:\' ),
    \'edit_item\' => __( \'Edit Subject\' ), 
    \'update_item\' => __( \'Update Subject\' ),
    \'add_new_item\' => __( \'Add New Subject\' ),
    \'new_item_name\' => __( \'New Subject Name\' ),
    \'menu_name\' => __( \'Subjects\' ),
  );    
  register_taxonomy(\'genre\',array(\'arkiv\'), array(
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'show_in_rest\' => true,
    \'show_admin_column\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'genre\' ),
  ));
}
add_action( \'init\', \'author\', 0 );
function author() {
  $labels = array(
    \'name\' => _x( \'Subjects\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Subject\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search Subjects\' ),
    \'all_items\' => __( \'All Subjects\' ),
    \'parent_item\' => __( \'Parent Subject\' ),
    \'parent_item_colon\' => __( \'Parent Subject:\' ),
    \'edit_item\' => __( \'Edit Subject\' ), 
    \'update_item\' => __( \'Update Subject\' ),
    \'add_new_item\' => __( \'Add New Subject\' ),
    \'new_item_name\' => __( \'New Subject Name\' ),
    \'menu_name\' => __( \'Subjects\' ),
  );    
  register_taxonomy(\'author\',array(\'arkiv\'), array(
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'show_in_rest\' => true,
    \'show_admin_column\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'author\' ),
  ));
}
我的CPT出现了,分类法也在那里,我将分类法分配给那里的帖子,但是当我试图列出帖子时,我只得到了帖子标题,没有任何分类法

我的查询:

<?php $args = array( 
  \'post_type\' => \'arkiv\', 
  \'post_status\' => \'publish\',
  \'posts_per_page\' => -1, 
  \'orderby\' => \'date\', 
  \'order\' => \'DESC\'
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    <h2><?php the_title(); ?></h2>
    <?php the_terms( $post->ID, \'author\', \'Author: \', \', \', \' \' ); ?>
    <?php the_terms( $post->ID, \'genre\', \'Genre: \', \', \', \' \' ); ?>
    
  <?php endwhile;
  wp_reset_postdata(); ?>
<?php endif; ?>
无法理解我做错了什么,提前谢谢。

1 个回复
最合适的回答,由SO网友:Petar Petrov 整理而成

你还没有暴露出$post 调用后的变量$the_query->the_post().

要么添加global $post 电话刚打完$the_query->the_post(), 或更换$post->ID 具有get_the_ID().

相关推荐