Forums

Advanced category and post list (8 posts)

  1. Gadzira
    Member
    Posted 3 years ago #

    I am trying to make a list in my sidebar of Categories with a sub list of the posts in that category... somewhat like this:

    <ul>
    <li>
        <a href="category_url">CATEGORY</a>
    
    <ul>
    <li><a href="post_url">POST</a></li>
    </ul>
    </li>
    </ul>

    Obviously I would like this to loop until it has played out all categories and posts within them.

    I am looking for tha code to make this happen in the same list format as I have custom styled menu tool in which i would like this code snippet to play out... so i need to di this WITHOUT A PLUGIN.

    Would really appreaciate any help!!

  2. Gadzira
    Member
    Posted 3 years ago #

    No takers??

  3. Gadzira
    Member
    Posted 3 years ago #

    Still no one able to give any assistance?

  4. Gadzira
    Member
    Posted 2 years ago #

    Whats the use of this support forum if there is no contribution :(

    I would really appreciate any input with this.

  5. AMP2307
    Member
    Posted 2 years ago #

    Hi,

    Don't know whether you still need help in this area, but I have the same requirement and have had no luck in finding a built in WordPress function to do it. I have therefore knocked the code below together, feel free to use it.

    It builds an Unordered List of top level categories and then a nested UL of the appropriate posts. It adds links to both the category list page and the posts themselves.

    If you need any help with it or if it doesn't quite do everything you need, let me know and I could add to it for you.

    To make it look good, you will have to do the appropriate CSS.

    <div id="sidebar">
        <ul>
            <?php
                $categories=get_categories();
                foreach($categories as $category) {
                    echo '<li><a href="'.get_category_link($category->cat_ID).'"><h2>'.$category->category_nicename."</h2></a></li><ul>";
                    $category_posts=get_posts('category='.$category->cat_ID);
                    foreach($category_posts as $post) {
                        echo '<li><a href="'.$post->guid.'">'.$post->post_title.'</a></li>';
                    };
                    echo "</ul>";
                }; ?>
        </ul>
    </div>
  6. websta
    Member
    Posted 2 years ago #

  7. Layop
    Member
    Posted 2 years ago #

    How would you do this for a specific category and all of its post?

  8. beepily
    Member
    Posted 2 years ago #

    Many thanks AMP2307. Your code got me up and running. I found that if I used "name" instead of "category_nicename" the categories were capitalized, which is what I wanted.

Topic Closed

This topic has been closed to new replies.

About this Topic