• I have a code-snippet on my site to show the last second tag.

    <?php
    $posttags = get_the_tags();
    $count=0;
    if ($posttags) {
      foreach($posttags as $tag) {
        $count++;
        if (2 == $count) {
    	echo '<a href="http://www.xyz.com/' . $tag->slug . '" alt="' . $tag->name . '" title="' . $tag->name . '">' . $tag->name . '</a>';
        }
      }
    }
    ?>

    i include this.. on the tag-page..

    18 sites… and on one… it is the wrong tag.
    i can’t find the bug… is it in the database?

    thanks for help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Hello again! Your code is fine. The issue is indirectly the database, but it’s not a bug.

    In practical terms, you cannot reliably expect get_the_tags() to return tags in a particular order. When you freshly add tags in order, you typically get tags in the order you want, but if any changes are made to the tags, the resulting order is unpredictable.

    Actually, the order is predictable, just not that useful. I believe the order is the order the tags appear in the terms table. You cannot specify any different order with get_the_tags(). That function does use a lower level function to get tag terms, and that function can accept orderby arguments. That function is wp_get_object_terms(). Even so, the columns available to orderby are limited, so this may be of little benefit.

    IMO, attempting to convey extra meaning in the order of the tags is not a good concept.

    Incidentally FYI, you don’t need to run a loop to get the second element of an array. You could get rid of the loop and on the echo line replace each $tag occurrence with $posttags[1] and get the same result more efficiently.

    Thread Starter mrfraz

    (@mrfraz)

    uuh.. i don’t understand all 🙂
    can i do a hotfix… for this tag?

    i have only on one.. page this (bug).

    or how can is fix it?

    thanks!

    Moderator bcworkz

    (@bcworkz)

    You can manually edit the database via phpMyAdminn so that the desired tag is placed such that it is the second tag found for that particular query. This can be quite dangerous as more than one table is involved, with a confusing relationship between them. If not done correctly, you could break all the term references. I encourage you to NOT try this, I only provide this as information since you asked. If you do try anyway, be sure to backup your database first.

    If the tags are not used elsewhere, you could try deleting them, then reinsert them in the correct order. This method may not always work right.

    Attempting to convey special meaning to the second tag is not a good approach. A better approach may be to create a custom hierarchical taxonomy where these important tags can all be children of a particular term.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Show second tag’ is closed to new replies.