• I’m looking to learn how to create a page that acts as a template for tags. Ideally, the page template would do the following:

    1. Allow the user to create a Page, select the “Tag” page template.
    2. The user would then enter a photo of the tag subject (Dogs, for instance.)
    3. The user then enters some introductory text and saves the page.
    4. The template formats the page so that the image supplied is left-formatted and the introductory text is displayed.
    5. Below the introductory text, headlines of all Posts tagged with ‘Dog’ are displayed.

    How can this be accomplished? Thanks much.

Viewing 14 replies - 1 through 14 (of 14 total)
  • You’ll need to access your site via ftp, and navigate to your active theme. Copy your index.php file out of the active theme folder, and replace all of the text with this (didn’t get a chance to test but ought to work):

    <?php
    /*
    Template Name: Tag Page
     */
    get_header();
    ?>
    
    <?php echo "<img src=\"".get_post_meta($id, 'Tag Image', true)."\">"; ? >
    
    <?php
    $myposts = get_posts('numberposts=-1');
    foreach($myposts as $post) :
    $blockit = "no";
    $posttags = get_the_tags();
    if ($posttags) {
      foreach($posttags as $tag) {
        if($tag->name=="x") {
    $blockit = "yes";
    }
    }
    }
    ?>
    <?php if($blockit=="no") { ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php }; ?>
    <?php endforeach; ?>
    </ul>
    <?php endwhile;
    get_footer();?>

    Now, have the user go in to create a page and choose the “tag page” template on the right.

    Make the desired tag the page’s title, put in the introductory text as the content, and then, I think your best bet for the picture is to use a custom field. Scroll down until you see them (if you haven’t used them previously, you might have to go to “screen options” at the top of the page, and make sure the custom fields box is checked. Once they’re visible, find them, click ‘enter new’ and type in ‘Tag Image’. Then, in the ‘value’ field to the right, enter the full url of the image you want to use for that tag.

    Hope this gets you started in the right direction!

    Thread Starter ithacaindy

    (@ithacaindy)

    Thanks. The tag page is created, but not the tag. The tag page’s introductory text displays, but not a list of the posts matching the tag.

    Just to be sure–Do you mean that you already had a tag page other than what I suggested? Or that you implemented what I suggested and from that you got the introductory text, but not a list of posts matching the tag?

    If it’s the latter, there’s an oversight in the code I provided:

    The line:

    if($tag->name=="x") {

    is looking for posts that have a tag of “x”. Replace that line with:

    if($tag->name==get_the_title()) {

    which should check if there’s a tag that matches the title of the page you’ve created.

    Hope that makes sense!

    Thread Starter ithacaindy

    (@ithacaindy)

    I meant the later. I’ll give the edit a try and let you know. Thanks, again.

    Thread Starter ithacaindy

    (@ithacaindy)

    I just get a blank white screen after creating a page, setting the template, then trying to view the page.

    Sorry–finally did what I should have done in the first place and tested it out, and the code was full of bugs. This now is tried and true:

    <?php
    /*
    Template Name: Tag Page
     */
    get_header();
    ?>
    <?php $tagpage = get_the_title(); ?>
    <?php echo "<img src=\"".get_post_meta($post->ID, 'Tag Image', true)."\">"; ?>
    
    <?php
    $myposts = get_posts('numberposts=-1');
    foreach($myposts as $post) :
    $showit = "no";
    $posttags = get_the_tags();
    if ($posttags) {
      foreach($posttags as $tag) {
     if(strtolower($tag->name)==strtolower($tagpage)) {
    $showit = "yes";
    }
    }
    }
    ?>
    <?php if($showit=="yes") { ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php }; ?>
    <?php endforeach;
    get_footer();?>

    Let me know if that does it

    Thread Starter ithacaindy

    (@ithacaindy)

    99% there. The matching post titles display, the image displays – but the intro text does not display.

    Thread Starter ithacaindy

    (@ithacaindy)

    I simply added <?php the_content(); ?> up front and it displays the intro text. All that’s needed now is to add some styling and convert it for the Genesis framework.

    Ah, so between:

    <?php echo "<img src=\"".get_post_meta($post->ID, 'Tag Image', true)."\">"; ?>

    and

    <?php
    $myposts = get_posts('numberposts=-1');

    insert:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <?php the_content(); ?>
    
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    Hi guys, i’m trying to do the same thing as ithacaindy but i have to add the content of the posts too. Is that possible??

    thanks a lot

    Absolutely–it’s just a matter of calling the contents. So in the code a couple of posts back above, right after:

    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

    you should just need to add:

    <?php the_content(); ?>

    Hi Dandy! Thanks for the help. I tried that but it shows whatever content I added to the page.

    If you want to paste your code, I’ll be happy to take a look. In the code I pasted before, there are two loops:

    One that starts with:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    and ends with:

    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    Anything that goes inside this loop will be calling the content you added to that specific page.

    The other loop, which begins:

    <?php
    $myposts = get_posts('numberposts=-1');
    foreach($myposts as $post) :

    and ends:

    <?php endforeach;

    is the one that’s calling all of your tags. So if you want the content for the tagged items, the code needs to be in there somewhere.

    Hey Dandy,

    this is pretty much what i have and it keeps showing the content inside my page. not sure what i am doing wrong.

    <div class="main span8" role="main">
    	<?php $tagpage = get_the_title(); ?>
    	<?php
    		$myposts = get_posts('numberposts=-1');
    		foreach($myposts as $post) :
    		$showit = "no";
    		$posttags = get_the_tags();
    		if ($posttags) {
    			foreach($posttags as $tag) {
    				if(strtolower($tag->name)==strtolower($tagpage)) {
    					$showit = "yes";
    				}
    			}
    		}
    	?>
    	<?php if($showit=="yes") { ?>
    		<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    		<?php the_excerpt(); ?>
    	<?php }; ?>
    	<?php endforeach; ?>
    
    </div>
Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘How to Create a Custom Tag Template Page?’ is closed to new replies.