1

Below is some basic code to call specficic categories to our front page post loop. It works fine, except my client wants the categories displayed in a particular order. I know there are other posts about this on the internet, but I didn't see any that addressed it the way my client is asking.

Can I take the $categories variable, created in the code below, and just call those objects into a new array?

In this instance the objects all have a number in front of them, like :

[0] => values
[1] => values
[2] => values
....

when the output is dumped.

Can I take that output and just call them in the order I want them? (ex: 1,3,4,2,5,0)

Current code:

`

    wp_reset_postdata();        
    /* top stories end */           

     $args = array(

        'child_of'                 => 0,
        'parent'                   => '',
        'hide_empty'               => 1,
        'hierarchical'             => 1,
        'orderby'                  => 'id',
        'order'                    => 'ASC',
        'include'                  => '12,13,14,15,16,1',
    ); 

     $categories = get_categories( $args );
 ?>

<?php
    echo '<div class="home-all-cat">';
     foreach( $categories as $cat)
     {
        $slug = $cat->slug ;
        echo '<div class="home-cat-item" >'; 
        echo '<span class="cat-title">'.$cat->name.'</span><span class="cat-archive-link"><a target="_blank" href="'.get_category_link( $cat->cat_ID ).'" title="'.$cat->name.'">Show all '.$cat->name.'</a></span>';
        echo do_shortcode('[blog number_posts="6" cat_slug="'.$slug.'" exclude_cats="1" title="" thumbnail="" excerpt="" excerpt_words="20" meta_all="no" meta_author="" meta_categories="" meta_comments="" meta_date="" meta_link="" paging="" scrolling="" strip_html="" blog_grid_columns="" layout="medium"][/blog]');
        echo '</div>';

     }
     echo '<div class="clear">';
     echo '</div>';
 ?>

`

  • What specific order are you looking for? Did you had a look at the orderby parameters in get_categories. Do you need to sort them differently than the excepted values in get_categories – Pieter Goosen Sep 4 '14 at 6:24
  • 6 objects are produced, which in the var dump show as: [0] => values [1] => values [2] => values There are 6 (0-5) and I need the order (1,2,3,4,5,0). Note that the first one needs to be the last one. So orderby won't work, as I only have ASC and DESC according to name, IDetc. But I need to control each object's position in the list according to the client's preference. – Garrett Sep 4 '14 at 21:36
  • Just noting, the include call is already in the right order. If I could instruct get_categories to not sort at all, it would be great. 'include' => '12,13,14,15,16,1', – Garrett Sep 5 '14 at 0:09
1

I think the most sane way and easiest way of doing this is to unset the first value in the returned array, and then adding it back at the end of the returned array before your foreach loop

For this to work, you will need to sort your categories by ID as you need to take category ID 1 and add that to the back. Something like this will work

<?php
$args = array(
    'hierarchical'             => 1,
    'orderby'                  => 'id',
    'order'                    => 'ASC',
    'include'                  => '13,21,41,1',
); 

$categories = get_categories( $args );

$v = $categories[0];
    unset($categories[0]);
    $categories[0] = $v;


foreach( $categories as $cat) {
    echo $cat->slug ;
}
?>
  • 1
    Excellent! What a great idea. Thanks so much, man, it worked perfectly. – Garrett Sep 5 '14 at 21:26
-2

Here is a workaround:

  1. Create a menu named Category Menu

    register_nav_menu( 'category-menu', "Category Menu );
    
  2. Now your client can configure the menu as he pleases. All you have to do is paste the code below where you want it to be displayed and voila

    wp_nav_menu('category-menu');
    
  • The OP has asked for a solution that displays the categories in a custom manor specific to his clients needs. Since he is working for a non-programmer I am pretty sure that if he changes his mind order-wise he wont be able to edit the php file and he will call him to do so, repeatedly. Creating a menu that gives the arranging power to the his client is a solution. But thank you for the quick downvote. – aifrim Sep 5 '14 at 6:53

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.