Title: static code for multiple lists with posts dif. cats
Last modified: August 20, 2016

---

# static code for multiple lists with posts dif. cats

 *  [Paul_NGC](https://wordpress.org/support/users/paul_ngc/)
 * (@paul_ngc)
 * [15 years ago](https://wordpress.org/support/topic/static-code-for-multiple-lists-with-posts-dif-cats/)
 * After a search I found this post that came close, but unfortunately that’s not
   what I’m looking for. [http://wordpress.org/support/topic/post-list-by-category?replies=2](http://wordpress.org/support/topic/post-list-by-category?replies=2)
 * What I want to display is a standard menu that shows 4 cats. within there the
   posts.
 * cat 1
    – post – post – post
 * cat 2
    – post – post
 * cat 3
    – post – post – post
 * cat 4
    – post – post – post – post
 * ——–
 * So I tried it with this code. I exclude all the others. But it doesn’t do anything.
   Is it because I use this code 4 times ( with different cats excluded )?
 *     ```
       <ul>
       <li>
       <?php
       if ( is_home() ) {
       	query_posts( 'cat=-1,-2,-3' );
       }
       ?>
       </li>
       </ul>
       ```
   

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years ago](https://wordpress.org/support/topic/static-code-for-multiple-lists-with-posts-dif-cats/#post-2094142)
 * can you paste the full code of your template into a [http://pastebin.com/](http://pastebin.com/)
   and post the link to it here?
 * as it is, you code snippet does not contain the loop:
    [http://codex.wordpress.org/The_Loop_in_Action](http://codex.wordpress.org/The_Loop_in_Action)
   [http://codex.wordpress.org/The_Loop](http://codex.wordpress.org/The_Loop)
 *  Thread Starter [Paul_NGC](https://wordpress.org/support/users/paul_ngc/)
 * (@paul_ngc)
 * [15 years ago](https://wordpress.org/support/topic/static-code-for-multiple-lists-with-posts-dif-cats/#post-2094235)
 * Hello Alchymyth
 * Thanks for the reply. This is the code. Footer isn’t included because theres 
   nothing in there. The part where it says <?php get_header(); ?> is where index.
   php starts ( so in this code example you have to ignore it 😉
 * [http://pastebin.com/duDtaA5Z](http://pastebin.com/duDtaA5Z)
 * In the header.php part you find 4 times <ul class=”submenu”>
    In there I want
   to display a list with all posts that have a specific category. Should be simple,
   but I can’t get it to work…
 *  Thread Starter [Paul_NGC](https://wordpress.org/support/users/paul_ngc/)
 * (@paul_ngc)
 * [15 years ago](https://wordpress.org/support/topic/static-code-for-multiple-lists-with-posts-dif-cats/#post-2094356)
 * At the moment I’m not home, but when I read the codex about The Loop in Action
   I get the idea I have to wrap every php code in a loop like this?
 *     ```
       <?php if (have_posts()) : ?>
          <?php while (have_posts()) : the_post(); ?>
            <div class="column">
               do something...
            </div>
          <?php endwhile; ?>
       ```
   
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years ago](https://wordpress.org/support/topic/static-code-for-multiple-lists-with-posts-dif-cats/#post-2094359)
 * take this section for example:
 *     ```
       <div class="category">cat1</div>
                 <ul class="submenu">
                   <?php
                    if ( is_home() ) {
                        query_posts( 'cat=1' );
                    }
                  ?>
                 </ul>
       ```
   
 * first:
    for each section, you need to close the div after the `</ul>` second:
   for the post list, you could use `get_posts()`:
 * example of a changed section:
 *     ```
       <div class="category">cat1</div>
                 <ul class="submenu">
                   <?php
                    if ( is_home() ) {
   
       $cat1_posts = get_posts(array('numberposts' => -1, 'category__in' => array(1) ) );
       if($cat1_posts) foreach( $cat1_posts as $post ) : ?>
       <li><a href="<?php echo get_permalink($post->ID); ?>"><?php echo get_the_title($post->ID); ?></a></li>
       <?php endforeach; ?>
   
                    }
                  ?>
                 </ul>
       </div>
       ```
   
 * (why is the `if ( is_home() ) {` in the code? is this supposed to happen only
   on the blog page?)
 * for the difference of `cat=1` and `category__in => array(1)` see:
    [http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters](http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters)
 *  Thread Starter [Paul_NGC](https://wordpress.org/support/users/paul_ngc/)
 * (@paul_ngc)
 * [15 years ago](https://wordpress.org/support/topic/static-code-for-multiple-lists-with-posts-dif-cats/#post-2094379)
 * I’ve tried your code, but it breaks the site. Also without the `if ( is_home()){`
 * But I’ve found something else that didn’t break the site:
 *     ```
       <ul class="submenu">
            <?php wp_list_pages('title_li=&depth=2&sort_column=menu_order'); ?>
       </ul>
       ```
   
 * But this one displays the pages. I need posts based on categories.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years ago](https://wordpress.org/support/topic/static-code-for-multiple-lists-with-posts-dif-cats/#post-2094387)
 * > I’ve tried your code, but it breaks the site
 * there was a php tag missing 🙁
 * cleaned-up version:
 *     ```
       <div class="category">cat1</div>
                 <ul class="submenu">
                   <?php
                    $cat1_posts = get_posts(array('numberposts' => -1, 'category__in' => array(1) ) );
       if($cat1_posts) foreach( $cat1_posts as $post ) : ?>
       <li><a href="<?php echo get_permalink($post->ID); ?>"><?php echo get_the_title($post->ID); ?></a></li>
       <?php endforeach; ?>
   
                  </ul>
       </div>
       ```
   
 *  Thread Starter [Paul_NGC](https://wordpress.org/support/users/paul_ngc/)
 * (@paul_ngc)
 * [15 years ago](https://wordpress.org/support/topic/static-code-for-multiple-lists-with-posts-dif-cats/#post-2094390)
 * Hey alchymyth,
 * Thanks for the code. But I now use a different code that I’ve found at css-tricks.
   com
 * This is the code that works perfect for me:
 *     ```
       <ul class="submenu">
       			<?php
       				$lastposts = get_posts('numberposts=0&order=ASC&cat=3');
       				foreach($lastposts as $post) :
       				setup_postdata($post); ?>
   
       				<li<?php if ( $post->ID == $wp_query->post->ID )  ?>>
       					<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
       				</li>
       			<?php endforeach; ?>
                 </ul>
       ```
   
 * But save the snippet because it looks good 🙂

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘static code for multiple lists with posts dif. cats’ is closed to new
replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 2 participants
 * Last reply from: [Paul_NGC](https://wordpress.org/support/users/paul_ngc/)
 * Last activity: [15 years ago](https://wordpress.org/support/topic/static-code-for-multiple-lists-with-posts-dif-cats/#post-2094390)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
