Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #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?

    #1406981

    Hi fuorilogo,

    Can you give us the code that you’re using?

    Best regards,
    Nikko

    #1407080

    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;
    }

    #1407204

    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

    #1407216
    This reply has been marked as private.
    #1407226

    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;
    }

    #1407311

    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

Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘Sort posts alphabetically only on a page showing posts in a specific category’ is closed to new replies.