Support » Fixing WordPress » Editing "The Loop". Need list alphabetical

  • Toby

    (@toby-barnes)


    Hi folks,
    I have been customizing a category-categoryname.php file, and I am having ‘fun’ with The Loop, trying to get the list of Pages (not Posts) to display alphabetically.

    I have a feeling that the answer is probably hidden in plain sight, but I’ve working at this late and I just can’t see it.

    Note: I am using a plugin to enable Pages to work with Categories:
    “Add Categories to Pages.”

    Here is where I got to before trying to make the list display A-Z.
    It all works fine:

    <?php // The Loop START
    
    while ( have_posts('orderby=title&order=ASC') ) : the_post('orderby=title&order=ASC'); ?>
    <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
    <?php endwhile; 
    
    else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    
    <?php endif; // The Loop END ?>

    … but I am trying to make the list of pages associated within the category list Alphabetically.

    I tried adding ‘orderby=title&order=ASC’ inside:
    while ( have_posts( here ) ) : the_post( or here );
    … but I found that this doesn’t work.

    I found this thread, where the OP provides some solution at the end, but he didn’t explain where exactly he placed it within The Loop, and the thread is closed.
    https://wordpress.org/support/topic/sort-posts-alphabetically-or-by-date-depending-on-category?replies=5

    I tried to use code from this closed thread, but my results just displayed all my Posts and not my Pages under the one Category:
    https://wordpress.org/support/topic/orderbytitle-not-working?replies=11

    Any help would be greatly appreciated.
    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi, have you added this code below to your function.php file? ;

    function add_taxonomies_to_pages() {
     register_taxonomy_for_object_type( 'post_tag', 'page' );
     register_taxonomy_for_object_type( 'category', 'page' );
     }
    add_action( 'init', 'add_taxonomies_to_pages' );
     if ( ! is_admin() ) {
     add_action( 'pre_get_posts', 'category_and_tag_archives' );
    
     }
    function category_and_tag_archives( $wp_query ) {
    $my_post_array = array('post','page');
    
     if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
     $wp_query->set( 'post_type', $my_post_array );
    
     if ( $wp_query->get( 'tag' ) )
     $wp_query->set( 'post_type', $my_post_array );
    }
    Toby

    (@toby-barnes)

    Thanks for replying. I tried adding your code into the functions.php file but unfortunately it caused the page to fail “Fatal error …”

    Solution:
    I have just tried putting this whole line straight before The Loop and it now seems to be working:
    <?php query_posts($query_string.'&orderby=title&order=ASC'); ?>

    I have found that putting this before The Loop, displays the list by Date, “newest” at the top:
    <?php query_posts($query_string.'&orderby=date'); ?>

    And to order the list by Date, “oldest” at the top, use this instead:
    <?php query_posts($query_string.'&orderby=date&order=ASC'); ?>

    – – – – – – – – – – – – – – – – – – – – – – – – – – –

    For those who are new to this, I have put the above examples (trying one example at a time, not multiple lines) before The Loop, as shown here:

    <?php // The Loop START
    
    while ( have_posts() ) : the_post(); ?>
    <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
    <?php endwhile; 
    
    else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    
    <?php endif; // The Loop END ?>

    The <h4> tags style the text (of the auto-generated list of Page/Post titles) to display as a H4 header. In my case I have used CSS (not shown here) to set the H4 to be the same size as the regular <p> body text, only bolder.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Editing "The Loop". Need list alphabetical’ is closed to new replies.