wp query - How to order posts on each different category?

In wordpress, right now only on my main blog page, I managed to sort all posts with:<?php if ( is_active_sidebar( �

In wordpress, right now only on my main blog page, I managed to sort all posts with:

<?php if ( is_active_sidebar( 'blog-category' ) ) : ?>
                    <?php dynamic_sidebar( 'blog-category' ); ?>
                <?php endif; ?>
              </div>
              <div class="blog_list_content">
<?php
           global $wp_query;
             $args =  array(
                'meta_key' => 'publish_date',
                'orderby' => 'meta_value',
                'order' => 'DESC'
            );
            $args = array_merge( $wp_query->query, $args );
            query_posts( $args );
           if (have_posts()) :
               while (have_posts()) : the_post();
                    get_template_part( 'template-parts/content', get_post_format() );
               endwhile;
               theme_paging_nav();
           endif;
           ?>

When I click a blog category, the sorting doesn't work. Only on the main blog page. What do I need to do in order to sort the posts on other categories in the same way?

In wordpress, right now only on my main blog page, I managed to sort all posts with:

<?php if ( is_active_sidebar( 'blog-category' ) ) : ?>
                    <?php dynamic_sidebar( 'blog-category' ); ?>
                <?php endif; ?>
              </div>
              <div class="blog_list_content">
<?php
           global $wp_query;
             $args =  array(
                'meta_key' => 'publish_date',
                'orderby' => 'meta_value',
                'order' => 'DESC'
            );
            $args = array_merge( $wp_query->query, $args );
            query_posts( $args );
           if (have_posts()) :
               while (have_posts()) : the_post();
                    get_template_part( 'template-parts/content', get_post_format() );
               endwhile;
               theme_paging_nav();
           endif;
           ?>

When I click a blog category, the sorting doesn't work. Only on the main blog page. What do I need to do in order to sort the posts on other categories in the same way?

Share Improve this question asked Apr 17, 2019 at 23:54 Oscar AndresOscar Andres 11 silver badge1 bronze badge 3
  • See codex.wordpress/Category_Templates – Jarod Thornton Commented Apr 18, 2019 at 1:53
  • There isn't a part for sorting – Oscar Andres Commented Apr 18, 2019 at 4:09
  • The answer below is the next step following the category template :) "And you can use it to modify order on category archives too:" wordpress.stackexchange/questions/334643/… – Jarod Thornton Commented Apr 18, 2019 at 18:25
Add a comment  | 

2 Answers 2

Reset to default 2

First of all, you should not use custom query just to change the order of posts. This may cause problems with pagination and definitely is not optimal.

So first things first. Remove that part of your code:

        global $wp_query;
        $args =  array(
            'meta_key' => 'publish_date',
            'orderby' => 'meta_value',
            'order' => 'DESC'
        );
        $args = array_merge( $wp_query->query, $args );
        query_posts( $args );

All it does is changing few params and calling the query again. But there is an action, that allows you to add your custom parameters before running the query: pre_get_posts. And you can use it to modify order on category archives too:

function my_set_custom_order( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {  // modify only main query on front-end
        if ( is_home() ) {
            $query->set( 'meta_key', 'publish_date' );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'DESC' );
        }
        if ( is_category( 'cats' ) ) {
            $query->set( 'meta_key', 'cat_name' );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'ASC' );                
        }
        // ...
    }
}
add_action( 'pre_get_posts', 'my_set_custom_order' );

This will sort your homepage DESC by publish_date and your cats category ASC by cat_name.

You can add whatever you want/need in there and you can use Conditional Tags to modify queries only for some requests.

You most likely updated your index.php right? In the case of category pages you can use category.php (or archive.php but category.php is more specific).

The Codex has a good example that you could adapt fairly easily: https://codex.wordpress/Category_Templates#Different_Text_on_Some_Category_Pages

<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages, 
I could be left blank</p>
<?php endif; ?>

Alternatively, you could get VERY specific, and customize by category slug, having a unique template for every category you create. So if you had three categories called:

  • Cats
  • Dogs
  • Hamsters

You would have three templates called:

  • category-cats.php
  • category-dogs.php
  • category-hampsters.php

In order to have custom sorting on each of those templates, you'd customize the wp_query args with the "orderby" argument. See here: https://codex.wordpress/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

I hope that clears it up a bit. Good luck!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745577684a4634066.html

相关推荐

  • wp query - How to order posts on each different category?

    In wordpress, right now only on my main blog page, I managed to sort all posts with:<?php if ( is_active_sidebar( �

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信