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?
Hi fuorilogo,
Can you give us the code that you’re using?
Best regards,
Nikko
add_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;
}
Hi 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,
Nikko
I 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;
}
Hi 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