• Resolved Class

    (@class)


    Is it possible to grab the tags and use them for meta keywords without a plugin?
    Since the_tags needs the The Loop, can I add another loop in the header?

Viewing 11 replies - 1 through 11 (of 11 total)
  • We can get the tags into the header this way:

    [See later reply for better version]

    <?php global $post;
    if( is_single() || is_page() ) :
    	$tags = get_the_tags($post->ID);
    	foreach($tags as $tag) :
    		$sep = (empty($keywords)) ? '' : ', ';
    		$keywords .= $sep . $tag->name;
    	endforeach;
    ?>
    <meta name="keywords" content="<?php echo $keywords; ?>" />
    <?php endif; ?>

    Note that I wrapped the output in a test for whether we are on a single post or Page query. Otherwise the keywords would come from the latest post displayed on the page.

    Thread Starter Class

    (@class)

    That works great, thanks, Kafkaesqui.

    Kaf, the below code generates invalid argument supplied for each function error, when you view Archive pages i.e. Category, Monthly, Daily etc. Archives.

    <?php global $post;
    if( is_single() || is_page() ) :
    	$tags = get_the_tags($post->ID);
    	foreach($tags as $tag) :
    		$sep = (empty($keywords)) ? '' : ', ';
    		$keywords .= $sep . $tag->name;
    	endforeach;
    ?>
    <meta name="keywords" content="<?php echo $keywords; ?>" />
    <?php endif; ?>

    Small update to the code takes into account the possibility there are no tags (not a good thing with the above statement).

    <?php global $post;
    if( is_single() || is_page() ) :
    	$tags = get_the_tags($post->ID);
    	if($tags) :
    		foreach($tags as $tag) :
    			$sep = (empty($keywords)) ? '' : ', ';
    			$keywords .= $sep . $tag->name;
    		endforeach;
    ?>
    <meta name="keywords" content="<?php echo $keywords; ?>" />
    <?php
    	endif;
    endif;
    ?>

    For continuity purposes, I need to point out that Kaf likely hadn’t yet seen Rok’s post when he made his last one. Rok’s was languishing in the akismet queue.

    Carry on then.

    It should never have generated an error on ‘archive’ pages because these are neither single posts or Pages, which the code first tests against (through the use of is_single() and is_page()) before proceeding. My mod is only meant to deal with posts and Pages that do not sport tags.

    (But Handy was correct about my not catching Rok’s reply!)

    Thread Starter Class

    (@class)

    Well, it still works! ??
    Thanks again guys.

    this is pretty good but how can I add meta keywords to my index page ( based on a page and not a post this seems impossible because their isn’t a tag option in the write page option.
    This means the posts havekeywords but the pages aren’t ???

    Wow, this is what I have been trying to do for the last 30 minutes with no success. I did not want to use a plugin, but do it ‘natural’ instead. I was missing an extra ‘ !! Thanks so much! (ps im using 2.5.1 and it works fine)

    Though, I did add one thing –
    is_single() || is_page() || is_home()

    so it would also use tags on the home page.

    @bernard1980 -= there used to be a plugin where you could add tags to a page… might try that and the could could read them and use them as keywords then? I personally think it would be a good thing to use keywords on pages and I am not sure why it is not a function built in.. but oh well thats just me.

    Thanks Kaf. This is great and thanks to you too Class for possing the question.

    I have a similar problem. I have been trying to set 5 most recent “Related Articles” to bottom-end of each post using by tags. This should be picked up from my previous Posts but no joy at the moment. Could anyone help please?

    How could I sort this out? Can I direct this question to Kaf or anyone else please?
    What I want is somthing like this –
    Related Articles

    <ul>
    	<li><strong>First Article</strong></li>
    	<li<strong>>... Article</strong></li>
    	<li><strong>Fifth Article</strong></li>
    </ul> More...

    siriusforex – There are related articles plugins… I would think you could set your keywords to match and cross reference?

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Meta keywords from tags?’ is closed to new replies.