Community Forums › Forums › Archived Forums › Design Tips and Tricks › Sorting Category Posts Alphabetically
Tagged: alphabetically, archive, category, sorting
- This topic has 13 replies, 4 voices, and was last updated 11 years, 12 months ago by
Kent.
-
AuthorPosts
-
December 11, 2012 at 8:53 am #4490
Kent
ParticipantSite I'm working on is here: http://www.ibsltd.ca/pub/fredericton_royals/
I found this code over on in the old Forums to sort category posts alphabetically. What I'm lost on is how/what I insert to sort only SPECIFIC category archives alphabetically. I know I have to put in the category IDs, but my syntax knowledge isn't good enough to know where.
add_action('template_redirect', 'child_conditional_actions');
/** Loads Conditional Actions after the Query is formed before the template is loaded **/
function child_conditional_actions() {
if( is_category() ) {
remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', 'child_do_abc_loop');
}
}/**Loads Custom Loop to make query sort Alphabetically**/
function child_do_abc_loop() {
genesis_custom_loop( array( 'orderby' => 'title', 'order' => 'DESC' ) );
}Also, I tried to use HTML on the code above to format it differently and it didn't work. What's the preferred/best practice for inserting snippets of code into these forums?
Thanks,
-Kent
Dad. Biker. Designer. | kentfackenthall.com
December 11, 2012 at 9:07 am #4495John
ParticipantHi Kent,
When in doubt, I nearly always go to the WordPress Codex and search for the function or whatever it is I have a question on. I think you'll find your answer on this Codex page in the examples:ย http://codex.wordpress.org/Function_Reference/is_category
And for inserting code here in the forums, after I type or paste it in I switch over to the HTML tab, select the code, and click on the "code" button in the toolbar. Your code needs to be single-spaced for it to work, but I've found that I can switch back and forth from Visual to HTML and the code styling stays there. Here's an example (from that Codex page):
is_category( '9' );
// When the archive page for Category 9 is being displayed.John
John Sundberg | blackhillswebworks.com
A WordPress developer’s toolbox: Firebug | WordPress Codex | Google ๐December 11, 2012 at 9:29 am #4498Kent
ParticipantJohn,
Thanks for the scoop. I'm noticing some differences in the code.
Does it matter if I use 'in_category' or 'is_category'? There's both in my functions.php already. I'm curious if there's a distinction or if they will both do the same thing.
-Kent
Dad. Biker. Designer. | kentfackenthall.com
December 11, 2012 at 9:55 am #4502Kent
ParticipantJohn,
I've tried it out, it's actually breaking the archive. now the page isn't listing any posts from the category at all, rather all the posts from other categories. If you look atย http://www.ibsltd.ca/pub/fredericton_royals/category/players/a/, it's supposed to be an archive for category 51, but actually there's no posts from that category at all. Weird. Here's the code I put in functions.php:
/** Sort Specific Category Posts Alphabetically **/
add_action('template_redirect', 'child_conditional_actions');
/** Loads Conditional Actions after the Query is formed before the template is loaded **/
function child_conditional_actions() {
if( is_category( array( '50', '51' ) )) {
remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', 'child_do_abc_loop');
}
}
/**Loads Custom Loop to make query sort Alphabetically**/
function child_do_abc_loop() {
genesis_custom_loop( array( 'orderby' => 'title', 'order' => 'DESC' ) );
}I'm going to keep hacking away at it.
Dad. Biker. Designer. | kentfackenthall.com
December 11, 2012 at 10:24 am #4509John
ParticipantIt looks like if you're using an array you need to NOT put the single-quote around the ID. Check out the example from the Codex:
is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) );
// Returns true when the category of posts being displayed is either term_ID 9, or slug "blue-cheese", or name "Stinky Cheeses".
John Sundberg | blackhillswebworks.com
A WordPress developer’s toolbox: Firebug | WordPress Codex | Google ๐December 11, 2012 at 10:40 am #4512Kent
ParticipantYep. Still not working.
I've tried with and without quotes, using 'in' category and 'is' category. Nada. must be something else. I'm not sure what's up. Funny, because I have this function right above it and it works fine:
// Customize the post info function
add_filter('genesis_post_info', 'post_info_filter');
function post_info_filter($post_info) {
if (in_category( array( '4', '8', '5', '6', '7' ) )) {
$post_info = '[post_date] [post_edit]';
return $post_info;
}}I'll keep digging. ๐
Dad. Biker. Designer. | kentfackenthall.com
December 11, 2012 at 11:29 am #4528John
Participantin_category lets you check if a post or posts are in a specific category, then you can do stuff with those posts.
is_category checks if a specific category (or categories) archive is being displayed, then lets you do stuff with those categories.
John Sundberg | blackhillswebworks.com
A WordPress developer’s toolbox: Firebug | WordPress Codex | Google ๐December 11, 2012 at 12:03 pm #4535John
ParticipantKent,
I would try this:
Make a copy of page.php from your Genesis directory, and rename it category.php
In that file, above the ending genesis(); function, add this code:
if is_category( 'players' ) {
// Customize the Loop
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'players_category_order_custom_loop' );
function players_category_order_custom_loop() {
global $paged;
$args = array( 'order' => 'ASC','orderby' => 'name','paged' => $paged );
// Accepts WP_Query args (http://codex.wordpress.org/Class_Reference/WP_Query)
genesis_custom_loop( $args );
} // Endย players_category_order_custom_loop
} // End is_category( 'players' )You could clean that up with some line breaks and indenting, but the code editor here won't let us do that.
You may also want to flush your permalinks: go to Settings > Permalinks and click "Save Changes"
John
John Sundberg | blackhillswebworks.com
A WordPress developer’s toolbox: Firebug | WordPress Codex | Google ๐December 11, 2012 at 12:08 pm #4536John
ParticipantOh, and these two Codex articles on categories should be helpful:
John Sundberg | blackhillswebworks.com
A WordPress developer’s toolbox: Firebug | WordPress Codex | Google ๐December 11, 2012 at 2:58 pm #4560nickthegeek
MemberSomething else to check out
January 10, 2013 at 4:46 pm #11054bluefrogj
MemberI must admit I'm trying the same thing and it's not working at all. I've been playing with it for 2 days and can't get it to work. I've read the Alphabetizing Posts codex before finding this thread created a template file named category-58.php with the following code:
<?php get_header(); ?> <div id="content"> <?php // we add this, to show all posts in our // Glossary sorted alphabetically $posts = query_posts($query_string . '&orderby=title&order=asc&posts_per_page=-1'); // here comes The Loop! if (have_posts()) : while (have_posts()) : the_post(); ?> genesis();
That's direct from WP's site. Breaks the page and I get a 500 error.
I"ve read about category templates. Understand them. I just can't get this to work. I'm not great at this, but I was able to figure out enough to build some custom taxonomies and to widgetize a couple of page templates...and to sort order some custom post types. But not specific categories. <sigh> I'm doing something wrong too. Anyone found a solution yet?
February 12, 2013 at 1:09 pm #19870Kent
ParticipantI had to put this project down for awhile, but have to get back to it shortly. Did you ever get it working? I'll have to review the above and see how it goes.
-Kent
Dad. Biker. Designer. | kentfackenthall.com
February 13, 2013 at 7:39 am #20055John
ParticipantHey Kent,
Just checked out your blog - how are you liking the Pugsley? I've got a Salsa Fargo myself that I ride/commute with year-round here in South Dakota, swapping wheels/tires back and forth depending on the snow and ice conditions.
John
John Sundberg | blackhillswebworks.com
A WordPress developer’s toolbox: Firebug | WordPress Codex | Google ๐February 13, 2013 at 7:52 am #20056Kent
ParticipantJohn,
Nice! Those Fargos are pretty cool. I've been commuting a bunch of years now, mostly on a Cross Check but I've always lusted for the fatbike. I've only had it out twice so far, but it was a blast! I'm really looking forward to a regular commute on it.
I don't know if you're on Google+ at all, but there's a real vibrant Bike Commuting Community over there sharing tips, tales and encouragement - a good deal of them hearty winter commuters as well. You might want to check it out! I spend my days over there! ๐
Cheers!
-Kent
Dad. Biker. Designer. | kentfackenthall.com
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.