• Hi everyone,

    Categories is something I find difficult to code in wordpress. It seems like there are not enough options to display posts in categories. Maybe someone can help me here.

    Just say I want to display a list, of all posts, but ordered in their respective category names….how would I do that please.

    For example:

    (Category) Holidays
    (Sub Category) Warm Places
    (Post 1) I went to an island in the ….
    (Post 2) We drove to a place called ….
    (Sub Category) Cold Places
    (Post 1) We went skiing in ….
    (Post 2) It was cold but fun in ….

    (Category) Animals
    (Sub Category) Dogs
    (Post 1) We have a black lab called ….
    (Post 2) Our dog does lots of tricks ….
    (Sub Category) Cats
    (Post 1) Our cat sleeps for ….
    (Post 2) Our cat sits by the fireplace ….

    Thank you anyone.
    Also, one a side note, don’t you hate it when you open the door for a cat and then it walks half way through the door and then just sits there so you can’t close the door and everyone watches you swear at the cat telling it to get inside or outside just not in the middle lolz

Viewing 5 replies - 1 through 5 (of 5 total)
  • Also, one a side note, don’t you hate it when you open the door for a cat and then it walks half way through the door and then just sits there so you can’t close the door and everyone watches you swear at the cat telling it to get inside or outside just not in the middle lolz

    don’t you wear boots … ? lol

    you example is a quite specific usage of categories; and an even more specific idea of generating this list – with no posts in the main categories, and sorted according to sub-categories – restricted to one sub-level.

    try to use something like this:

    function sub_category_posts_list() {
    $cats = get_categories();
    echo '<ul class="cat-list">';
      foreach($cats as $cat) :
        //only loop through categories which have subcategories
        if( get_categories('parent='.$cat->term_id) ) :
    	$cat_name=$cat->slug;
        echo ' <li class="cat-name"><h3>' . $cat->name . '</h3><ul>';
    	  // get the direct sub categories
    	  $sub_cats = get_categories('parent='.$cat->term_id);
    	    foreach($sub_cats as $sub_cat) :
    		$sub_cat_name=$sub_cat->slug;
    		echo ' <li class="sub-cat-name">' . $sub_cat->name;
    		// get all posts with that category
            $cat_posts = get_posts('numberposts=-1&category_name='.$sub_cat_name);
              foreach($cat_posts as $post) :
              setup_postdata($post);
              echo '<br/><span class="post-link"><a href="' . get_permalink() . '">' . get_the_title() . '</a></span>';
              endforeach; // closes 'foreach($cat_posts as $post)'
            echo '</li>';
    		endforeach; // closes 'foreach($sub_cats as $sub_cat)'
    		echo '</ul></li>';
    	endif;
      endforeach; // closes 'foreach($cats as $cat)'
    echo '</ul>';
    }

    the function would be added to functions.php of your theme;
    and could be called whereever by:
    <?php sub_category_posts_list(); ?>

    Thread Starter antistandard

    (@antistandard)

    Hi alchymyth,

    Thank you for the function.

    I will try it this weekend and tell you how I go.

    Thanks again,

    James

    Thread Starter antistandard

    (@antistandard)

    Hi Alchymyth,

    Thank you sooo much.

    Your function worked really well as a base for what I am doing. I really appreciate your help and your code is excellent.

    I had to edit a few things for formatting (of course) but the core code was perfect. Thank you !!

    The only thing I could not work out was how to list the post title. When I used <? echo single_post_title(); ?>, it returned the sub cat title instead.

    You have helped me more than enough with this so I don’t expect you to help me anymore, but ofcourse, if it is simple to solve I’d love to hear the solution.

    Thanks again,

    You rule

    James

    The only thing I could not work out was how to list the post title. When I used <? echo single_post_title(); ?>, it returned the sub cat title instead.

    to show the post title, you would use
    <?php the_title(); ?>

    or

    <?php echo get_the_title(); ?>

    as i suggested in the code snippet from my first reply (i.e. the post title linked to the single post):

    echo '<br/><span class="post-link"><a href="' . get_permalink() . '">' . get_the_title() . '</a></span>';

    Thread Starter antistandard

    (@antistandard)

    Great, thanks. I tried that and the result was that it returned the sub cat title instead of the post title, but I found a different way to display the post title using custom fields.

    Thanks again on your help. I really do appreciate it

    James

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Post list with their respective category names as headings’ is closed to new replies.