Wordpress - 如何按标题按字母顺序排序每个类别中的帖子,而不显示所有类别的所有帖子

时间:2014-02-12 16:21:58

标签: php wordpress title categories

我很难在每个wordpress类别中按标题显示帖子ASC。我找到的各种代码将按字母顺序列出但返回网站上的所有帖子,而不仅仅是用户正在查看的类别。我不想创建多个category.php文件或硬编码哪个类别,因为每次将类别添加到网站时我都必须手动设置新文件。我只是希望代码只查看当前类别,但我没有知识来编写它。

我正在使用带有loop.php文件的标准category.php文件。我删除了所有试图重新订购帖子的代码,但我尝试的其中一个例子来自这个网站:

http://wordpress.org/support/topic/display-posts-in-ascending-order

我的category.php是这样的:

<?php get_header(); ?>
     <div class="breadcrumbs">
   <span> <?php if(function_exists('bcn_display'))
    {
        bcn_display();
    }?></span>
</div>

    <!-- section -->
    <div class="area">
    <section role="main">
            <h2><?php single_cat_title();  ?></h2>
        <!--<h2><?php the_category();  ?></h2>-->

         <?php echo category_description( $category_id ); ?> 
        <div class="display-posts-listing">

        <?php get_template_part('loop'); ?>
        </div>
        <?php get_template_part('pagination'); ?>

    </section>
    <!-- /section -->

<?php get_sidebar(); ?>
    </div>
<?php get_footer(); ?>

循环是这样的:

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



    <!-- article -->
    <div class="listing-item">
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <!-- post title -->
        <h3>
            <?php the_title(); ?>
        </h3>
        <!-- /post title -->
        <!-- post thumbnail -->
        <?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
                        <?php the_post_thumbnail('full'); // Declare pixel size you need inside the array ?>

        <?php endif; ?>
        <!-- /post thumbnail -->



        <!-- post details -->

                <!--<span class="author"><?php _e( 'Published by', 'html5blank' ); ?> <?php the_author_posts_link(); ?></span>-->
        <!--<span class="comments"><?php comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></span>-->
        <!-- /post details -->

        <?php //html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>

        <?php the_content(); ?>
        <?php edit_post_link(); ?>

    </article>
    </div>
    <!-- /article -->

<?php endwhile; ?>

<?php else: ?>

    <!-- article -->
    <article>
        <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
    </article>
    <!-- /article -->

<?php endif; ?>

由于

凯西

2 个答案:

答案 0 :(得分:0)

你应该尝试这样的事情:

if(is_category('your_category') :
    $args = array(
    'post_type'        => 'post',  
    'posts_per_page'   => 'how_many_posts_you_want_-1_if_all',
    'cat'                 => 'your_category_number',
    'orderby'             => 'title', 
    'order'       => 'ASC'
);
else :
    $args = array(
    'post_type'         => 'post',  
    'posts_per_page'    => 'how_many_posts_you_want_-1_if_all',
    'order'         => 'ASC'
);

endif;

$loop = new WP_Query( $args ); 

while($loop->have_posts()) : $loop->the_post(); 
//do your magic here
endwhile;

wp_reset_query();

这应该可以解决问题。

答案 1 :(得分:0)

快速编辑说明:我忽略了OP正在为类别页面请求它,我的建议是标记页面。应该能够通过将is_tag更改为is_category来适应它。希望它可以帮助某人。

我试图记住我是如何在一些旧主题上做到这一点的,今晚在新主题上遇到了麻烦。

在functions.php中

/* this controls some tag.php arguments */
    function modify_query_order_tag( $query ) {
       if ( $query->is_tag() && $query->is_main_query() && !is_admin() ) {
          $query->set( 'orderby', 'title' );
          $query->set( 'order', 'ASC' );
       }
    }
    add_action( 'pre_get_posts', 'modify_query_order_tag' );
tag.php中的

/* check functions.php for function modify_query_order_tag to modify arguments */
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    html

    <?php endwhile; endif; ?>

您当然可以在函数中添加其他参数,例如posts_per_page等。

(不知道我是否自己一起攻击它,或者我是否从更聪明的人那里得到它,可能来自其他地方。)

相关问题