• Paul_NGC

    (@paul_ngc)


    After a search I found this post that came close, but unfortunately that’s not what I’m looking for. http://ww.wp.xz.cn/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

    (@alchymyth)

    can you paste the full code of your template into a http://pastebin.com/ and post the link to it here?

    as it is, you code snippet does not contain the loop:
    http://codex.ww.wp.xz.cn/The_Loop_in_Action
    http://codex.ww.wp.xz.cn/The_Loop

    Thread Starter Paul_NGC

    (@paul_ngc)

    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

    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

    (@paul_ngc)

    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

    (@alchymyth)

    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.ww.wp.xz.cn/Class_Reference/WP_Query#Category_Parameters

    Thread Starter Paul_NGC

    (@paul_ngc)

    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

    (@alchymyth)

    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

    (@paul_ngc)

    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.