• Resolved jowe

    (@jowe)


    Short version: I need a way to say (in single.php) “if (tag=breaking)”. I can take it from there.

    Long version: I’m working on a newspaper website for my college. I’m really excited about and I’m sure this won’t be my first post in the support forums. What my issue is right now is this: I don’t want to display the time something was published (since we’re a weekly, there’s no sense in putting the time). I still want the date, of course, and I know how to code that easily. What I want to do is put a time (XX:XX XM) if a post has the tag, “breaking.” Meaning, for breaking stories, I want the time the post was last updated. I’m using a plugin to show the post updated, but I can’t figure out how to code single.php to display the post_updated when its a breaking story, and just the regular date when it’s not.

    I found the “is_tag()” conditional tag, but then I realized that’s for if the post is being displayed on the tags own archive page.

    FYI: Here’s the code I’m using right now:

    <span class="time">
    <?php
    ***if-is-tag=breaking*** {
    echo "This breaking story was last updated on";
    post_updated('F jS, Y \at h:i:s A', 0, '', '');
    } else {
    the_time('F jS, Y');
    } ?>
    </span>

    Obviously, that *’ed part is what I’m asking for.

    Thanks for all the help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter jowe

    (@jowe)

    Alright, I did some snooping around and I think I’m getting closer.

    I think I should be using get_the_tags(). But the thing is, it’s still not working. I then tried to echo what get_the_tags() was giving me (for a post that had a tag) and it printed “Array.” The tag is called breaking. Here’s my updated code (including my test to echo what get_the_tags() is spitting out).

    <?php // If the post is breaking news, insert the last time it was updated
    $posttags = get_the_tags();
    echo "$posttags"; // what the heck is get_the_tags printing??
    if($posttags == 'breaking') { // if tag is 'breaking'
    echo "This breaking story was last updated on";
    post_updated('F jS, Y \at h:i:s A', 0, '', '');
    } else { // if not a breaking story, just print the date
    the_time('F jS, Y');
    } ?>

    Any help would be greatly appreciated! Thanks.

    It would be simpler if you just made a ‘breaking’ category instead of a tag (here it is as category ID 44):

    <?php if (in_category(44)) { ?>
    <?php echo "This breaking story was last updated on";
    post_updated('F jS, Y \at h:i:s A', 0, '', ''); ?>
    <?php } else { ?>
    <?php the_time('F jS, Y') ?>
    <?php } ?>

    Here is a page with more info:

    https://codex.www.ads-software.com/Template_Tags/in_category

    Thread Starter jowe

    (@jowe)

    I saw that in the codex last night… and I’m reconsidering. I was just hoping to not have to deal with anything other than top-level categories (eg. News, Sports, Features, etc.).

    I hacked around some more last night and left it with if($posttags == Array) since that’s what get_the_tags() was printing for that post. Currently, I don’t have any other posts with tags, so that worked fine. But I know there has to be a way to do what I want.

    I was thinking of possibly using Custom Fields. I know I will end up having some of those anyway (for Issue/Volume information), so I’m gonna have to deal with it eventually.

    Thanks

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    I made a patch to do this sort of thing but it hasn’t been added to the core code yet: https://trac.www.ads-software.com/ticket/6590

    You can do this by adding this to your theme’s functions.php file:

    function has_tag($tag = '') {
    	global $post;
    	$taxonomy = "post_tag";
    
    	if ( !in_the_loop() ) return false; // in-the-loop function
    
    	$post_id = (int) $post->ID;
    
    	$terms = get_object_term_cache($post_id, $taxonomy);
    	if (empty($terms))
    	       	$terms = wp_get_object_terms($post_id, $taxonomy);
    	if (empty($terms)) return false;
    
    	if (empty($tag)) return (!empty($terms));
    
    	$tag = (array) $tag;
    	foreach($terms as $term) {
    		if ( in_array( $term->term_id, $tag ) )
    			return true;
    		elseif ( in_array( $term->name, $tag ) )
    			return true;
    		elseif ( in_array( $term->slug, $tag ) )
    			return true;
    	}
    
    	return false;
    }

    Then just use it, in the loop, like this:

    if (has_tag('breaking')) {
    // whatever...
    }

    Hopefully this will get included in the core soon.

    Thread Starter jowe

    (@jowe)

    Yes! Thank you so much! I knew there was a way to define these sorts of things but I didn’t know how! functions.php! Of course.

    And I agree- I’d like to see it in the core… I think if we’re gonna have in_category(), we should have an equivalent for tags.

    Again, thanks so much Otto.

    Can I do this same sort of thing for attachments? I want to use
    ‘echo wp_get_attachment_link’ to put image attachments on my home page,

    but only for certain images. Would be nice if I could just add tags to the images that I want to use.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Conditional tag for if a post has a certain tag’ is closed to new replies.