WordPress.org

Forums

[resolved] Want to alphabetize posts on custom pages (14 posts)

  1. David Borrink
    Member
    Posted 1 year ago #

    I have a store setup on a site, where there are custom pages calling up products in specific categories. I'd like to have those pages show the products in alphabetical order, not in chronological order.

    I found Custom Queries section on the Codex, and I'm struggling with understanding how to implement this with my existing categories. Obviously this is a function that needs to be added on my functions.php file.

    On the codex example, it talks about setting things up with a glossary category, but they don't specify "glossary" as the name of the category. So I'm not sure where I would put my category names in there. The example from the Codex....

    add_filter('posts_orderby', 'gloss_alphabetical' );
    add_filter('post_limits', 'gloss_limits' );
    
    function gloss_alphabetical( $orderby )
    {
      global $gloss_category;
    
      if( is_category( $gloss_category )) {
         // alphabetical order by post title
         return "post_title ASC";
      }
    
      // not in glossary category, return default order by
      return $orderby;
    }
    
    function gloss_limits( $limits )
    {
      global $gloss_category;
    
      if( is_category( $gloss_category )) {
         // remove limits
         return "";
      }
    
      // not in glossary category, return default limits
      return $limits;
    }

    On the site I'm working on, I have 23 custom store pages where each one calls up a specific category, and one page calls up everything in the store. I understand what the above filter is doing, but I'm not seeing how I would specify the product category for each page. Can this be handled in one function that covers all situations (that is, do I need to list all 23 categories in the one function)? Not sure what to do here.

  2. Christian1012
    Member
    Posted 1 year ago #

    If you're modifying the main query, I would do something more like this. It's a little simpler and easier to manage:

    function borrink_sort_categories( $query ) {
        // $query is global
    
        // Modify as long as it's the main query
        if ( ! is_admin() || ! $query->is_main_query() )
            return;
    
        // Sort all category archives by title ascending
        if ( $query->is_category ) {
            // Set order to ASC ( a,b,c)
            $query->set( 'order', 'ASC' );
            // Set order by post_title
            $query->set( 'orderby', 'title');
        }
    }
    add_action( 'pre_get_posts', 'borrink_sort_categories' );
  3. David Borrink
    Member
    Posted 1 year ago #

    Thanks, Christian,

    This isn't the main query, though. These are pages that have custom callups.

    One page calls up all "store" category posts and that's the entire store.

    Another page calls up "store-apple" category posts and that's all products about apples.

    And so on for 22 other pages. I only want these pages to show things alphabetically.

    The main blog page shows things in normal chronological order and I want it to remain that way.

    Thanks.

  4. Christian1012
    Member
    Posted 1 year ago #

    Back up for one second. When you say that the pages have custom callups, does that mean that the template for the page creates a new WP_Query object to loop through? Can you post the code to "store-apple" to pastebin, and I'll have a look at it for you?

    I'm just trying to figure out your site structure. I would understand if you have a "page" ( page not post ) for your store where you would create a custom loop for all your categories and all your products. That might be, mystore.com/store. However, your category template wouldn't need a custom loop, as mystore.com/apples would display all the products whose category is apples. If that's confusing I apologize.

  5. David Borrink
    Member
    Posted 1 year ago #

    Sure. Here's the coding for the store page called "page-store-apples.php"

    <div id="container">
    			<div id="content" role="main">
    
    			<?php
    			/* Run the loop to output the page.
    			 * If you want to overload this in a child theme then include a file
    			 * called loop-page.php and that will be used instead.
    			 */
    			get_template_part( 'loop', 'page' );
    			?>
    
                <div id="thumbframe">
    
                    <!-- To display unlimited number of posts, but only posts with a featured image will actually show up here,
                     since that's how this kind of post will show up. So only product posts should have a featured image -->
    				<?php query_posts( array ( 'category_name' => 'store-apples', 'posts_per_page' => -1 ) ); ?>      
    
    				<?php get_template_part( 'loop', 'thumb' ); ?>
    
    				<?php wp_reset_query(); ?>
    
                    </div>
    
    			</div><!-- #content -->
    		</div><!-- #container -->

    And the "thumbframe" div is a loop design that calls up the post featured image as a thumbnail, and a linkable title, which will take someone to the actual post where they can by a product. You'll note the custom query for a specific category of product, and then a reset.

  6. Christian1012
    Member
    Posted 1 year ago #

    You shouldn't be using query_posts. Query posts is an inefficient and limiting way to modify the main query.

    What you want in thumbframe div is a new WP_Query. Here's the code for the apple page, replace:

    <div id="thumbframe">
    
                    <!-- To display unlimited number of posts, but only posts with a featured image will actually show up here,
                     since that's how this kind of post will show up. So only product posts should have a featured image -->
    				<?php query_posts( array ( 'category_name' => 'store-apples', 'posts_per_page' => -1 ) ); ?>      
    
    				<?php get_template_part( 'loop', 'thumb' ); ?>
    
    				<?php wp_reset_query(); ?>
    
                    </div>

    with

    <!-- To display unlimited number of posts, but only posts with a featured image will actually show up here,
                     since that's how this kind of post will show up. So only product posts should have a featured image -->
    <?php
    // Query arguements http://codex.wordpress.org/Class_Reference/WP_Query
    $args = array(
        'category_name'     => 'store-apples',
        'posts_per_page'    => -1,
        'order'             => 'ASC',
        'orderby'           => 'title'
        );
    $my_query = new WP_Query ( $args );
    
    // Begin Custom Loop
    if ( $my_query->have_posts() ) : ?>
        <div id="thumbframe">
    
        <?php while ( $my_query->have_posts () ) : $my_query->the_post(); ?>
    
          <?php get_template_part( 'loop', 'thumb' ); ?>
    
        <?php endwhile; ?>
    
        </div> <!-- /#thumbframe -->
    
    <?php endif; ?>
  7. David Borrink
    Member
    Posted 1 year ago #

    Interesting. So I see what you're doing here. And it's more specific to the instance needed without being global, right?

    For placement. I see the thumbframe div is surrounding part of your code, while the arguments are before the thumbframe div. Do I replace the contents of the content div with your code, replacing the following beginning?...

    <?php
    			/* Run the loop to output the page.
    			 * If you want to overload this in a child theme then include a file
    			 * called loop-page.php and that will be used instead.
    			 */
    			get_template_part( 'loop', 'page' );
    			?>
  8. PlaneDan
    Member
    Posted 1 year ago #

    I have a similar request, only much more simple. I want the viewer of posts on my site to be able to request that they are presented in FIFO or LIFO order by date and time posted. In reading the above, is seems that it is not too complicated to do, but even if I could substitute the proper syntax, I would have no idea on how to implement it. I am an ASP developer, and am just starting to get into WP.

    Thanks for any help you can give me.

  9. David Borrink
    Member
    Posted 1 year ago #

    Christian,

    Could you reply to my question two posts up? I'd like to understand the placement of your code so I can try it out. Thanks! :-)

  10. David Borrink
    Member
    Posted 1 year ago #

    Christian,

    Okay, I understand what your code is doing. Defines the argument, gives it a title, calls up a query based on those arguments, starts a query, hands off those posts to the loop, which uses the template to create thumbnails and a title in a box for each post. Great.

    It seems pretty straight-forward, and I can use it on every page's template, just replacing the category name.

    I finally got around to trying your suggestion. I replaced the thumbframe div with everything your shared in the example. But when I tried it, I expected to see all my boxes reordered, but instead they didn't show up.

    I'm wondering if it helps to say this is a child theme off TwentyTen? It might be that your suggestions are based on TwentyEleven or TwentyTwelve?

  11. Christian1012
    Member
    Posted 1 year ago #

    Sorry for the delayed response David, but thanks for getting in touch to remind me about this thread. I looked over the issue again, and I can see why it may not work. The issue would lie in the contents of loop-thumb.php.

    Therein, you're likely to find separate loop logic. Or at least, that is my best guess. When your code calls get_template_part( 'loop', 'thumb' ), it's looking for loop-thumb.php and including that code right there. If there is another loop in the template, it has the high potential to create issues. So take a look in that file for another loop ...

    ( if( have_posts() ) : while( have_posts() ) : the_post();

    If you find one, and you're confident, you can comment it out. Otherwise, post it here and I'll give you a hand.

  12. David Borrink
    Member
    Posted 1 year ago #

    Okay, that makes perfect sense.

    Here is the code of loop-thumb.php below. What this does is create posts that contain only the thumbnail of the post product, and a title. So when the page calls up all the posts, it's basically a visual index to the actual posts where the products can be purchased. I just want to alphabetize all these thumbnails on a given catagory's page.

    <?php while (have_posts()) : the_post(); ?>
    
    <?php if( has_post_thumbnail() ): ?>
    	<div class="thumbbox">
                <div class="thumb-entry-title">
                <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( '%s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_post_thumbnail('thumb'); ?> <?php the_title(); ?></a></div>            
    
    	</div>
    <?php else: ?>
    <?php endif; ?>        
    
    <?php endwhile; ?>
  13. David Borrink
    Member
    Posted 1 year ago #

    SUCCESS! I figured out what you said, and I deleted both the "while" and "endwhile" statements from the loop-thumb.php file. That solved the problem, so it obviously was a double loop situation.

    I can now go through and replace the code on all the store pages with the proper categories.

    Thanks so much for your help, Christian. It always helps to have someone walk alongside for a few moments to help figure out how things work.

  14. Christian1012
    Member
    Posted 1 year ago #

    No problem, glad I could help!

Topic Closed

This topic has been closed to new replies.

About this Topic