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 have a post that shows the titles and excerpts from a specific category underneath the content. Currently this is coded into a specific template like this:

<?php query_posts('category_name=dogs&showposts=5'); if (have_posts()) : ?>

This is fine but I need the client to be able to handle this themselves from a generic template so that I don't need to create a new one each time a category is created.

I want the client to create a new post as normal but then use a custom field to pull in posts from a specified category underneath.

For displaying the category name I would use something like this (assume we have a custom field name of "featured" and a value of "dogs":

<?php echo get_post_meta($post->ID, 'featured', true); ?>

So what I am guessing I need to do is replace the category_name=dogs part of my first example with something that pulls in the meta info.

And that is where my knowledge ends. I know I can post one into the other but I don't know how to resolve it.

Pleeeeeeeeeeeeeeease help!

thanks

James

ps using wordpress 3.2.1 and a custom theme

share|improve this question
add comment

2 Answers

Finally solved it! I needed to add this at the end of each loop :)

<?php $my_query = new WP_Query(array( 'category_name' => get_post_meta($post->ID, 'featured', true), 'posts_per_page' => 5, )); if ($my_query->have_posts()): ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?> content etc goes here
<?php endwhile; ?> <?php endif; ?><?php wp_reset_query(); ?> 

many thanks

James

share|improve this answer
add comment

Try to avoid query_posts:

$my_query = new WP_Query(array(
  'category_name'  => get_post_meta($post->ID, 'featured', true),
  'posts_per_page' => 5,
));

if ($my_query->have_posts()){

   while ($my_query->have_posts()){

     $my_query->the_post();

     the_title();
     the_excerpt(); 
   }

 }
share|improve this answer
    
ok I had a look through the codex and worked out what i needed and ended up with the following. The downside is that while it is now displaying posts and there is the correct number its just pulling up the 5 latest and not ones specifically form that category. so I am a bit puzzled! <?php $my_query = new WP_Query(array( 'category_name' => get_post_meta($post->ID, 'featured', true), 'posts_per_page' => 5, )); if ($my_query->have_posts()): ?> <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> content etc goes here <?php endwhile; ?> <?php endif; ?> –  James Olney Sep 21 '11 at 15:18
    
Maybe the requested category name doesn't match the real one? Try capitalizing it with ucfirst(get_post_meta($post->ID, 'featured', true)) –  onetrickpony Sep 21 '11 at 15:24
    
It turned out that a bit further up the page I was using query_posts still and that was interfering (basically there is the article and then one excerpt from one cat and then 5 excerpts from another cat). But I have found that I can only show the 1 excerpt or the 5 excerpts so i guess the two blocks of code are interfering with each other as the second block always seems to show the latest posts (hope that makes sense) –  James Olney Sep 21 '11 at 15:33
    
Don't use query_posts at all, because it messes up some global variables. Use WP_Query instead; it does the same thing, but without the side effects –  onetrickpony Sep 21 '11 at 21:20
    
Yeah i have done away with it now. But my two my_query bits of code seem to interfere with each other. any idea how i get round that? thanks for your help! –  James Olney Sep 22 '11 at 7:48
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.