$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>page template - Inserting article:tags meta in html head-element, using get tags, no wp_head|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

page template - Inserting article:tags meta in html head-element, using get tags, no wp_head

matteradmin9PV0评论

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):

<?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'.

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/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'.

Share Improve this question asked Nov 30, 2018 at 2:07 andiOakandiOak 1716 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

Post a comment

comment list (0)

  1. No comments so far