• Reading over the codex I have the impression that you can only access a page or post’s custom fields from within the loop. Is there any way around that?

    I’d like to be able to use custom fields to create descriptions and keywords for some post and pages which I could then insert as meta elements in head.

    I know there are plugins that will do what I want, but I was wondering if I could do this just using template tags and a little clever PHP?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Assuming you have custom fields called ‘description’ and ‘keywords’, try:

    <meta name="description" content="<?php if(get_post_meta($post->ID, "description", true) !='' ) echo get_post_meta($post->ID, "description", true); else bloginfo('description');?>" />
    <meta name="keywords" content="<?php if(get_post_meta($post->ID, "keywords", true) != '') echo get_post_meta($post->ID, "keywords", true); else echo strtolower(get_bloginfo('name'));?>" />

    That gives you a very basic fallback if the necessary custom fields aren’t available.

    salandra104

    (@salandra104)

    how can I customize this meta information to do this following. If there is no field called “description” to include the my custom description “This site is all about this and that”

    salandra104

    (@salandra104)

    <meta name="description" content="<?php if(get_post_meta($post->ID, "metadesc", true) !='' ) echo get_post_meta($post->ID, "metadesc", true); else bloginfo('description');?>" />
    <meta name="keywords" content="<?php if(get_post_meta($post->ID, "metakey", true) != '') echo get_post_meta($post->ID, "metakey", true); else echo strtolower(get_bloginfo('name'));?>" />

    this worked thank you

    samanthaarmacost

    (@samanthaarmacost)

    @esmi

    Thanks so much for posting your code, this is exactly what I have been looking for, all I have found through Google has over complicated it.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Using custom fields for meta tags (keywords and descriptions) in head’ is closed to new replies.