Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to set up my template so that Pages show a list of posts where the category slug matches the page slug. So on the Page "About Us" ('about'), posts tagged with the category-slug 'about'. The sidebar should show a list of these posts (I need to hardcode this. NO PLUGINS). Index.php should show all posts, EXcluding the posts that show up on a page.

This is the custom loop generates a list of recent posts that I will be adding to sidebar.php. This needs to be modified so that it only shows posts in a category that matches the page.

<ul>
<?php
query_posts( 'posts_per_page=5' );
if ( have_posts() ) :
      while ( have_posts() ) : the_post();
    ?><li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li><?php
endwhile;
endif;
wp_reset_query();
?>
</ul>

I tried to explain as clearly as possible, but it's a little confusing, so please me know what I need to make more clear.

share|improve this question
add comment

3 Answers

If this is going in a page.php (or similar) template, then using query_posts() is a bad idea and could have some pretty bad consequences. I also prefer WP_Query over get_posts() as it easily allows you to use template tags and it explicitly exists for running secondary loops on a page.

<?php
global $post;
$my_query_args = array(
    'posts_per_page' => 5, // change this to any number or '0' for all
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => $post->post_name // this gets the page slug
        )
    )
);
// a new instance of the WP_query class   
$my_query = new WP_Query( $my_query_args );

if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post(); ?>

    <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>

<?php endwhile; endif; wp_reset_postdata(); ?>
share|improve this answer
add comment

I'm assuming that your page slug is the same as the category slug? Anyway here is the code. It should be self explanatory.

<?php if(is_page()) : // check we are a page ?>
        <?php
        global $post;
        $cposts = get_posts("numberposts=5&category_name={$post->post_name}");
        if($cposts) : ?>
            <ul>
                <?php foreach($cposts as $cpost) : ?>
                    <li><a href="<?php echo get_permalink($cpost->ID) ?>"><?php echo $cpost->post_title; ?></a></li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
<?php endif; ?>
share|improve this answer
add comment

This is what worked for me, quite simpel:

<h1><?php single_cat_title(); ?></h1>
share|improve this answer
2  
Please explain how this solves the problem. –  s_ha_dum Apr 4 '13 at 17:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.