WordPress.org

Ready to get started?Download WordPress

Forums

Display list of headlines within a specific category (3 posts)

  1. theturninggate
    Member
    Posted 5 years ago #

    Hello,

    I'm attempting to convert my Textpattern blog over to WordPress (hurray, a convert!). WordPress' tag structure is very different from Textpattern's, however, and I'm having some trouble.

    I'd like to create a menu of entries within a category. Is there a tag that will list headlines within a specific category?

    Thanks,
    theturninggate

  2. tugbucket
    Member
    Posted 5 years ago #

    the get_posts tag this is dependent on knowing the category ID and calling each one individually.

    you can alternatively make a function and add it to your functions.php in your theme like so:

    /* list categories with posts by tugbucket.net */
    function cat_and_posts(){
    foreach (get_categories(array('hide_empty'=>true)) as $category)
    {
    $catid = $category->cat_ID;
    global $post;
    $myposts = get_posts('category='.$catid);
    echo '<li>' . $category->cat_name . "\n";
    echo '<ul>' . "\n";
    foreach($myposts as $post) {
    echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
    }
    echo '</ul>' . "\n";
    echo '</li>' . "\n";
    }
    };
    /* end */
    1. So we get all the categories that are not empty.
    2. We make a variable that is each categories ID.
    3. Set the posts global.
    4. Set a variable for the get_posts function where the category is our first variable.
    5. Then start building a list where the first is the category name.
    6. Then nest a list inside that listing each post in that category.
    7. Finally close out all the lists in order.

    Call it in your template like so:

    <ul>
    <?php cat_and_posts(); ?>
    </ul>

    It will output something like this:

    • Category Name
      • Post 1
      • Post 2
    • Category Name
      • Post 1
      • Post 2
  3. theturninggate
    Member
    Posted 5 years ago #

    Thanks, tugbucket. That's working nicely.

    Is it possible, though, to specify a particular category or categories using a list of category names?

    Also, I changed the output code some:

    /* list categories with posts by tugbucket.net */
    function cat_and_posts(){
    foreach (get_categories(array('hide_empty'=>true)) as $category)
    {
    $catid = $category->cat_ID;
    global $post;
    $myposts = get_posts('category='.$catid);
    echo '<dt>' . $category->cat_name . '</dt>' . "\n";
    
    foreach($myposts as $post) {
    echo '<dd><a href="' . get_permalink() . '">' . get_the_title() . '</a></dd>' . "\n";
    }
    }
    };
    /* end */

Topic Closed

This topic has been closed to new replies.

About this Topic