Wordpress自定义分类存档页面无法正常工作

时间:2014-07-27 20:12:19

标签: php wordpress

我将自定义分类标识分配给自定义帖子类型。我创建了一个名为“taxonomy-brands.php”的模板,当我访问自定义分类存档时会调用该模板,但是当我在此模板中调用循环时,它不认为我有任何帖子(它跳过if(have_posts())并执行else {})。我的自定义帖子类型有'has_archive'=>是的,当我进入邮件类型的存档页面时,我看到了存档,它只是分类存档不起作用。

代码中是否有可能导致此问题的内容? 在此先感谢您的帮助。

以下是我用来注册CPT的代码。

add_action( 'init', 'register_custom_post_type_apknews' );
function register_custom_post_type_apknews() {

$phone_labels = array(
'name' => _x( 'specification', 'specification' ),
'singular_name' => _x( 'specification', 'specification' ),
'add_new' => _x( 'Add New', 'specification' ),
'add_new_item' => _x( 'Add New specification', 'specification' ),
'edit_item' => _x( 'Edit specification', 'specification' ),
'new_item' => _x( 'New specification', 'specification' ),
'view_item' => _x( 'View specification', 'specification' ),
'search_items' => _x( 'Search specification', 'specification' ),
'not_found' => _x( 'No specification found', 'specification' ),
'not_found_in_trash' => _x( 'No specification found in Trash', 'specification' ),
'parent_item_colon' => _x( 'Parent specification:', 'specification' ),
'menu_name' => _x( 'specifications', 'specification' ),
);
$phone_args = array(
'labels' => $phone_labels,
'hierarchical' => false,
'description' => 'All mobile phone specifications.', // description for custom post type
'supports' => array( 'title','thumbnail'), // more support : 'excerpt','author','trackbacks','custom-fields','comments','revisions','page-attributes','post-formats'
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => get_stylesheet_directory_uri(). '/images/specification.png',
'show_in_nav_menus' => false,
'publicly_queryable' => true,
'exclude_from_search' => true,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true, //array( 'slug' => 'specif' ),
'capability_type' => 'post'
);

register_post_type( 'specification', $phone_args );
}

以下是我用来注册自定义分类的代码。

add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 );

function create_topics_hierarchical_taxonomy() {    

$brands_labels = array(
'name' => _x( 'Brands', 'Brands' ),
'singular_name' => _x( 'Brand', 'Brand' ),
'search_items' =>  __( 'Search Brands' ),
'all_items' => __( 'All Brands' ),
'parent_item' => __( 'Parent Brand' ),
'parent_item_colon' => __( 'Parent Brand:' ),
'edit_item' => __( 'Edit Brand' ), 
'update_item' => __( 'Update Brand' ),
'add_new_item' => __( 'Add New Brand' ),
'new_item_name' => __( 'New Brand Name' ),
'menu_name' => __( 'Brands' ),
);

register_taxonomy('brands',array('specification'), array(
'hierarchical' => true,
'labels' => $brands_labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'brands' ),
));

}

最后这里是' taxonomy-brands.php '中的代码。

    <?php
    $args = array(
        'post_type' => 'specification',
        'tax_query' => array(
            array(
                'taxonomy' => 'brands',
            )
        )
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
      <!--My stuff-->
        <div class="fix single_content floatleft">
            <a href="<?php the_permalink();?>"><?php the_post_thumbnail('specification-home-feature-img', array('alt' => get_the_title())); ?></a>
            <h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
            <p>Tk: 8900 BDT</p>
        </div>
       <!--My stuff-->
      <?php endwhile; ?>
      <?php wp_reset_postdata(); ?>
    <?php else:  ?>
      <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

1 个答案:

答案 0 :(得分:0)

我认为问题出在你的查询中,你需要添加术语才能工作。

$args = array(
'post_type' => 'post',
'tax_query' => array(
    array(
        'taxonomy' => 'brands',
        'field' => 'slug',
        'terms' => array('brand1','brand2'....)
    )
)
);