Forums

formatting based on tag slug (11 posts)

  1. MarjWyatt
    Member
    Posted 9 months ago #

    I've been all over Codex looking for a code sample to do what I want, which is to read the post tags and, if the tag slug is equal to something, do a different format and continue processing the loop.

    Once I got the format to work with the basic code that read the tag slug, I began crafting code for circumstances where there would be more than one tag, as is often the case. That is when my joy ended. :)

    I have an image with my comments here:
    http://mildlymystified.com/toomanytags/too-many-fav-images.png

    Here is the code that is producing top half of image, without the comments, of course. The problem is in the foreach loop, I believe.

    <div class="thumbnail">
        <a href="<?php the_permalink(); ?>">
        <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
        <span class="overlay"></span>
        </a>
        <div class="category"><?php the_category(); ?></div>
        <span class="month"><?php echo get_post_meta( $post->ID, '_ct_text_4e3318cbab83d', true ); ?>
        <?php foreach (get_the_tags('slug') as $tag) {
            if ($tag = 'fav') { ?>
                <span class="fav"><img src="<?php bloginfo(url)?>/wp-content/themes/TheStyle/images/fav-icon.png" /></span>
        <?php } elseif (!($tag = 'fav')) { ?>
                <span class="fav"><img src="<?php bloginfo(url)?>/wp-content/themes/TheStyle/images/no-fav-icon.png" /></span>
        <?php }; ?>
        </span><!--</span>-->
        <?php }; ?>
    </div> <!-- end .thumbnail -->

    Ultimately, the goal is to not have an image if the tag slug is not equal to "fav." If ANYONE has any insights as to how I can tweak this code to accomplish this, it would be most helpful!

    Thanks in advance.

  2. alchymyth
    The Sweeper
    Posted 9 months ago #

    not sure about this:
    get_the_tags('slug')
    the only possible parameter for get_the_tags() would be the post id, which is not needed if the code is used in the loop:
    http://codex.wordpress.org/Function_Reference/get_the_tags

    also, use:

    if ($tag == 'fav') { ?>

    and:

    (!($tag == 'fav'))

    http://php.net/manual/en/language.operators.comparison.php

  3. MarjWyatt
    Member
    Posted 9 months ago #

    Thanks for the tips, alcymyth. Sadly, the result was that the image was replaced with the elseif image for both tags, which is not what I want.

    Do you know how I can stop processing the foreach loop after the code has encountered and processed the tag == 'fav' condition?

  4. alchymyth
    The Sweeper
    Posted 9 months ago #

    i should have checked more in depth; i missed that $tag is not actual the tag slug;
    this is more or less also explained in the link i posted: http://codex.wordpress.org/Function_Reference/get_the_tags

    try to edit this section to:

    <?php foreach (get_the_tags() as $tag) {
            if ($tag->slug == 'fav') { ?>
                <span class="fav"><img src="<?php bloginfo(url)?>/wp-content/themes/TheStyle/images/fav-icon.png" /></span>
        <?php } elseif (!($tag->slug == 'fav')) { ?>
  5. MarjWyatt
    Member
    Posted 9 months ago #

    Thanks Alchymyth!

    I am not thrilled with the idea of having an empty image to fill in the blank when the tag is not fav but doing it this way alleviates the need to work out how to stop processing the foreach loop after the code finds slug == 'fav' condition. I tried adding more tags to a post and removing the fav tag and all is well now.

    I guess I'll let the next developer, who takes on the project after I retire on my yacht scoff at the kludge. :)

    You're awesome! You and so many other members at Codex make things much easier for all of us out here in WordPress land.

  6. alchymyth
    The Sweeper
    Posted 9 months ago #

    there is no need to have the elseif section; have you tried to replace this section:

    <?php foreach (get_the_tags() as $tag) {
            if ($tag->slug == 'fav') { ?>
                <span class="fav"><img src="<?php bloginfo(url)?>/wp-content/themes/TheStyle/images/fav-icon.png" /></span>
        <?php } elseif (!($tag->slug == 'fav')) { ?>
                <span class="fav"><img src="<?php bloginfo(url)?>/wp-content/themes/TheStyle/images/no-fav-icon.png" /></span>
        <?php }; ?>
        </span><!--</span>-->
        <?php }; ?>

    with:

    <?php foreach (get_the_tags() as $tag) {
            if ($tag->slug == 'fav') { ?>
                <span class="fav"><img src="<?php bloginfo(url)?>/wp-content/themes/TheStyle/images/fav-icon.png" /></span>
        <?php } //end of if ?>
        <?php }; //end of foreach ?>
    </span>

    (also corrected for the wrong position of the </span> )

  7. MarjWyatt
    Member
    Posted 9 months ago #

    Hi again, Alchymyth.

    It seemed that I had tried your code correction to end the if before but I guess I hadn't. Once it worked properly with your help yesterday, I didn't want to break it again. :)

    I have adapted your end php segments to the code and the kludge is gone! The closing span was correct as there is a span class above called month so I left that part out.

    Because you took an interest, and to help anyone else who may have a similar need down the road, here is the final (and working) code snippet:

    <div class="thumbnail">
      <a href="<?php the_permalink(); ?>">
      <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
      <span class="overlay"></span></a>
      <div class="category"><?php the_category(); ?></div>
      <span class="month"><?php echo get_post_meta( $post->ID, '_ct_text_4e3318cbab83d', true ); ?>
      <?php foreach (get_the_tags() as $tag) {
        if ($tag->slug == 'fav') { ?>
          <span class="fav"><img src="<?php bloginfo(url)?>/wp-content/themes/TheStyle/images/fav-icon.png" /></span>
        <?php } //end of if ?>
      </span><!-- end month </span>-->
      <?php }; //end of foreach ?>
    </div> <!-- end .thumbnail -->

    Thanks again for your help, Alchymyth!

  8. alchymyth
    The Sweeper
    Posted 9 months ago #

    The closing span was correct as there is a span class above called month so I left that part out.

    i know; as far as i remember, i just moved the closing span from within the foreach loop to after it; to avoid multiple closing spans in case of posts with multiple tags.

  9. MarjWyatt
    Member
    Posted 9 months ago #

    Hi Alchynmyth.

    The code was working with multiple tags with the span close where it was so I didn't look at it that way. I've moved the span close where you suggested it be earlier.

    I have discovered that the code is fragile to human error, however. If there are no tags on the post, an ugly error displays calling out the endif line within the foreach.

    The person for whom I am developing this code is not dull and I'm sure he will forgive the necessity to add tags to the posts but it would be nice to not have the risk. Have you got any ideas for a remedy on that issue?

    Thanks!

  10. alchymyth
    The Sweeper
    Posted 9 months ago #

    The code was working with multiple tags with the span close where it was

    iw ould assume that most browsers can ignore excess closing span tags; it would show if you run your site over the http://validator.w3.org/ ...

    to avoid the 'invalid argument for foreach..' error if the post has no tags, you could change this line:

    <?php foreach (get_the_tags() as $tag) {

    to:

    <?php if(get_the_tags()) foreach (get_the_tags() as $tag) {

    (untested)

  11. MarjWyatt
    Member
    Posted 9 months ago #

    Hello Alchymyth ...

    Here is the final, FINAL and tested code:

    <div class="thumbnail">
      <a href="<?php the_permalink(); ?>">
      <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
      <span class="overlay"></span></a>
      <div class="category"><?php the_category(); ?></div>
      <span class="month"><?php echo get_post_meta( $post->ID, '_ct_text_4e3318cbab83d', true ); ?>
      <?php if(get_the_tags()) foreach (get_the_tags() as $tag) {
        if ($tag->slug == 'fav') { // place "fav-icon" image if post is tagged fav ?>
          <span class="fav"><img src="<?php bloginfo(url)?>/wp-content/themes/TheStyle/images/fav-icon.png" /></span>
          <?php } //end of if ?>
      <?php };  //end of foreach ?>
      </span> <!-- end month </span>-->
    </div> <!-- end .thumbnail -->

    No margin for human error now and everything is working great.

    I am very grateful for your assistance and thank you again.

Reply

You must log in to post.

About this Topic