• Hi. I am trying to insert some keywords into my page titles for SEO reasons (per client request). My page structure is already set and i don’t want to mess with the naming structure, as i don’t need to edit all the pages. Here’s what i’ve tried to do:

    I’m trying to achieve this structure for my titles:

    (page keyword) | Blog Title | (general keyword)

    I’ve set custom fields in my pages and posts called page-keyword and general-keyword, and echoed them inside the title tag if they exist. However, for the category archives, i can’t add custom fields (right?) so i’m setting variables in my category templates before the call for the header, and trying to echo them in the title tag. The custom fields are working fine, and they output both of my keywords in the title. However, the keywords i’m trying to set with variables are not working. Here’s some of my code:

    in my header.php:

    <title>
    		<?php if(get_post_meta($post->ID, "page-keyword", true)) { ?>
            <?php echo get_post_meta($post->ID, "page-keyword", true); ?>
            <? } else { ?>
                    	<? echo $pageKeyword ; ?>
        	<? } ?>
     | <?php bloginfo('name'); ?>
     		<?php if(get_post_meta($post->ID, "general-keyword", true)) { ?>
     | <?php echo get_post_meta($post->ID, "general-keyword", true); ?>
     		<? } else { ?>
     | <?php echo $generalKeyword; } ?>
     </title>

    and in my category templates, something like this at the beginning….

    <? $pageKeyword = "My Page Keyword"; ?>
    <? $generalKeyword = "My General Keyword"; ?>
    <?php get_header(); ?>

    Can anyone tell me why those variables aren’t being passed or echoed…is my syntax wrong?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Why not use something like WordPress › All in One SEO Pack « WordPress Plugins. It has something like 3.6 million downloads at last count. It might not help with the exact title structure you want to use, but it seems to be bombproof when it comes to working with metatags.

    For what it’s worth, this is what I used to use for custom fields as meta:

    <meta name="description" content="<?php if (is_page(array('130','162','243','402','464','557','558','559','560','561'))) { echo get_post_meta($post->ID, 'description', true);
    } elseif (is_page('542')) { echo bloginfo('description');
    } elseif (is_category()) { echo trim(strip_tags(category_description()));
    } else { echo bloginfo('description'); } ?>" />

    Thread Starter derekelcreativecom

    (@derekelcreativecom)

    Hi. I appreciate the reply. I actually was already using the all in one plugin, but it didn’t allow me to have two different, arbitrary titles for each page. Before, i just pasted a simplified version of the code. I’m actually doing something similar to your code, using the category description as part of the category titles. And everything works except that i’m not able to pass two keyword phrases for each category, just the category description. If there’s any other thing i can set and use for each category, i’ll do it. Or, as a last resort, i guess i could use a long “if” statement in the header like:

    <? if (is_category('3')) { echo "Category 3"; ?>
    <? } elseif (is_category('5')) { echo "Category 5"; ?>
    <? } // and so on.... ?>

    That just seemed like a weird way to do it. And it just bugs me that i can’t get the variable thing to work.

    Here’s my full title code:

    <title>
    		<?php if(get_post_meta($post->ID, "page-keyword", true)) { ?>
            <?php echo get_post_meta($post->ID, "page-keyword", true); ?>
            <? } else { ?>
    
    				<? if(in_category('18') || in_category('3')) { // Stuff to do if this is a long-titled page ?>
    					<?php
                        $tit = the_title('','',FALSE);
                        echo substr($tit, 0, 45);
                        if (strlen($tit) > 45) echo "...";
                        ?>
    				<? } elseif (is_category()) { ?>
                    	<? //echo $pageKeyword ; ?>
                        <?php echo single_cat_title(); ?>
    
    				<? } else { wp_title(''); } ?>
    
        	<? } ?>
     | <?php bloginfo('name'); ?>
     <?php if(get_post_meta($post->ID, "general-keyword", true)) { ?>
     | <?php echo get_post_meta($post->ID, "general-keyword", true); ?>
     <? } elseif (is_category()) { ?>
     | <?php echo strip_tags(category_description()); } ?>
     </title>

    I’ll have to take a look at this later and parse it out. Interesting thing to try. But for now, sure All in one SEO isn’t conflicting with your custom code?

    According to this:
    http://codex.wordpress.org/Include_Tags

    the get_header() function is simply a safety function that includes a default header.php file if one does not exist in your theme folder or a custom header file if you declare it.

    So instead of calling the function just include the header file:

    include (“header.php”);

    Then all your variables will pass into the file. (I think) 🙂

    @feoconsulting — Thank you. I was having the same problem and using a simple include("header.php") rather than get_header() allowed me to pass variables.

    For example:
    ============================

    <?
    /*
    Template Name: Peaceful Powerful You
    */
    $pageName = "Peaceful, Powerful You!";
    $sectionName = "peaceful";
    include("header.php");
    ?>

    This code allowed me to pass the $pageName and $sectionName variables into the header. Using get_header() did not allow me to pass the variables.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Pass Variables From Templates into Header’ is closed to new replies.