-
AuthorPosts
-
May 10, 2023 at 9:16 am #1406962
Good morning lads. I love your theme!
I am writing to you for a problem: I need to sort the posts alphabetically only on the “Azzurri Marchigiani” page.
In this page I show the list of posts in the category “azzurri-marchigiani”.I had found among your support tickets a code that worked as it sorted the posts alphabetically, but it did it on all pages.
I need to isolate it for the page I linked to above.
can you produce the code for me?May 10, 2023 at 12:27 pm #1406981Hi fuorilogo,
Can you give us the code that you’re using?
Best regards,
NikkoMay 11, 2023 at 7:26 am #1407080add_filter( ‘pre_get_posts’, ‘custom_get_posts’ );
function custom_get_posts( $query ) {
if( is_category() || is_archive() ) {
$query->set( ‘orderby’, ‘name’ );
$query->set( ‘order’, ‘ASC’ );
}
return $query;
}May 12, 2023 at 10:44 am #1407204Hi fuorilogo,
Try to use conditional tags is_page() and use page id number: https://codex.wordpress.org/Conditional_Tags#A_PAGE_Page
In your case, the page id is 3383:is_page(3383) { // some code here }Hope it helps.
Best regards,
NikkoMay 12, 2023 at 11:20 am #1407216This reply has been marked as private.May 12, 2023 at 11:59 am #1407226I managed to force it:
i first assigned a custom class to the body_class of the page i wanted to modify and then i used the function using the conditional tag to check if it was a category and that specific class
lascio il codice se fosse utile per qualcuno
function add_body_class_for_page_id( $classes ) {
global $post;
if ( isset( $post->ID ) && $post->ID == 3383 ) {
$classes[] = ‘custom-class’;
}
return $classes;
}
add_filter( ‘body_class’, ‘add_body_class_for_page_id’ );add_filter( ‘pre_get_posts’, ‘custom_get_posts’ );
function custom_get_posts( $query ) {
if( is_category() && in_array( ‘custom-class’, get_body_class() ) ) {
$query->set( ‘orderby’, ‘name’ );
$query->set( ‘order’, ‘ASC’ );
}
return $query;
}May 13, 2023 at 10:56 pm #1407311Hi off-logo,
I’m glad that you were able to find the right code.
Thanks for using Enfold and have a great day!Best regards,
Nikko -
AuthorPosts
- The topic ‘Sort posts alphabetically only on a page showing posts in a specific category’ is closed to new replies.
