WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Meta keywords from tags? (12 posts)

  1. Class
    Member
    Posted 6 years ago #

    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?

  2. Kafkaesqui

    Posted 6 years ago #

    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.

  3. Class
    Member
    Posted 6 years ago #

    That works great, thanks, Kafkaesqui.

  4. Rok
    Member
    Posted 6 years ago #

    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; ?>
  5. Kafkaesqui

    Posted 6 years ago #

    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;
    ?>
  6. Chris_K
    Member
    Posted 6 years ago #

    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.

  7. Kafkaesqui

    Posted 6 years ago #

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

  8. Class
    Member
    Posted 6 years ago #

    Well, it still works! :)
    Thanks again guys.

  9. bernard1980
    Member
    Posted 6 years ago #

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

  10. estjohn
    Member
    Posted 6 years ago #

    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.

  11. siriusforex
    Member
    Posted 6 years ago #

    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...
  12. estjohn
    Member
    Posted 6 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic