0

I have a site with posts in 16 categories ("departments"). The home page is a grid of the current (newest) post in each category.

I would like to create the HTML for displaying a department's current post and use a word press loop to repeat this 16 times to get the grid.

ALSO - and her is the part I need help with - the client wants to specify the categories AND the order they appear.

So, my page is the newest post from "Dining", then the newest from "travel", etc.

Here's the site currently where the previous designer did this by hand in a table every month, which is silly.

  • Do you have an example of what you have tried? – ngearing Oct 5 '15 at 23:25
  • @Nath just starting to code it. I'm confident I can loop the posts. I just don't know how to order the categories to match the order they are in currently – Steve Oct 5 '15 at 23:36
0

I am hoping that I understood your question, because recently I found myself needed to dynamically include a list of child categories and their count in a hierarchy menu in a post and so I came up with this:

I put this in file called listcat.php

<?php
/*
  Plugin Name: List Categories
  Reference: http://codex.wordpress.org/Template_Tags/wp_list_categories
  Description: Simple plugin to display categories in any post or page
  with a shortcode. It's basically a shortcode API interface to the
  wp_list_categories WordPress function.
*/
class ListCategories{
  static function list_categories($atts, $content = null) {
    $atts = shortcode_atts(
      array(
        'show_option_all'    => '',
        'orderby'            => 'name',
        'order'              => 'ASC',
        'style'              => 'list',
        'show_count'         => 0,
        'hide_empty'         => 1,
        'use_desc_for_title' => 1,
        'child_of'           => 0,
        'feed'               => '',
        'feed_type'          => '',
        'feed_image'         => '',
        'exclude'            => '',
        'exclude_tree'       => '',
        'include'            => '',
        'hierarchical'       => 1,
        'title_li'           => __( 'Categories' ),
        'show_option_none'   => __( 'No categories' ),
        'number'             => null,
        'echo'               => 1,
        'depth'              => 0,
        'current_category'   => 0,
        'pad_counts'         => 0,
        'taxonomy'           => 'category',
        'walker'             => null
      ), $atts
    );

    ob_start();
    wp_list_categories($atts);
    $output = ob_get_contents();
    ob_end_clean();
    return $output;
  }
}

add_shortcode( 'categories', array('ListCategories', 'list_categories') );

once that is done, I include it in my header file (technically you could put this anywhere but if you want it to be available to you anywhere, then make sure its a part that loads everywhere, like a header, footer, hooks or whatnot:

include '/server/path/to/the/file/listcat.php';

And then from any post, or page in the site you can use the shortcode to generate the list and hierarchy (if you want) of the categories/or child categories that you want dynamically and as they update, where its included will automatically update too, no need to do it again or manually update anything. Here is an example of what I did for myself:

[categories child_of=36 hide_empty=0 title_li='Explore Samples' orderby=id show_count=1]

and it produces this for me:

enter image description here


You can tweak this and style it to whatever you need, hope it helps you. I have also included the link to the Codex documentation so you can see what each option does and how it displays to help you decide what to use. Good luck.

  • Thanks. Nice short code approach. I am try to do something in a query and a loop though. – Steve Oct 6 '15 at 1:54
  • You should be able to adapt it to a query system, instead of setting up the shortcode, you can use the api itself wp_list_categories(attributes) to try and build it, similar to how it was used to build the shortcode. – GµårÐïåñ Oct 6 '15 at 16:41

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.