从wordpress中的帖子使用wordpress查询获取类别列表

时间:2015-06-27 10:54:28

标签: php wordpress

我想获取类别列表,但我的查询会返回所有帖子

 <?php 
    global $post;
    $args = array(
      'post_type' => 'post',
      'posts_per_page' => 24,
      'order' => 'ASC',
      'orderby' => 'title'
    );

    $the_query = new WP_Query( $args );

    while ( $the_query->have_posts() ) { $the_query->the_post();
        if(get_the_id() == $thisId){$class = "active";}else{$class="";}
    ?>

    <li class="<?php echo $class; ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php }

    wp_reset_postdata();

    ?>

给我输出中的所有帖子,我想显示所有类别

2 个答案:

答案 0 :(得分:0)

如果有人要求,我得到了答案

<ul>
<?php 
            global $post;
            $args = array(
                'show_option_all'    => '',
                'orderby'            => 'name',
                'order'              => 'ASC',
                'show_count'         => 0,
                'hide_empty'         => 1,
                'use_desc_for_title' => 1,
                'child_of'           => 0,
                'feed'               => '',
                'title_li'           => '',
                'feed_type'          => '',
                'feed_image'         => '',
                'exclude'            => '',
                'exclude_tree'       => '',
                'include'            => '',
                'hierarchical'       => 1,
                'echo'               => 1,
                'taxonomy'           => 'category'
            );
            $the_query = wp_list_categories( $args ); 

            ?>
</ul>

答案 1 :(得分:0)

我找到了显示类别的简单方法。

wordpress具有内置功能wp_list_catergories()

<?php 
    $args = array(
    'style'      => 'list',
    'hide_empty' => 1,
    );
    wp_list_categories($args); 
?>

这将自动显示具有链接的类别,无需编写循环和全部内容。

如果您想拥有所有类别的道具,请使用get_categories()

 <?php 
    $args = array(
    'style'      => 'list',
    'hide_empty' => 1,
    );
    get_categories($args); 
?>

ref:https://phppot.com/wordpress/how-to-get-wordpress-categories/

相关问题