• Resolved bluedrag

    (@bluedrag)


    I’m attempting to display all my posts within the artists category in alphabetical order on the category page, and I can’t seem to get it working. I’m following these instructions: https://codex.www.ads-software.com/Alphabetizing_Posts

    Here is the code in my category.php page. With this code my posts are shown alphabetically, but ALL my posts are shown (other categories as well)

    
    <?php
    /**
     * The template for displaying Category Archive pages.
     */
    
    get_header(); ?>
    
    <?php
    	// we add this, to show all posts in our
    	// Glossary sorted alphabetically
    	if (is_category('artists')) {
    		$args = array('posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC');
    		$artistposts = get_posts($args); 
    	}
    ?>
    
    	<h1><?php single_cat_title(); ?></h1>
    	
    	<div id="artist-grid" role="main">
    	<?php while (have_posts()) : the_post(); ?>
    
    		<?php foreach($artistposts as $post) :	setup_postdata($post); ?>
    			<a class="product_thumbnail" href="<?php the_permalink() ?>">
    				<?php //the_post_thumbnail grabs the post's "featured image" this feature has added theme support in the child theme functions.php ?>
    				<?php the_post_thumbnail('thumbnail', array('class' => 'thumbnail')); ?>
    				
    				<?php //This piece of code figures out the corrosponding post_title of the featured image ?>
    				<figcaption class="post_caption">
    					<h1><?php the_title(); ?></h1>	
    				</figcaption>
    			</a> <!-- product_thumbnail -->
    		<?php endforeach; ?>
    
    	<?php endwhile; // end of the loop. ?>
    			
    			<div class="clear"></div>
    
    	</div><!-- #content -->
    
    <?php get_footer(); ?>
    
Viewing 13 replies - 1 through 13 (of 13 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    That’s not how I’d do it; I’d use pre_get_posts() to add a sort by title on for the existing archive page(s).

    https://codex.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts

    Thread Starter bluedrag

    (@bluedrag)

    I don’t really understand how I would do this. I have two post categories. One I need to display by date (newest to oldest) and the other alphabetically.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    In that case, yes, you need to do two loops.

    Thread Starter bluedrag

    (@bluedrag)

    I’m afraid you’re overestimating my programming abilities. Should my code above be working? Am I close or far off?

    Thank you for responding to my post.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I don’t think that snippet has anything to do with what you want to do.

    You want to do two wp_query calls, one for the category you want one way and one for the category you want the other.

    https://codex.www.ads-software.com/Class_Reference/WP_Query

    Thread Starter bluedrag

    (@bluedrag)

    I actually have two separate category templates for each category, so so the code you see above will only effect the category I am trying tosort alphabetically. Sorry I should have mentioned that above.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    In that case, it’s back to pre_get_posts. Your code needs to check which archive it’s working with.

    Thread Starter bluedrag

    (@bluedrag)

    Could you please be more specific? Possibly give me an example of how that would work in code? I appreciate your help but programming is not my strong suit.

    Moderator bcworkz

    (@bcworkz)

    Steve is right, pre_get_posts is the best way to manage this. But given your experience level, maybe going with what’s easiest is the best solution. As long as you do not have a huge number of posts (thousands), it will not make much difference in user experience.

    The code you are using is mixing two different approaches (pre_get_posts is yet a third). Either one is valid, but needs to be implemented consistently. The easiest fix is to simply remove the while have posts line and the corresponding endwhile. Also add this above the foreach line (within a <?php ?> block):
    global $post;
    That with the foreach loop along with setup_postdata($post) is enough to run a proper loop. There may be additional fine tuning, but removing those lines and adding the global declaration should generally achieve what you want.

    Thread Starter bluedrag

    (@bluedrag)

    Hi bcworkz,

    Thank you so much for your in depth response! I am definitely on the right track now. The only issue I seem to be having is, posts from seperate categories are showing on my category page. This leaves me to believe the if (is_category('artists')) is not working properly. The category is called Artists (the slug is artists) Do you have any insight into why this may not be working properly?

    Here is the whole category.php page:

    
    <?php
    /**
     * The template for displaying Category Archive pages.
     */
    
    get_header(); ?>
    	<h1><?php single_cat_title(); ?></h1>
    	
    	<div id="artist-grid" role="main">
    		<?php // we add this, to show all posts in our category sorted alphabetically
    			if (is_category('artists')) {
    				$args = array('posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC');
    				$artistsPosts = get_posts($args); 
    			} ?>
    
    		<?php global $post; ?>
    		
    		<?php foreach($artistsPosts as $post) :	setup_postdata($post); ?>
    			<a class="product_thumbnail" href="<?php the_permalink() ?>">
    				<?php //the_post_thumbnail grabs the post's "featured image" this feature has added theme support in the child theme functions.php ?>
    				<?php the_post_thumbnail('thumbnail', array('class' => 'thumbnail')); ?>
    				
    				<figcaption class="post_caption">
    					<h1><?php the_title(); ?></h1>	
    				</figcaption>
    			</a> <!-- product_thumbnail -->
    		<?php endforeach; ?>
    
    			<div class="clear"></div>
    			
    	</div><!-- #artist-grid -->
    
    <?php get_footer(); ?>
    

    Again, thank you for your detailed response!

    Moderator bcworkz

    (@bcworkz)

    Hmmmm, AFAICT it should be working. However, I think I misunderstood your template organization. Your template will no longer work as a generic category template. Please rename this template category-artists.php. Reinstate your theme’s category.php (or whatever its current name is) template in its original state. It will be used for all other categories and this category-artists.php template will be used only for artists category requests.

    Thus you do not need the is_category() conditional. Assign $args and get posts unconditionally. I think what’s going on now is not that is_category() is not working, but that the current template cannot handle non-artists categories. I’m essentially asking you to setup your templates the way I thought they were to begin with.

    Thread Starter bluedrag

    (@bluedrag)

    Thanks bcworkz ??

    I was actually able to solve the issue by adding category_name => artists to the code:

    				
    $args = array('posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC', 'category_name' => 'artists');
    				$artistsPosts = get_posts($args); 
    

    Appreciate all the help!

    Moderator bcworkz

    (@bcworkz)

    Happy to help. I should have caught the missing category_name. At least one of us did. Team effort FTW!! ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Displaying Posts in Alphabetical Order’ is closed to new replies.