WordPress.org

Ready to get started?Download WordPress

Forums

wordpress list posts and categories related to each post (5 posts)

  1. antistandard
    Member
    Posted 3 years ago #

    Hello everyone,

    I am simply trying to create a list of all posts in one category. For each post in that category, I also want to list the categories associated with it:

    Example:

    List the titles of all posts in cat5 and under each title, list the posts associated categories:

    Post title 1
    - dogs category
    - fish category

    Post title 3
    - fish category
    - birds category
    - insect category

    post title 4
    - birds category
    - dog category

    -------------------
    So in the backend, post 3 would have the birds and dog categories checked

    hope this makes sense

    thank you

  2. antistandard
    Member
    Posted 3 years ago #

    after re-reading this question I thought it might sound confusing.

    To make it easy to explain,

    I want to list all the posts in a category.

    Just say there are 3 posts in category 'animals'

    Now, with each of the 3 posts in 'animals' category, i want to show what categories they are associated with.

    So, the first post in the 'animals' category might have 'dogs' and 'fish' associated with it.

    and the second post might have the 'fish' 'birds' and 'insects' category associated with it.

    so it would look like

    Post title 1
    - dogs category
    - fish category

    Post title 3
    - fish category
    - birds category
    - insect category

    post title 4
    - birds category
    - dog category

    At the moment my code looks like:

    <? query_posts('cat=5&order=DESC');
    		if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    		<p class="whosrunning"><?php the_title(); ?></p>
    	    <? wp_list_cats('child_of=9');
    		endwhile; else:
    		echo "No members have events coming up at the moment";
    		endif;
    ?>

    which lists all posts in the 'animals' category but unfortunately lists out all the categories which are children of animals.

    what i want to happen is for the loop to look at each post individually and only show the categories that that particular post is associated with and then look at the next post etc etc.

    Thank you for you time

    James

  3. alchymyth
    Forum Moderator
    Posted 3 years ago #

    instead of:
    wp_list_cats('child_of=9'); (which is depracated and would show the categories related to the site, not the post)

    try and use the_category()
    http://codex.wordpress.org/Function_Reference/the_category
    (it will be linked category names)

    if you don't want linked category names, check http://codex.wordpress.org/Function_Reference/get_the_category

  4. antistandard
    Member
    Posted 3 years ago #

    Thanks Alchymyth, you know your php :)

    It works well, here's my code:

    <? query_posts('cat=5&order=DESC');
    		if ( have_posts() ) : while ( have_posts() : the_post(); 
    
    		?>
    		<p class="whosrunning"><?php the_title(); ?></p>
    		<?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(9, $childcat)) {
    echo $childcat->cat_name;
    }}
    
    		endwhile; else:
    		echo "No members have events coming up at the moment";
    		endif;
    ?>

    but do happen to know how I can modify this query to ONLY show posts that have category 9 cats associated with them?

    in other words, if a post exists, but does not have any category 9 cats ticked in it's edit screen, I don't want to show it.

    Thanks again for all your help

    James

  5. antistandard
    Member
    Posted 3 years ago #

    In addition I was hoping to also re-order the categories so they display in some manual way that I can set using a plugin or such.

    Thank you

Topic Closed

This topic has been closed to new replies.

About this Topic