WordPress自定义文章循环无法获取分类 求技术排查
WordPress自定义文章类型无法获取分类问题排查
问题说明
在实现WordPress自定义文章类型knowledge_capsules的循环展示时,无法获取文章对应的分类信息,相关代码如下:
循环代码
<?php $ids = array(); $pages = get_pages("child_of=".$post->ID); if ($pages) { foreach ($pages as $page) { $ids[] = $page->ID; } } $paged = (get_query_var("paged")) ? get_query_var("paged") : 1; $args = array( "paged" => $paged, "post__in" => $ids, "posts_per_page" => 50000, "post_type" => "knowledge_capsules", "orderby" => "date", "order" => "DESC" ); query_posts($args); if (have_posts()) : while (have_posts()) : the_post(); ?> <div class="kcIndex_feedItemWrapper" id="postID<?php echo get_the_ID(); ?>"> <!-- Need to add in the lightbox content here and fire it open on click --> <a href="#" data-featherlight="#IssueExpandedContent_<?php echo get_the_ID(); ?>"> <img src="<?php the_field('knowledgecapsule_teaser_thumbimg'); ?>" title="<?php the_title(); ?>" alt="<?php the_title(); ?>"> <h4><?php the_title(); ?></h4> <div class="categoriesHere"> <?php $terms = get_the_terms( get_the_ID(), 'knowledge_capsules' ); if(!empty($terms)){ foreach($terms as $term){ echo $term->name.'<br>'; } } ?> </div> </a> <!-- Expanded content --> <div id="IssueExpandedContent_<?php echo get_the_ID(); ?>"> <?php the_field('knowledgecapsule_teaser_embeddcode'); ?> <p class="viewFullVideoLink"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">View the full video</a></p> </div> </div> </div> <?php endwhile; else: ?> <!--<h2>Oh No!!</h2> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>--> <?php endif; ?>
自定义文章类型注册代码
<?php function prefix_create_custom_post_type() { /* * The $labels describes how the post type appears. */ $labels = array( 'name' => 'Knowledge Capsules', // Plural name 'singular_name' => 'Knowledge Capsule' // Singular name ); /* * The $supports parameter describes what the post type supports */ $supports = array( 'title', // Post title 'editor', // Post content 'excerpt', // Allows short description 'author', // Allows showing and choosing author 'thumbnail', // Allows feature images 'comments', // Enables comments 'trackbacks', // Supports trackbacks 'revisions', // Shows autosaved version of the posts 'custom-fields' // Supports by custom fields ); /* * The $args parameter holds important parameters for the custom post type */ $args = array( 'labels' => $labels, 'description' => 'Post type post knowledge capsule', // Description 'supports' => $supports, 'taxonomies' => array( 'category', 'post_tag' ), // Allowed taxonomies 'hierarchical' => false, // Allows hierarchical categorization, if set to false, the Custom Post Type will behave like Post, else it will behave like Page 'public' => true, // Makes the post type public 'show_ui' => true, // Displays an interface for this post type 'show_in_menu' => true, // Displays in the Admin Menu (the left panel) 'show_in_nav_menus' => true, // Displays in Appearance -> Menus 'show_in_admin_bar' => true, // Displays in the black admin bar 'menu_position' => 5, // The position number in the left menu 'menu_icon' => '/wp-content/themes/genesis-nal-new/includes/structure/KC_icon.svg', // The URL for the icon used for this post type 'can_export' => true, // Allows content export using Tools -> Export 'has_archive' => true, // Enables post type archive (by month, date, or year) 'exclude_from_search' => false, // Excludes posts of this type in the front-end search result page if set to true, include them if set to false 'publicly_queryable' => true, // Allows queries to be performed on the front-end part if set to true 'capability_type' => 'post' // Allows read, edit, delete like “Post” ); register_post_type('knowledge_capsules', $args); //Create a post type with the slug is ‘knowledge_capsules’ and arguments in $args. } add_action('init', 'prefix_create_custom_post_type'); ?>
问题原因
分类法参数错误:循环中调用
get_the_terms(get_the_ID(), 'knowledge_capsules'),第二个参数传的是自定义文章类型的slug,但注册文章类型时关联的是默认的category(分类)和post_tag(标签)分类法,此处应该传入category而非文章类型slug。查询逻辑错误:
get_pages()函数默认只获取page类型的页面,而非knowledge_capsules自定义文章类型,因此$ids数组中存储的都是页面ID,用这些ID去查询knowledge_capsules类型的文章,会导致查询不到正确内容,自然无法获取分类。不规范的查询方式:使用
query_posts()会干扰WordPress主查询,容易引发后续查询冲突,建议改用WP_Query。
修复方案
1. 修正分类获取代码
将循环中的分类获取部分修改为:
<?php $terms = get_the_terms( get_the_ID(), 'category' ); if(!empty($terms) && !is_wp_error($terms)){ foreach($terms as $term){ echo $term->name.'<br>'; } } ?>
- 把第二个参数改为
category,匹配注册时关联的分类法。 - 增加
!is_wp_error($terms)判断,避免分类查询出错时抛出错误。
2. 修正文章查询逻辑
如果是要展示所有knowledge_capsules文章,直接移除post__in参数:
$args = array( "paged" => $paged, "posts_per_page" => 50000, "post_type" => "knowledge_capsules", "orderby" => "date", "order" => "DESC" );
如果是要关联某个页面的knowledge_capsules文章,建议通过自定义字段实现:
- 给
knowledge_capsules文章添加自定义字段(比如related_page_id),存储关联的页面ID。 - 修改查询参数为:
$args = array( "paged" => $paged, "posts_per_page" => 50000, "post_type" => "knowledge_capsules", "orderby" => "date", "order" => "DESC", "meta_query" => array( array( 'key' => 'related_page_id', 'value' => $post->ID, 'compare' => '=' ) ) );
3. 替换query_posts为WP_Query
<?php $paged = (get_query_var("paged")) ? get_query_var("paged") : 1; $args = array( "paged" => $paged, "posts_per_page" => 50000, "post_type" => "knowledge_capsules", "orderby" => "date", "order" => "DESC" ); $kc_query = new WP_Query($args); if ($kc_query->have_posts()) : while ($kc_query->have_posts()) : $kc_query->the_post(); ?> <!-- 内容部分不变 --> <?php endwhile; wp_reset_postdata(); else: ?> <!-- 无内容提示 --> <?php endif; ?>
- 使用
WP_Query创建独立查询,避免干扰主查询。 - 循环结束后调用
wp_reset_postdata()恢复主查询的上下文。
内容的提问来源于stack exchange,提问作者dubbs
相关产品推荐
相关产品推荐

