WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show a single category and up to 10 posts in a menu (5 posts)

  1. Flocktome
    Member
    Posted 5 years ago #

    Hi All,

    Ok, so, i have a category called "tours-of-duty" which is category id 4

    I want to be able to show this in the top menu, as well as up to 10 of the lastest posts in it. I have seen how to get posts from a particlar category, and show all categories, but not just bfrom 1 category.

    My desired out put would be like:

    <ul>
    <!-- standard WP menu output -->
        <li class="first current_page_item"><a href="http://localhost/501tag/">Home</a></li>
    <!-- then show my specific category, and up to 10 posts within it -->
        <li class="page_item page-item-2"><a href="http://mywebsite.com/category/tours-of-duty/" title="Tours of Duty">Tours of Duty</a>
            <ul>
                <li class="page_item page-item-3"><a href="http://www.mywebsite.com/category/tours-of-duty/" title="TOD 2009">TOD 2009</a></li>
            </ul>
        </li>
    <!-- wrap up the menu output -->
    </ul>

    Any pointers in the right direction would be really appreciated, as all my searching hasn't come to fruit.

    Regards,

  2. MichaelH
    Member
    Posted 5 years ago #

    This will do that, but modify to your needs:

    <?php
    //for category, show category title, then show 10 posts
    $cat_args=array(
      'include' => 4
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        $args=array(
          'showposts' => 10,
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);
          if ($posts) {
            echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
            foreach($posts as $post) {
              setup_postdata($post); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>
  3. Flocktome
    Member
    Posted 5 years ago #

    Thanks for the reply Michael. Sorry for my late thanks (moving house).

    That is perfect!

  4. Anydog
    Member
    Posted 4 years ago #

    Wow, that's nice.
    I'm trying to make something that I thought is something similar to this, but I didn't know how ...

    I have categories with sub-categories, and would like each root level item to make div containing its sub categories.

    DIV DIV
    ------------------------- --------------------------
    | Cat.level 0 item 1 || Cat.level 0 item 2 |
    | Cat.level 1 item 1 || Cat.level 1 item 1 |
    | Cat.level 1 item 2 || Cat.level 1 item 2 |
    | Cat.level 1 item 3 || Cat.level 1 item 3 |
    ------------------------- -------------------------- and so on ...
    Can it be done, and if so, hooow ... :(
    Many thanks in advance !! :)

    Bye

  5. Anydog
    Member
    Posted 4 years ago #

    Oh, this strips empty spaces ... Maybe if I put it in code tags:

    DIV                       DIV
    ------------------------- --------------------------
    | Cat.level 0 item 1     || Cat.level 0 item 2     |
    |    Cat.level 1 item 1  ||    Cat.level 1 item 1  |
    |    Cat.level 1 item 2  ||    Cat.level 1 item 2  |
    |    Cat.level 1 item 3  ||    Cat.level 1 item 3  |
    ------------------------- --------------------------

    Bye ...

Topic Closed

This topic has been closed to new replies.

About this Topic