Sorting Category Posts Alphabetically

Community Forums Forums Design Tips and Tricks Sorting Category Posts Alphabetically

This topic is: resolved

This topic contains 13 replies, has 4 voices, and was last updated by  Kent 1 year, 6 months ago.

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #4490

    Kent
    Participant
    Post count: 58

    Site 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 | Circle me on Google+, and say hi so I can add you to my WordPress circle!

    • This topic was modified 1 year, 8 months ago by  Kent. Reason: I messed up
    • This topic was modified 1 year, 8 months ago by  Kent.
    • This topic was modified 1 year, 8 months ago by  Kent. Reason: I messed up
    • This topic was modified 1 year, 8 months ago by  Kent.
    #4495

    John
    Participant
    Post count: 157

    Hi 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 ;)

    #4498

    Kent
    Participant
    Post count: 58

    John,

    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 | Circle me on Google+, and say hi so I can add you to my WordPress circle!

    #4502

    Kent
    Participant
    Post count: 58

    John,

    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 | Circle me on Google+, and say hi so I can add you to my WordPress circle!

    #4509

    John
    Participant
    Post count: 157

    It 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 ;)

    #4512

    Kent
    Participant
    Post count: 58

    Yep. 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 | Circle me on Google+, and say hi so I can add you to my WordPress circle!

    #4528

    John
    Participant
    Post count: 157

    in_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 ;)

    #4535

    John
    Participant
    Post count: 157

    Kent,

    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 ;)

    #4536

    John
    Participant
    Post count: 157

    Oh, and these two Codex articles on categories should be helpful:

    Alphabetizing Posts

    Category Templates


    John Sundberg | blackhillswebworks.com
    A WordPress developer’s toolbox: Firebug | WordPress Codex | Google ;)

    #4560

    nickthegeek
    Keymaster
    Post count: 751
    #11054

    bluefrogj
    Participant
    Post count: 2

    I 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:

     

    [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();

    [/code]

    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?

     

    #19870

    Kent
    Participant
    Post count: 58

    @bluefrogj,

    I 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 | Circle me on Google+, and say hi so I can add you to my WordPress circle!

    #20055

    John
    Participant
    Post count: 157

    Hey 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 ;)

    #20056

    Kent
    Participant
    Post count: 58

    John,

    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 | Circle me on Google+, and say hi so I can add you to my WordPress circle!

Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.