WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add category post list in menu (HELP!) (14 posts)

  1. M@Design
    Member
    Posted 4 years ago #

    Hi
    Im trying to get a list of posts from a specific category.
    at the moment i only have the list of categories showing (see code below)

    <div class="box">
                 <ul>
                    <?php //wp_list_bookmarks(); ?>
                     <?php wp_list_categories('title_li=<h2>' . __('Categories') . '</h2>' ); ?>
                 </ul>
            </div>

    Can anyone please help, i just seem to be going in circles and pulling my hair out lol

    MANY THANKS :-)

  2. MichaelH
    Member
    Posted 4 years ago #

    <?php
    //get 3 posts from category id 7
    $args=array(
      'cat' => 7,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 3,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. vtxyzzy
    Member
    Posted 4 years ago #

    Perhaps try the List category posts plugin.

  4. M@Design
    Member
    Posted 4 years ago #

    Thanks MichaelH
    Trying not to sound stupid but where do i put this?
    i would guess at the first php line?
    <?php //wp_list_bookmarks(); ?>

    And sorry vtxyzzy this plugin does not suit what i want to use it for :) thanks for the help any way =)

  5. M@Design
    Member
    Posted 4 years ago #

    Scrap that ive just given it a go and didn't work :(

    here's the code ive got plus a probably better explanation:

    <div class="box">
                 <ul>
                    <?php //wp_list_bookmarks(); ?>
                     <?php wp_list_categories('title_li=<h2>' . __('Categories') . '</h2>' ); ?>
                 </ul>
            </div>
            <div class="box">
                 <ul>
                    <?php //wp_list_bookmarks(); ?>
                     <?php wp_list_categories('title_li=<h2>' . __('Categories') . '</h2>' ); ?>
                 </ul>
            </div>
            <div class="box">
                 <ul>
                    <?php //wp_list_bookmarks(); ?>
                     <?php wp_list_categories('title_li=<h2>' . __('Categories') . '</h2>' ); ?>
                 </ul>
            </div>

    im trying to display the lists of posts from a different category in each [box].
    (or to be completely honest i want to display posts from a different sub-category)

  6. MichaelH
    Member
    Posted 4 years ago #

    Yes you will want to use the code I displayed above for each of your 'boxes'. Of course you will change the 'cat'=> x value each time.

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

  7. M@Design
    Member
    Posted 4 years ago #

    OK im getting confused. . .
    doesn't take much lol

    ive tried adding the code you have given me wich works exactly the same as the code i already had???
    my guess on that issue is the problem im having with cat ID as i dont know the category ID's. so i tried using slugs instead and that didn't work, also having tried to change from displaying categories to sub catagories i well ended up with blank pages lol

    Please help :)
    wow i feel thick sometimes ;)

  8. MichaelH
    Member
    Posted 4 years ago #

  9. M@Design
    Member
    Posted 4 years ago #

    Hi! OK so got the cat number but how do i link it to sub cat?
    also due to code given it either displays nothing or if i add (see code below) to the end i get it all back but it displays all categories???

    <?php wp_list_categories('title_li=<h2>' . __('Categories') . '</h2>' ); ?>

    So starting again because even im confused now.
    here is my code for one box:

    <div class="box">
                 <ul>
                    <?php //wp_list_bookmarks(); ?>
                     <?php wp_list_categories('title_li=<h2>' . __('Categories') . '</h2>' ); ?>
                 </ul>
            </div>

    i want to display all posts in a sub catagory, the catagory ID (not sub cat ID) is 36

    Sorry im a pain and thank you for the fantastic help and advice so far :)

  10. MichaelH
    Member
    Posted 4 years ago #

    wp_list_categories lists just the categories, NOT the posts in those categories.

  11. M@Design
    Member
    Posted 4 years ago #

    OK so would you be able to help on code for listing posts in a specific sub category?

  12. MichaelH
    Member
    Posted 4 years ago #

    See my example above and use the sub-category ID for the 'cat' -> x argument.

    Also see query_posts() for more arguments.

    Note: typically you don't see posts listing in a header navigation.

  13. M@Design
    Member
    Posted 4 years ago #

    Ah, Got it!
    Thank you so much :)
    i realised that using your code had worked before but wasnt linking through to some of the css design and looked wrong when actually it was right :)

    Sorry for the hassle :)
    and thanks you so much you a saviour :-)

  14. jannmirch
    Member
    Posted 4 years ago #

    Thank you!! This is exactly what I've been looking for. Worked like a charm.

Topic Closed

This topic has been closed to new replies.

About this Topic