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
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…
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; ?>
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
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.
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>
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 π