WordPress.org

Ready to get started?Download WordPress

Forums

Creating a custom metatag for posts and leveraging post tags (9 posts)

  1. icemonkey9
    Member
    Posted 1 year ago #

    Google announced that for sites that are Google News certified, they are going to use a new metatag that will actually matter. Great. So I need, for each post, to come up with a metatag that looks like this:

    <meta name="news_keywords" content="tag1, tag2, tag3, tag4...">

    I know I'll want to put this code in the header.php file but I really just want it to show up on Posts only, but I have no idea how to write the appropriate PHP code to take the tags of a particular post and have them appear as I want them to in the above example.

    Any help?

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/Function_Reference/is_single

    http://codex.wordpress.org/Function_Reference/get_the_tags

    example code:

    <?php if( is_single() ) : ?>
    <meta name="news_keywords" content="<?php $sep = ''; $tags = get_the_tags($post->ID); if( $tags ) foreach( $tags as $tag ) { echo $sep . $tag->name; $sep = ', '; } else echo 'un-tagged'; ?>">
    <?php endif; ?>
  3. rdholla
    Member
    Posted 1 year ago #

    @alchymth - after putting the code you provided in my header.php, how exactly do I designate the keywords for each specific post? Would I use the custom fields in the post? And for the "Name" do I just put "keywords" (without quotes) and then list my keywords in the "Value"???

    I actually figured out how to implement and get it working correctly and I've written a tutorial here:

    http://bit.ly/Sa19gC

    But if you visit the at http://tabletcrunch.com and look in the top left corner, you'll see (or at least I see) some code that looks like this:

    /**/ " />

    If I remove the code for the new meta tag, then that code disappears - when I put it back, that little bit of code appears on the top left of my homepage - but only my homepage.

    Thoughts?

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    how exactly do I designate the keywords for each specific post?

    from your question, I had to assume you were referring to the build-in WordPress taxonomy tags ...
    which are retrieved in the code by 'get_the_tags()'.

    your code outputs a lot of duplicate keywords on a front page (for each post where the custom field is not set).

    as for the stray output, double check your code - the closing
    " />
    seems to be broken (?)

    or copy the section from header.php of your theme and post it here, so someone can help checking it.

  5. rdholla
    Member
    Posted 1 year ago #

    Below is the entire block of code I put right before the </head> tag in header.php. Also, how do I stop "a lot of duplicate keywords on the front page" for posts where the custom field is not set?

    <meta name="news_keywords" content="<?php
    while (have_posts()) : the_post();
    $keywords = get_post_meta($post->ID, 'keywords', false);
    if ($keywords){
    echo $keywords[0];
    }
    else
    {
    echo 'iPad, Android tablets, tablet pc, tablet pcs' ;
    }
    endwhile;
    ?>" />

  6. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    @rdholla
    I just noticed, you have actually hijacked the topic ;-(

    anyway - hopefully this will also improve the answer to the original question.

    stop "a lot of duplicate keywords on the front page" for posts where the custom field is not set

    possibly work with an array;

    example:

    <meta name="news_keywords" content="<?php
    $news_keys = array();
    while (have_posts()) : the_post();
    $keywords = get_post_meta($post->ID, 'keywords', false);
    if ($keywords){
    $news_keys[] = $keywords[0];
    }
    else
    {
    $news_keys[] = 'iPad, Android tablets, tablet pc, tablet pcs' ;
    }
    endwhile;
    echo implode(', ', array_unique($news_keys));
    ?>" />

    http://php.net/manual/en/function.array-unique.php
    http://php.net/manual/en/function.implode.php

    as for the stary characters again, can you check the full code of header.php; it seems that you maybe added the new code into something existing (the <div ...> is very invalid in that section of a header; and there seems to be some script code involved ???);

    the section below is causing the stray characters, and also contains your news_keywords code's output:

    <div
    style="font-color: white;"><meta
    name="news_keywords" content="news_keywords, google news keyword tag, keyword meta tag google news, google news, google news keywords,these, are, test, keywords, that, testingwalmart, amazon, kindle fire, target, kindle fire hdiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcsiPad, Android tablets, tablet pc, tablet pcs <script type="text/javascript">/*<![CDATA[*///
            var disqus_shortname = 'tabletcrunch';
            (function () {
                var nodes = document.getElementsByTagName('span');
                for (var i = 0, url; i < nodes.length; i++) {
                    if (nodes[i].className.indexOf('dsq-postid') != -1) {
                        nodes[i].parentNode.setAttribute('data-disqus-identifier', nodes[i].getAttribute('rel'));
                        url = nodes[i].parentNode.href.split('#', 1);
                        if (url.length == 1) { url = url[0]; }
                        else { url = url[1]; }
                        nodes[i].parentNode.href = url + '#disqus_thread';
                    }
                }
                var s = document.createElement('script'); s.async = true;
                s.type = 'text/javascript';
                            s.src = 'http' + '://' + 'disqus.com/forums/' + disqus_shortname + '/count.js';
                (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
            }());
        ///*]]>*/</script> " /></div>
  7. rdholla
    Member
    Posted 1 year ago #

    umm... what does that mean "I hijacked the topic?"

    is the section you posted that's causing the stray characters in my header.php?

    I'm sorry, I'm trying to follow your help but either I'm not getting it - or you are speaking way over my head.

  8. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    question is:

    what is the full code of header.php?

    i.e. where exactly is the new code integrated into the existing code of header.php?

  9. rdholla
    Member
    Posted 1 year ago #

    Full code to my header.php - I just selected the whole thing and pasted it below. PLEASE let me know your thoughts! And thank you so much for your help

    <<<< header.php code follows >>>>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes( 'xhtml' ); ?>>
    <head profile="http://gmpg.org/xfn/11">
    <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
    <title><?php tj_custom_titles(); ?></title>
    <?php tj_custom_description(); ?>
    <?php tj_custom_keywords(); ?>
    <?php tj_custom_canonical(); ?>
    <link rel="profile" href="http://gmpg.org/xfn/11" />
    <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
    <link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />
    <link rel="alternate" type="application/atom+xml" title="<?php bloginfo('name'); ?> Atom Feed" href="<?php bloginfo('atom_url'); ?>" />
    <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
    <link rel="stylesheet" type="text/css" href="<?php bloginfo( 'template_url' ); ?>/styles/<?php echo get_option('freshlife_theme_stylesheet');?>" />
    <link rel="stylesheet" type="text/css" href="<?php bloginfo( 'template_url' ); ?>/custom.css" />
    <?php wp_head(); ?>

    <div style="font-color: white;">
    <meta name="news_keywords" content="<?php
    while (have_posts()) : the_post();
    $keywords = get_post_meta($post->ID, 'keywords', false);
    if ($keywords){
    echo $keywords[0];
    }
    else
    {
    echo 'iPad, Android tablets, tablet pc, tablet pcs' ;
    }
    endwhile;
    ?>" />
    </div>

    <meta name="verifyownership" content="15d21e8fc52144b369bb92862230bcac" />

    </head>
    <?php if (is_home() || is_archive() || is_search() ) add_filter('img_caption_shortcode', create_function('$a, $b, $c','return $c;'), 10, 3); ?>
    <body <?php body_class(); ?>>
    <div id="wrapper">
    <div id="top">

    <?php $menuClass = 'topnav';
    $menuID = 'page-nav';
    $primaryNav = '';
    if (function_exists('wp_nav_menu')) {
    $primaryNav = wp_nav_menu( array( 'theme_location' => 'primary-nav', 'container' => '', 'fallback_cb' => '', 'menu_class' => $menuClass, 'menu_id' => $menuID, 'echo' => false ) );
    };
    if ($primaryNav == '') { ?>
    <ul id="<?php echo $menuID; ?>" class="<?php echo $menuClass; ?>">
    <?php if (get_option('freshlife_home_link') == 'on') { ?>
    <li class="<?php if(is_home()) echo('first');?>">"><?php _e('Home', 'themejunkie') ?>
    <?php } ?>
    <?php show_page_menu($menuClass,false,false); ?>

    <?php } else echo($primaryNav); ?>

    <div id="search">
    <?php get_search_form(); ?>
    </div> <!-- end #search -->

    <div class="clear"></div>

    </div> <!--end #top-->

    <div id="header">

    <?php if (get_option('freshlife_text_logo_enable') == 'on') { ?>
    <div id="text-logo">
    <h1 id="site-title">"><?php bloginfo('name'); ?></h1>
    <p id="site-desc"><?php bloginfo('description'); ?></p>
    </div> <!-- end #text-logo -->
    <?php } else { ?>
    "><?php $logo = (get_option('freshlife_logo') <> '') ? get_option('freshlife_logo') : get_bloginfo('template_directory').'/images/logo.png'; ?><img src="<?php echo $logo; ?>" alt="<?php bloginfo('name'); ?>" id="logo"/>
    <?php }?>

    <?php /* Header Ad */
    if( get_option('freshlife_header_ad_enable') == 'on'){
    echo "<div class='header-ad'>";
    echo get_option('freshlife_header_ad_code');
    echo "</div>";
    } ?>

    <div class="clear"></div>

    </div> <!--end #header-->

    <div id="cat-menu">
    <?php $menuClass = 'nav';
    $menuID = 'cat-nav';
    $secondaryNav = '';
    if (function_exists('wp_nav_menu')) {
    $secondaryNav = wp_nav_menu( array( 'theme_location' => 'secondary-nav', 'container' => '', 'fallback_cb' => '', 'menu_class' => $menuClass, 'menu_id' => $menuID, 'echo' => false ) );
    };
    if ($secondaryNav == '') { ?>
    <ul id="<?php echo $menuID; ?>" class="<?php echo $menuClass; ?>">
    <?php show_categories_menu($menuClass,false,false); ?>

    <?php } else echo($secondaryNav); ?>
    </div> <!--end #cat-nav-->

Topic Closed

This topic has been closed to new replies.

About this Topic