【问题标题】:Wordpress - how to order posts within each category alphabetically by title, without it showing all posts from all categoriesWordpress - 如何按标题按字母顺序排列每个类别中的帖子,而不显示所有类别的所有帖子
【发布时间】:2014-03-11 02:41:01
【问题描述】:

我在每个 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; ?>

谢谢

凯茜

【问题讨论】:

  • 您可以发布您目前用于构建 wp_query 的代码吗?
  • 发布更新谢谢:)

标签: php wordpress title categories


【解决方案1】:

你应该试试这样的:

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();

这应该可以解决问题。

【讨论】:

  • 我认为第一行缺少 ) 但我已经添加了这个,但仍然无法正常工作,它仍然列出了所有帖子,我也不想指定类别,我希望所有类别自动单独显示他们的帖子 AZ,而不是所有类别都显示它现在正在做的所有帖子 AZ。
【解决方案2】:

快速编辑说明:我忽略了 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 等。

(不知道是我自己破解的,还是我从其他更聪明的人那里得到的。可能来自更聪明的人。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    相关资源
    最近更新 更多