1

As the title says. Would be grateful for your help. Thank you.

  • The (relevant part of) your theme's category.php file might be a helpful addition to this question. – Johannes Pille Apr 29 '13 at 12:08
  • I'm just using the default twenty twelve theme at the moment – rabbid May 1 '13 at 1:03
  • Sorry what I meant to say was I'm using a child of the default theme – rabbid May 1 '13 at 1:10
5

Try to do all theme modifications in functions.php whenever possible. It keeps the theme files clean and uncluttered. Here's an example using the pre_get_posts action:

function order_category_archives( $query ) {
  if ( is_category() && $query->is_main_query() ){ // is_category() can specify a category, if necessary
    $query->set( 'orderby', 'title' );
    $query->set( 'order', 'ASC' );
  }
}

add_action( 'pre_get_posts', 'order_category_archives' );

Notice that this uses is_category() to modify the query only if we're looking at a category archive page. An optional parameter can be added to specify which category (or categories) this should be limited to.

Finally, we use is_main_query() to avoid modifying any additional queries that may be on the page.

  • Thanks for your answer. I am unsure on how to use this. I am using a child theme of default twenty twelve. I copied functions.php into my child theme and pasted this function and action into it, but apparently I'm not allowed to have two functions.php? So I pasted this into the original functions.php. I then wrote pre_get_posts() into category.php but I guess that's wrong as well. Sorry I know zip about Wordpress coding. Thanks for your help. – rabbid May 1 '13 at 1:16
  • Sorry would like to clarify after reading my comment. What happened after I use pre_get_posts() in category.php is I got the Fatal Error: Call to a member function is_main_query() on a non-object. What should I do? Thank you – rabbid May 1 '13 at 9:08
  • I accidentally figured it ou. Thanks a lot! This was a good solution. – rabbid May 1 '13 at 15:05
-2
<ul>

<?php

    // we add this, to show all posts in our
    // Glossary sorted alphabetically
    if (is_category())
    {
        $args = array( 'posts_per_page' =&gt; -1, 'orderby'=&gt; 'title', 'order' =&gt; 'ASC' );

        $glossaryposts = get_posts( $args );
    }

    foreach( $glossaryposts as $post ) :
        setup_postdata($post);
        ?>
        <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>;

<?php endforeach; ?>

</ul>
  • This won't actually work, since the secondary loop is an entirely new query, that doesn't reference the main query - and therefore won't actually display posts queried by the main query, i.e. the specific category's posts. – Chip Bennett Apr 29 '13 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.