• I am working on a site https://www.einhorns-epic-cookies.com. I am trying to change the Title and Meta Description for the individual pages but the code shown below prevents me from dong that and the Title and Meta Description seem the same for all the pages. Any help would be gretaly appreciated.

    <meta charset="<?php bloginfo('charset'); ?>" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta charset="<?php bloginfo('charset'); ?>" />
    <meta property="og:type" content="website">
    
    <?php if(is_single()) : global $post; ?>
    <title><?php echo WP_SITENAME; ?> | <?php single_post_title(''); ?></title>
    
    <meta property="og:site_name" content="<?php echo WP_SITENAME; ?>"/>
    <meta property="og:title" content="<?php single_post_title(''); ?>" />
    <meta property="og:url" content="<?php the_permalink() ?>" />
    <meta property="og:description" content="<?php echo htmlentities(strip_tags($post->post_content)); ?>" />
    <?php
    if (has_post_thumbnail()) {
      $img_array = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
      $og_thumb = $img_array[0];
    }
    else {
      $og_thumb = TEMPLATEDIR.'/assets/images/og.jpg';
    }
    ?>
    <?php elseif(is_home()): ?>
    <title><?php bloginfo('name'); ?> | Cookies. Comics. Unicorns.</title>
    <meta name="description" content="<?php echo WP_SITENAME; ?> | Cookies. Comics. Unicorns.">
    <meta property="og:site_name" content="<?php echo WP_SITENAME; ?> | Cookies. Comics. Unicorns."/>
    <meta property="og:title" content="<?php echo WP_SITENAME; ?> | Cookies. Comics. Unicorns." />
    <meta property="og:url" content="<?php echo WP_HOME_URL; ?>" />
    <meta property="og:description" content="<?php bloginfo('description'); ?>">
    <?php $og_thumb = TEMPLATEDIR.'/assets/images/og.jpg'; ?>
    <?php elseif(is_post_type_archive('cookies')): ?>
    <title><?php bloginfo('name'); ?> | Cookies. Comics. Unicorns.</title>
    <meta name="description" content="Cookie delivery to your door, a space unicorn comic book in every bag!">
    <meta property="og:site_name" content="<?php echo WP_SITENAME; ?> | Order Cookies Online. Cookie Delivery. Cookie Gift Boxes."/>
    <meta property="og:title" content="<?php echo WP_SITENAME; ?> | Order Cookies Online. Cookie Delivery. Cookie Gift Boxes." />
    <meta property="og:url" content="<?php echo WP_HOME_URL; ?>/cookies/" />
    <meta property="og:description" content="Cookie delivery to your door, a space unicorn comic book in every bag!">
    <?php $og_thumb = TEMPLATEDIR.'/assets/images/og.jpg'; ?>
    <?php else : ?>
    <title><?php echo WP_SITENAME; ?> |  | Cookies. Comics. Unicorns.</title>
    <meta name="description" content="<?php bloginfo('description'); ?>">
    <meta property="og:site_name" content="<?php echo WP_SITENAME; ?>"/>
    <meta property="og:title" content="<?php echo WP_SITENAME; ?>" />
    <meta property="og:url" content="<?php echo WP_HOME_URL; ?>" />
    <meta property="og:description" content="<?php bloginfo('description'); ?>">
    <?php $og_thumb = TEMPLATEDIR.'/assets/images/og.jpg'; ?>
    <?php endif; ?>
    <meta property="og:image" content="<?php echo $og_thumb;?>" />

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been damaged by the forum’s parser.]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You first need to save what you want as title and description of each page somewhere. Custom fields are a good option for this. Then you can retrieve this saved information on your template with get_post_meta(). After getting the custom field, check if the value is an empty string, meaning no one assigned a value for this post/page. In that case some sort of default value should be output.

    You probably only want this behavior for individual posts and pages, and not for index lists and other pages that list multiple items. You should then check if is_single() is true before trying to use custom fields.

    Another issue you will encounter is the current post ID has not yet been assigned when your template code executes. For single queries, you can get the post ID with get_queried_object_id().

Viewing 1 replies (of 1 total)
  • The topic ‘Creating Title and Meta Description Manually from custom PHP’ is closed to new replies.