WordPress.org

Ready to get started?Download WordPress

Forums

Using custom fields for meta tags (keywords and descriptions) in head (5 posts)

  1. dzr
    Member
    Posted 5 years ago #

    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?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 5 years ago #

    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.

  3. salandra104
    Member
    Posted 5 years ago #

    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"

  4. salandra104
    Member
    Posted 5 years ago #

    <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

  5. samanthaarmacost
    Member
    Posted 5 years ago #

    @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.

Topic Closed

This topic has been closed to new replies.

About this Topic