【问题标题】:WordPress Custom Post Type Category Not ListingWordPress 自定义帖子类型类别未列出
【发布时间】:2013-07-14 18:40:52
【问题描述】:

我创建了一个名为跟踪记录(想想投资组合)的自定义帖子类型。我的代码如下:

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 列出某个类别中的所有帖子时,分配给类别 Track Record 的自定义跟踪记录帖子类型不会显示。如果我创建一个帖子并以正常方式将其分配给跟踪记录类别(通过帖子 -> 添加新而不是跟踪记录 -> 添加新),它工作正常。

想法? :/

我的 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 要容易得多。

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    我只是在 Stack Overflow 上发布问题后几分钟才想到这一点。

    对于可能偶然发现此页面并遇到相同问题的任何人,您必须在 WP_Query() 的 $args 中设置 post_type

    WP_Query() 开头应该是这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 2014-07-10
      • 2015-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多