admin管理员组文章数量:1022203
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
2 Answers
Reset to default 2First 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!
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
2 Answers
Reset to default 2First 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!
本文标签: wp queryHow to order posts on each different category
版权声明:本文标题:wp query - How to order posts on each different category? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745577683a2157121.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论