WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Want to display different single page layouts based on post tag? (4 posts)

  1. shagen
    Member
    Posted 3 years ago #

    Have searched the forums but cannot quite find what I am looking for. I am trying to display various page layouts for my single.php file based on if a post is tagged with a certain tag or not. Have tried using is_tag() and an if statement at the top of my single.php template and it does not seem to honor it. So therefore I know I am off teh mark. Here is what I had tried...

    <?php if (is_tag('resource')) { ?>
    		Display resource loop here and layout here.
    		<?php }
    		else { ?>
    		Display all others here.
    		<?php
    	}; ?>

    The page before this contains a loop of various posts. Do I need to do something to special to the link on that page before clicking into a single post? Feels like it has no clue what the post is tagged with.

    Also sometimes the post could be tagged with this as well as others items. I am only interested when the resource tag shows up no matter how many other tags it has.

    I considered using tag-slug.php and creating a template but that did not seem like it was for the single post layout but is more designed for a loop of multiple post that have a certain tag.

    Any direction and help is much appreciated. Thanks for the time!

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    http://codex.wordpress.org/Conditional_Tags#A_Tag_Page

    is_tag() targets the tag archive;

    for use in single posts, i would try
    has_tag()

  3. shagen
    Member
    Posted 3 years ago #

    Thank you thank you! Stared at that page for a while and was so stuck on is_tag() that I did not read the others close enough. Still getting the hang of all the syntax so your reply and time is greatly appreciated. has_tag() is working great. Here is what it looks like in case others need the exact code...

    <?php if (has_tag('resource')) { ?>
    		Display resource loop here and layout here.
    		<?php }
    		else { ?>
    		Display all others here.
    		<?php
    	}; ?>
  4. meganvwalker
    Member
    Posted 2 years ago #

    Thank you, thank you, thank you! This is exactly what I needed. I know it was over 9 months ago, but wanted to post my gratitude!

Topic Closed

This topic has been closed to new replies.

About this Topic