WordPress.org

Ready to get started?Download WordPress

Forums

Get posts based on page title? (9 posts)

  1. Raid33
    Member
    Posted 6 years ago #

    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?

  2. AntonioP
    Member
    Posted 6 years ago #

    I'm trying to do the same exact thing. Let me know when you get this working by email me @ antonio_b_perez@hotmail.com

    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).

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

    http://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.

  3. adamrice
    Member
    Posted 6 years ago #

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

  4. Raid33
    Member
    Posted 6 years ago #

    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();?>
  5. chaoskaizer
    Member
    Posted 6 years ago #

    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
  6. Jeff
    Member
    Posted 6 years ago #

    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.

  7. monkeyfight
    Member
    Posted 6 years ago #

    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);
    ?>
  8. wescrock
    Member
    Posted 5 years ago #

    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>
  9. johnh10
    Member
    Posted 5 years ago #

    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); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags