Forums

Display list of post titles within get_categories? (5 posts)

  1. seaneherron
    Member
    Posted 1 year ago #

    Hey everyone,

    Running into a (small) issue that's bugging me. Basically, I want to display each category on the site in it's own div, and then inside that div, have a list of the 3 most recent posts in that category. So, ideally, a result should be output like this:

    <div class="categoryslug">
    <h3>Category Name</h3>
    <p>Category Description</p>
    <ul>
    <li><a href="linktopost1">Post 1</a></li>
    <li><a href="linktopost2">Post 2</a></li>
    <li><a href="linktopost3">Post 3</a></li>
    </ul>
    </div>

    This is what I have so far:

    $args=array(
      'orderby' => 'name',
      'parent' => '5',
      'order' => 'ASC',
      'hide_empty' => '0'
      );
    $categories=get_categories($args);
      foreach($categories as $category) {
        echo '
        <div class="' . $category->slug . '">
    		<h3>' . $category->name . '</h3>
    		<p>' . $category->description .'</p>
    		<ul>
                    <li>Link to post 1 should go here</li>
    		<li><a href="' . get_category_link( $category->term_id ) . '">More...</a></li>
    		</ul>
    		</div>
        ';
    }

    Any suggestions on how to do what I'm trying to do? I tried to do a loop in there using get_posts but failed miserably.

    Thanks so much for your help!

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    you probably need to break your echo string for the get_posts loop;

    what was your code, and where did it fail?

  3. seaneherron
    Member
    Posted 1 year ago #

    This is the code I previously had:

    [code moderated according to forum rules - please use the pastebin]

    With that, the page doesn't load at all. Thanks for your help!

  4. alchymyth
    The Sweeper
    Posted 1 year ago #

    as i suggested before, break the echo string at the point where you start the posts loop; the either connect the outputs into one long string or echo them directly:

    http://pastebin.com/CeR6iXXK

    or

    http://pastebin.com/Xqj5CcrB

  5. seaneherron
    Member
    Posted 1 year ago #

    You are a hero - thank you for your help!

Topic Closed

This topic has been closed to new replies.

About this Topic