0

I´m trying to add code to a template file of mine, working with a raw theme I generated from “”. The file is a duplicate of single.php and called single-post.php. Like WP states in Codex. I am trying to add my tags of my post to the of my pages for blog posts. I want to wrap them in code for META tags, namely the article:tags. Whatever I do I keep getting the correct output, but no actual information, no tag names.

I am not using the wp_head in the template file. For custom reasons. Also, I have been trying without and with the loop present before my calls to the get_tags. Additionally, this is made with code that is not usable in head, I know, but it doesn´t matters, this happens no matter what I put there. Will have the meta tags later.

Basic WP code (supposedly for single posts): https://codex.wordpress.org/Function_Reference/get_tags

<?php

  $tags = get_tags();
  $html = '<div class="post_tags">';
  foreach ( $tags as $tag ) {
    $tag_link = get_tag_link( $tags->term_id );

    $html .= "<a href='{$tag_link}' title='{$tags->name} Tag' class='{$tags->slug}'>";
    $html .= "{$tags->name}</a>";
  }
  $html .= '</div>';
  echo $html;

?>

Adding this code produces:

Now I´ve tried this with some small mods, using the post while loop:

<?php

if( have_posts() ):

        while( have_posts() ): the_post();

        // here´s the tag code
        $tags = get_tags();
        $html = '<div class="post_tags">';
        foreach ( $tags as $tag ) {
            $tag_link = get_tag_link( $tags->term_id );

            $html .= "<a href='DELETE' title='{$tags->name} Tag' class='DELETE'>";
            $html .= "{$tags->name}</a>";
        }
        $html .= '</div>';
        echo $html;

    ?>

<?php endwhile;

endif;

?>

It still produces:

<div class="post_tags">
    <a href='DELETE' title=' Tag' class='DELETE'></a>
    <a href='DELETE' title=' Tag' class='DELETE'></a>
    <a href='DELETE' title=' Tag' class='DELETE'></a>
    <a href='DELETE' title=' Tag' class='DELETE'></a>
</div>

The title tag is still empty, title=' Tag'.

1 Answer 1

0

Okay, so after some more tinkering, I found another solution. This one works with or without the loop. So adding it in the template file or your single.php is fine, in the <head> of the HTML.

<?php $tags = get_the_tags($post->ID);  ?>
<?php foreach($tags as $tag) :  ?>
    <meta property="article:tag" content="<?php print_r($tag->name);?>" />
<?php endforeach; ?>

It also works on the page, I get it working with no html or php present whatsoever in the single.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.