[英]WordPress Custom Post Type Category Not Listing
我創建了一個自定義帖子類型,稱為跟蹤記錄(想想投資組合)。 我的代碼如下:
function track_record_register() {
$labels = array(
'name' => _x('Track Record', 'post type general name'),
'singular_name' => _x('Track Record Item', 'post type singular name'),
'add_new' => _x('Add New', 'track record item'),
'add_new_item' => __('Add New Track Record Item'),
'edit_item' => __('Edit Track Record Item'),
'new_item' => __('New Track Record Item'),
'view_item' => __('View Track Record Item'),
'search_items' => __('Search Track Record'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'has_archive' => true,
'taxonomies' => array('category'),
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'track-record' , $args );
}
這可以正常工作,我可以創建一個跟蹤記錄項並將其分配給一個類別。 但是,當我使用WP_Query列出類別中的所有帖子時,分配給類別“跟蹤記錄”的自定義跟蹤記錄帖子類型不會顯示。 如果我以常規方式創建帖子並將其分配給“跟蹤記錄”類別(通過轉到“帖子”->“添加新記錄”而不是“跟蹤記錄”->“添加新記錄”),則效果很好。
有什么想法嗎? :/
我的QP_Query()代碼:
$args = array('cat' => get_cat_id($category));
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<li>
<a href="<?php echo the_permalink();?>"><i class="icon-circle-arrow-right"></i></a>
<a href="<?php echo the_permalink(); ?>"><?php echo the_title(); ?></a>
</li>
<?php
endwhile;
?>
在上面的QP_Query()代碼中,變量$category
是$category
的名稱,WordPress然后將其轉換為類別的ID。 該代碼是窗口小部件的一部分,因為某人記住類別的名稱比ID容易得多。
我只是在幾分鍾后在Stack Overflow上發布問題后才弄清楚這一點。
對於可能在同一頁面上post_type
相同問題的任何人,您都必須在$args
為WP_Query()設置post_type
。
WP_Query()開始時應如下所示:
$args = array('cat' => get_cat_id($category), 'post_type' => 'track-record');
$category_posts = new WP_Query($args);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.