• Hello Everyone,

    I have run into a problem with wordpress that I can’t figure out. What I would like to do is create a page template that displays posts with a certain tag based on the name of the page. So lets say that my page name is testpage (www.wordpress.com/testpage), I would like this page to only display posts that have the tag “testpage”.

    The idea here is too only have to create one page template instead of 1 for each page that I want to show different posts on.

    Can anyone give me a hand, I would greatly appreciate it.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Why not just create a tag template?

    Perhaps I misunderstand what you want, so this may not apply. What I think you want to do is create a template that can be assigned to several different pages, but have each page show only the posts that have a tag the same as the page title.

    If that is correct, you could use something like this in the template:

    <?php $tag = wp_title('',false);
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("paged=$paged&tag=$tag);
    // rest of loop
    ?>
    Thread Starter erikw46

    (@erikw46)

    Thanks for your help!

    You are correct about what I am trying to achieve. However I tried getting that code you supplied to work and was having no luck. I believe (but I could be wrong) that in the last set if parenthesis you are missing a quotation mark however even after fixing that I was still having no luck. All I get is the title of the page displaying instead of the posts that have a tag which is the same title as the page.

    You are correct about the missing quote mark – sorry about that. You need to replace the line ‘// rest of the loop’ with all of the code you need to display what you want to see. It depends on the theme, but it should look something like this:

    if (have_posts()) : ?>
          <?php while (have_posts()) : the_post(); ?>
    
             <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
                <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                <small><?php the_time('F jS, Y') ?> by <?php the_author() ?></small>
                <div class="entry">
                   <?php the_content('Read the rest of this entry &raquo;'); ?>
                </div>
                <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    
                </div>
    
          <?php endwhile; ?> 
    
          <div class="navigation">
             <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
             <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
          </div>
    
       <?php else : ?>
    
          <h2 class="center">Not Found</h2>
          <p class="center">Sorry, but you are looking for something that isn't here.</p>
          <?php get_search_form(); ?>
    
       <?php endif; ?>

    If your code already looks something like that, post it in the pastebin so I can take a look.

    Thread Starter erikw46

    (@erikw46)

    I really appreciate you helping me out with this. Still no luck, I am probably just doing some stupid mistake with the code. I have been teaching wordpress to myself so there are some gaps in my knowledge.

    Here is the link to the pastebin: https://wordpress.pastebin.com/h1Wx8Rn2

    Thank you so much.

    A couple of changes should make it work. First, wp_title is putting spaces in front of the title even though you gave an empty string. So we have to remove them later. Change this:

    $tag = wp_title('',false);

    to this:

    $tag = preg_replace('/^ +/','',wp_title('',false));

    Next, the query_posts arguments need to be in double quotes, like this:

    query_posts("paged=$paged&tag=$tag");

    Give that a try.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Show only posts who’s tags match page name’ is closed to new replies.