I have a working funtion:

<?php query_posts('category_name=category');?>

I want to make the category name dynamic to reflect the page title. Something like this:

<?php query_posts('category_name='.the_title());?>

But this doesnt work, & nothing I do seems to have the desired effect.

  • So you have created page with the same text used for category, and you want to query posts by the page title as category name ? if that's the question, the your approach is correct. You just need to change 'category_name='.the_title() to 'category_name='. get_the_title(). Also as you are making query from a Page, you need to pass the post type, so it would be 'category_name='. get_the_title() . '&post_type=post'. – Shazzad Feb 9 '14 at 16:33
  • yes get_the_title instead of the_title - works. If you make an answer I will accept. Thanks – Jon Feb 9 '14 at 16:47
  • Please do not use query_posts. – s_ha_dum Feb 9 '14 at 19:27
  • Ok but it would have been a bit easier with code! <?php $the_query = new WP_Query('category_name='. get_the_title()); if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php endwhile; wp_reset_postdata(); endif; ?> – Jon Feb 10 '14 at 13:04

Use get_the_title() replacing the_title(). Difference is get_the_title() returns the value while the_title() echos it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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