• Hi, please I need help. There should be a code to add to the functions.php that sorts the posts alphabetically in the front-end. But I cant find it.

    I found a code but it has 2 problems. It sorts only one category and it seems to have a bug, I can’t add any more code (error).

    Thanks four your answers!

    function modify_query_for_categories( $query ) {
    if ( $query->is_category('info-a-z') ) {
    $query->set( 'orderby', 'title' ); // es soll nach dem Titel sortiert werden
    $query->set( 'order', 'ASC' ); // die Sortierung soll alphabetisch sein
    $query->set( 'posts_per_page', '25' ); // es sollen 25 Beiträge angezeigt werden
    }
    }

    add_action( 'pre_get_posts', 'modify_query_for_categories' );

    • This topic was modified 6 years, 4 months ago by Jan Dembowski.
    • This topic was modified 6 years, 4 months ago by Brisch.

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter Brisch

    (@brisch)

    Thanks I saw both of them. The first link revers to the cathegory.php and the plugins mentioned in the second are too old.

    The first link doesn’t just refer to category.php though. Read through it again. A theme’s category.php is just built off index.php, which it sounds like maybe is the template you want to edit (if you only want to alphabetize the blog/front page).

    Have you created a child theme? That might be the best way to start going about these sorts of changes on specific screens.

    Thread Starter Brisch

    (@brisch)

    Yes, I have a child theme. And sorry, I still can’t find what code to add to the functiond.php. Can you please copy the code in your answer, thank you.

    • This reply was modified 6 years, 4 months ago by Brisch.

    if you want to sort the post in the posts page and all archive pages, including search result, try this code:

    function posts_sort_alphabetical( $query ) {
        if ( is_admin() || ! $query->is_main_query() )
            return;
     
            $query->set( 'orderby', 'title' );
            $query->set( 'order', 'ASC' );
            return;
        
    }
    add_action( 'pre_get_posts', 'posts_sort_alphabetical', 1 );
    Thread Starter Brisch

    (@brisch)

    Thank you! It did not work.

    https://betriebsrat-kwp.uni-ak.ac.at/wp-content/uploads/2020/01/alphabet.png

    “K” is still before “F”

    • This reply was modified 6 years, 4 months ago by Brisch.
    Thread Starter Brisch

    (@brisch)

    I also tried this plugin but did not work:
    https://ww.wp.xz.cn/plugins/sort-categories-by-alphabetical-order/

    what theme are you using?

    what is the full code of the theme’s index.php file?

    Thread Starter Brisch

    (@brisch)

    Fluida Plus (child) Cryout Ceations
    http://demos.cryoutcreations.eu/wp/fluida/
    This is the index.php – but it is not in the child theme now

    <?php
    /**
    * The main template file.
    *
    * This is the most generic template file in a WordPress theme
    * and one of the two required files for a theme (the other being style.css).
    * It is used to display a page when nothing more specific matches a query.
    * E.g., it puts together the home page when no home.php file exists.
    *
    * @package Fluida
    */
    get_header();
    ?>
    <div id="container" class="<?php echo fluida_get_layout_class(); ?>">
    <main id="main" role="main" class="main">
    <?php cryout_before_content_hook(); ?>

    <?php if ( have_posts() ) : ?>

    <div id="content-masonry" class="content-masonry" <?php cryout_schema_microdata( 'blog' ); ?>>
    <?php /* Start the Loop */
    while ( have_posts() ) : the_post();
    get_template_part( 'content/content', get_post_format() );
    endwhile;
    ?>
    </div> <!-- content-masonry -->
    <?php fluida_pagination(); ?>

    <?php else :
    get_template_part( 'content/content', 'notfound' );
    endif; ?>

    <?php cryout_after_content_hook(); ?>
    </main><!-- #main -->

    <?php fluida_get_sidebar(); ?>
    </div><!-- #container -->

    <?php
    get_footer();

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘posts sort alphabtically in fuctions.php’ is closed to new replies.