分类法模板未使用have_posts()

时间:2017-02-17 10:24:28

标签: wordpress wordpress-theming

我使用自定义分类法来定制帖子类型。如果我点击使用texanomy创建的类别。它导航到分类法模板但是没有显示所选类别中的帖子。任何人都可以帮助我。 将url显示为url // portfolio_category / cricket /

function.php

add_action( 'init', 'create_team_post_type' );
function create_team_post_type() {
  register_post_type( 'portfolio',
    array(
      'labels' => array(
        'name' => __( 'Portfolio' ),
        'singular_name' => __( 'Portfolio' )
      ),
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'taxonomies' => array('portfolio_category'),
        'supports' => array('title','editor','thumbnail')
    )
  );
}
function taxonomies_portfolio() {
    $labels = array(
        'name'              => _x( 'Portfolio categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Portfolio categories', 'taxonomy singular name' ),
        'search_items'      => __( 'Query portfolio categories' ),
        'all_items'         => __( 'All portfolio categories' ),
        'parent_item'       => __( 'Parent category' ),
        'parent_item_colon' => __( 'Parent category:' ),
        'edit_item'         => __( 'Edit portfolio category' ), 
        'update_item'       => __( 'Update portfolio category' ),
        'add_new_item'      => __( 'Add Edit portfolio category' ),
        'new_item_name'     => __( 'New portfolio category' ),
        'menu_name'         => __( 'Categories' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'rewrite' => true
    );
    register_taxonomy( 'portfolio_category', 'portfolio', $args );
}
add_action( 'init', 'taxonomies_portfolio', 0 );

的index.php

    <?php 
     $defaults = array(
            'hide_empty'          => false, 
            'taxonomy'            => 'portfolio_category',
            'title_li'            => __( 'Categories' )
        );
    wp_list_categories($defaults); ?>

分类法portfolio_category.php

<?php 
if(have_posts()):while(have_posts()):the_post();
    the_title();
endwhile;
endif;
?>

我需要解决方案。如何使用分类法按类别过滤。

1 个答案:

答案 0 :(得分:0)

为什么要列出index.php中的类别。我没有进入那个。

在taxonomy-portfolio_category.php中,

<?php 
 $taxonomies = get_taxonomies(); 
    foreach ( $taxonomies as $taxonomy ) {
        if($taxonomy = 'portfolio_category'){
            $terms['trm'] = get_terms($taxonomy, array('hide_empty'=> false, 
                                                       'taxonomy'=> 'portfolio_category',
                                                        'title_li'=> __( 'Categories' ));
        }
    }
    foreach ($terms['trm'] as $term){
            $query = new WP_Query(
                                array(
                                    'post_type' => 'portfolio',
                                    'tax_query' => array(
                                                        array(
                                                            'taxonomy' => 'taxonomy',
                                                            'field'    => 'name',
                                                            'terms'    => $term->name,
                                                        ),
                                                    ),
                                    )
                                );
        if($query->have_posts()):while($query->have_posts()):$query->the_post(); ?>
             <h1><?php the_title(); ?></h1> <!-- place html code here -->
   <?php

        endwhile;
        endif;
        wp_reset_postdata(); 
   }

   ?>

希望这对你有用。感谢