Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Junior Member STHLMWebDesign's Avatar
    Join Date
    Apr 2010
    Location
    Stockholm
    Posts
    10
    Member #
    21731
    I'm trying to make dynamic meta tags without using a plug-in. What I'm looking to do is add the post tags and categories to the meta tags when viewing individual posts.

    This is what I've got right now:

    Code:
        <?php if (is_single()) {
        			foreach((get_the_category()) as $category) {
    					$keywords[] = strtolower($category->cat_name);
    				} 
    				foreach((get_the_tags()) as $tag) {
    					$keywords[] = strtolower($tag->name);
    				} 
    	?>
    	<meta name="keywords" content="standard, keywords, go, here, <?php echo implode(", ", array_unique($keywords)); ?>" /> 
         <?php } ?>
    This works pretty well, but the problem is that when you are viewing a page which is not a single post, then the entire meta tag line is never written into the HTML. It get's completely skipped over.

    What I'd like is for it to be printed with just the standard keywords skipping the extra stuff that's added for single pages. Can anyone help me, PHP isn't my strong point.

  2.  

  3. #2
    Member WPExplorer's Avatar
    Join Date
    Apr 2010
    Location
    USA
    Posts
    89
    Member #
    21716
    Personally, I wouldn't worry about meta keywords. These are pretty much not used at all by search engines. But if you really need them here is the problem...

    Your loop checks "if (is_single())" so it will only work on your single posts. If you want it to work on all pages you must change it to be: if (have_posts()

  4. #3
    Junior Member STHLMWebDesign's Avatar
    Join Date
    Apr 2010
    Location
    Stockholm
    Posts
    10
    Member #
    21731
    Thanks for your reply, unfortunately it doesn't help me much. I am aware that the code is set up for single posts. My problem is I can't seem to modify it to make it work on both single pages and other pages. I attempted to change it to this:

    Code:
     <?php if (is_home()) { $keywords = 'Bob Dole'; }
    elseif (is_single()) {
        			foreach((get_the_category()) as $category) {
    					$keywords[] = strtolower($category->cat_name);
    				} 
    				foreach((get_the_tags()) as $tag) {
    					$keywords[] = strtolower($tag->name);
    				} 
    	?>
    	<meta name="keywords" content="standard, keywords, go, here, <?php echo implode(", ", array_unique($keywords)); ?>" /> 
         <?php } ?>
    However the problem persists and the meta line is only printed into single pages. Examining it I can see that in the original code snippet the bracket opened on is_single doesn't get closed until after the meta line, it's closed in the final php tag which is only there with the curly bracket in it.

  5. #4
    Member WPExplorer's Avatar
    Join Date
    Apr 2010
    Location
    USA
    Posts
    89
    Member #
    21716
    <?php if (have_posts()) { foreach((get_the_category()) as $category) { $keywords[] = strtolower($category->cat_name); } foreach((get_the_tags()) as $tag) { $keywords[] = strtolower($tag->name); } ?> <meta name="keywords" content="standard, keywords, go, here, <?php echo implode(", ", array_unique($keywords)); ?>" /> <?php } ?>

  6. #5
    Junior Member STHLMWebDesign's Avatar
    Join Date
    Apr 2010
    Location
    Stockholm
    Posts
    10
    Member #
    21731
    Thanks for the response, but that's not exactly what I'm looking to do.

    What I am trying to do is maintain the existing condition for single pages and modify it, so if it's not a single page another condition is executed. Hence my code where I was trying to echo "Bob Dole" when is_home and run the original sequence to grab the categories and keywords when it's not.

    Get what I'm trying to do?

  7. #6
    Member WPExplorer's Avatar
    Join Date
    Apr 2010
    Location
    USA
    Posts
    89
    Member #
    21716
    You can set up 3 headers: header-home.php, header-single.php, header-page.php.

    Then add what you want to those meta fields and call each one as such

    home.php - <?php get_header(home); ?>
    single.php - <?php get_header(single); ?>
    page.php - <?php get_header(page); ?>

    The reason I would do this is because it lessens the amount of required code needed to pull out what you want from your db. Plus, you can add your homepage meta tags manually without having to use any loop.

    *But personally it would be much easier to set up 2 custom fields - one for meta keywords and one for meta-description. This way you have greater control.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 09:31 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com