2

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

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

4

1 回答 1

0

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

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

WP_Query() 一开始应该是这样的:

$args = array('cat' => get_cat_id($category), 'post_type' => 'track-record');
$category_posts = new WP_Query($args);
于 2013-07-16T14:20:41.757 回答