• I’ve created a page template for a blog that will use that template for over 100 pages. I have the template pulling information based on page title, ie: <?php the_title(); ?>.jpg

    I would like the template to display the latest posts from a category of the same name as the page. So, if the page was named “Kittens” I would like it to display stories from the “Kittens” category. Here is the code I’m trying:

    <h3>Recent Stories</h3>
    
    <?php $recent = new WP_Query("cat=<?php the_title(); ?>&showposts=2"); while($recent->have_posts()) : $recent->the_post();?>

    WP doesn’t appear to be recognizing the code after “cat=” Does anyone know of a way to do this?

Viewing 8 replies - 1 through 8 (of 8 total)
  • I’m trying to do the same exact thing. Let me know when you get this working by email me @ [email protected]

    This might be of help (its a plugin) – you might also need a plugin called something like category inheritor – think its mentioned on the page below. My understanding with this plugin is that after you activate the plugin you have to name the template something like cat-#.php (where # is the category you want to display).

    https://guff.szub.net/2005/07/21/post-templates-by-category/

    https://mattmedia.net/2007/10/09/how-to-set-up-custom-wordpress-category-templates-in-four-easy-steps/

    I’ve been trying to do this for a couple of days.

    You are nesting php tags.
    <?php recent … <?php the_title… ?> … ?>
    Try removing the inner php tags.

    Thread Starter Jeff Milone

    (@raid33)

    Nice catch adamrice. That makes sense, but it tried it, and still no luck. This is what I’m using now:

    <h3>Recent Stories</h3>
    
    <?php $recent = new WP_Query("category_name=the_title()&showposts=2"); while($recent->have_posts()) : $recent->the_post();?>

    not sure if it work but you can try get the title with the following methods

    $title = sanitize_title_with_dashes(wp_title('','false));

    or

    $title = explode(" ",(wp_title('','false));
    $title = $title[0]; // grab the first words only

    I’ve made the following work in the past:

    <?php $page_cat = $post->post_title; //copy the page title ?>
    <?php $page_cat = strtolower($page_cat); // make it lower case only?>
    
    <?php $my_query = new WP_Query('category_name='.$page_cat); ?>
    
    	<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    stuff here
    
    	<?php endwhile; ?>

    Hope it helps.

    upekshapriya, you couldn’t have posted that at a better time – it works perfectly ??

    <?php
    global $more;
    $more = 0;
    
    $page_cat = $post->post_title; //copy the page title
    $page_cat = strtolower($page_cat); // make it lower case only
    $my_query = new WP_Query('category_name='.$page_cat);
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('category_name='.$page_cat.'&showposts=5&paged='.$paged);
    ?>

    Hey there, I am so glad I found this, but i cant seem to get it to display the results… below is my code:

    <div id="video_archive">
    	<h4>Featured Videos</h4>
    		<?php $page_cat = $post->post_title; //copy the page title ?>
    		<?php $page_cat = strtolower($page_cat); // make it lower case only?>
    			<?php $my_query = new WP_Query('category_name='.$page_cat); ?>
    			<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    		<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    	<?php endwhile; ?><li> <?php echo $page_cat ?></li>
    </div>

    I’m trying to do the same thing, only displaying posts who have a tag that match the page name. Seems to work fine if it’s a one word tag, but not multiple word tags.

    <?php $tag = $post->post_title; ?>
    $tag_query = new WP_Query('tag='.$tag);
    <?php if ($tag_query->have_posts()) : while ($tag_query->have_posts()) : $tag_query->the_post(); ?>
      stuff here
    <?php endwhile; endif; ?>

    anyone see something I’m missing?

    Of course after I post the missing link is
    <?php $tag = sanitize_title($post->post_title); ?>

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Get posts based on page title?’ is closed to new replies.