treehouse : what would you like to learn today?
Web Design Web Development iOS Development

When post_meta just ain't there...

  • Hey guys,

    I'm working on a parent page that basically consists of nothing more than a big list of its child pages. The title of each child page is given, along with a thumbnail and some basic excerpt details via custom fields and get_post_meta. So far so good. Where I'm falling down is what happens when a custom field is requested and it's not there, and how to then replace that missing data with default data. I can manage it no probs with the thumbnail, but not for the excerpt. The page isn't live I'm afraid - but here's the code I'm using:


    $excerptPath = get_post_meta($post->ID, \"excerpt\", true);
    if($excerptpath ==\"\") {
    $excerptpath = \"We haven't included any text here yet, but we hope to have done so shortly!\";
    }


    If the 'excerpt' custom field is not present, however, the dummy text doesn't show. This exact syntax works fine for the thumbnail custom field (replacing it with a fallback img path). Why doesn't it work in this case?

    Thanks in advance! Any help appreciated.
  • Try this:

    $excerptPath = get_post_meta($post->ID, \"excerpt\", true);
    if($excerptpath) {
    $excerptpath = \"We haven't included any text here yet, but we hope to have done so shortly!\";
    }
  • No Joy...

    I figure that the code is correct, ('coz that's basically what's in the Codex) but that I'm putting it in the wrong place.

    Here's the whole page (not too big!):


    <?php
    /*
    Template Name: pastponies
    */

    get_header(); ?>


    <div id=\"centered-wrap\">
    <h2 class=\"mainsub-title\"><?php the_title() ?></h2>
    <div id=\"content\">
    <?php the_content(); ?>

    <?php $intropath = get_post_meta($post->ID, \"intro\", true); ?>
    <p class=\"intro\"><?php echo $intropath ?></p>

    <?php if ( (is_page('Past Ponies')) ) {
    query_posts (array('showposts' =>100, 'post_type' =>'page', 'post_parent' => 337, 'orderby' => 'title', 'order' => 'ASC' ));

    while (have_posts()) {

    the_post();

    $thumbpath = get_post_meta($post->ID, 'thumbnail', true);

    if($thumbpath ==\"\") {
    $thumbpath = '/images/logo/logo_pic_coming_soon_200.gif';
    }
    ?>

    <div class=\"biglist-item\">

    <h3 class=\"list-title\"><a href=\"<?php the_permalink() ?>\"><?php the_title() ?></a></h3>
    <a href=\"<?php the_permalink() ?>\"><img src=\"<?php echo $thumbpath ?>\" alt=\"\" class=\"list-thumb\" /></a>

    <?php $excerptPath = get_post_meta($post->ID, \"excerpt\", true);
    if($excerptpath) {
    $excerptpath = \"We haven't included any text here yet, but we hope to have done so shortly!\";
    }
    ?>

    <p><?php echo $excerptPath ?></p>

    <?php $studDetailsPath = get_post_meta($post->ID, \"studDetails\", true); ?>
    <?php echo $studDetailsPath ?>

    <span class=\"goto\"><a href=\"<?php the_permalink() ?>\">More about <?php the_title() ?></a></span>

    </div>

    <?php }

    wp_reset_query();

    }
    ?>

    <div class=\"clear\"></div>
    </div>

    <?php get_footer(); ?>


    Again, any help greatly appreciated! :D
  • Any luck?