WordPress自定义帖子类型类别未列出

时间:2013-07-16 14:15:28

标签: php wordpress custom-post-type

我创建了一个名为track-record(think portfolio)的自定义帖子类型。我的代码如下:

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是WordPress随后转换为类别ID的类别的名称。这是代码是小部件的一部分,因为人们更容易记住类别的名称而不是它的ID。

1 个答案:

答案 0 :(得分:0)

我在Stack Overflow上发布这个问题后,我想出了几分钟。

对于可能在此页面中遇到同样问题的任何人,您必须在post_type中为WP_Query()设置$args

WP_Query()在开始时应如下所示:

$args = array('cat' => get_cat_id($category), 'post_type' => 'track-record');
$category_posts = new WP_Query($args);
相关问题