0

I have multiple loops on my home page. I am trying to display custom fields in each loop. But some of them are not showing. Any idea, why? Here is the code:

//check for Featured Events here
   $featured = new WP_Query (array(
   'category_name' => 'Featured Events',
   'posts_per_page' => 2) 
   ); 
    if ($featured -> have_posts()) { 
    while ($featured->  have_posts()) : $featured -> the_post(); ?>

<h2 class="featured"> <?php the_title(); ?> </h2>  
      <h3 class="featured">
    <?php echo get_post_meta($post->ID, 'Date', true); ?>  </h3>

<h4 class="featured">
    <?php echo get_post_meta($post->ID, 'Start Time', true); ?>
      <?php if ( get_post_meta($post->ID, 'End Time', true) != "") { ; ?>
     &nbsp;-&nbsp; 
    <?php echo get_post_meta($post->ID, 'End Time', true); }?> </h4>

<!--only print sponsor if value there  -->
 <?php if ( get_post_meta($post->ID, 'Sponsor', true) != "") { ; ?>

<h5 class="featured">

<?php echo get_post_meta($post->ID, 'Sponsor', true); ?></h5>
<?php }; ?>
 <?php if ( get_post_meta($post->ID, 'Cost', true) != "") { ; ?>

 <p class="cost"> 

<?php echo 'Cost: '. get_post_meta($post->ID, 'Cost', true); ?> </p> <?php }; 
    if ( get_post_meta($post->ID, 'Location', true) != "") { ; ?>

 <h6 class="featured">

<?php echo 'Location: '. get_post_meta($post->ID, 'Location', true); ?>
    </h6>

 <?php }; 
    global $more;
$more = 0; 
  the_content('Read more please'); 
  if ( get_post_meta($post->ID, 'link', true) != "") { ; ?>

 <p> <a href="
 <?php echo get_post_meta($post->ID, 'link', true); ?> " 

> Click here for more information </a> </p>

 <?php } endwhile; 
     } 
     wp_reset_query(); ?> 

           </div> <!-- content_inner -->
         </div> <!-- content -->
       </div>   <!-- end 2 column wrap div   -->

           <div  id="lftcol">
      <div id="lcol_inner">

<?php 
   $ann_query = new WP_Query (array(
   'category_name' => 'announcement',
   'orderby'=> 'modified',
   'posts_per_page' => 5) );

    if ($ann_query -> have_posts()) { 
    while ($ann_query->  have_posts()) : $ann_query -> the_post();

    if( $post->ID == $do_not_duplicate ) continue; //This is the Magic Line
// echo 'NON urgent announcement should follow '; ?>

 <h2 class="featured"> <?php the_title(); ?> </h2>

    <?php 
//The code must be inserted ahead of the call the_content, but AFTER the_post()
global $more;
$more = 0; ?>

<h3 class="featured">

<?php echo get_post_meta($post->ID, 'Date', true); ?>  

</h3>

 <h4 class="featured"> 

    <?php echo get_post_meta($post->ID, 'Start Time', true); ?>
     <?php if ( get_post_meta($post->ID, 'End Time', true) != "") { ; ?>
     &nbsp;-&nbsp; 
    <?php echo get_post_meta($post->ID, 'End Time', true); }?> 

</h4>

     <?php if ( get_post_meta($post->ID, 'Location', true) != "") { ; ?>

     <h6 class="featured">

<?php echo 'Location: '. get_post_meta($post->ID, 'Location', true); ?>

</h6> 

<?php }; 
    the_content('Read more please');
    if ( get_post_meta($post->ID, 'link', true) != "") { ; ?>

<p> <a href="

<?php echo get_post_meta($post->ID, 'link', true); ?> 

" > Click here for more information </a> </p>

    <?php } 
            endwhile;   }
            wp_reset_query(); ?>

        </div>
           </div> 

1 Answer 1

0

Yes, there are problems in your code. I'd like to recommend you to code on WordPress by opening relevant Codex pages for reference that, you are not mistaking.

First of all, in WP_Query() you used 'category_name' => 'Featured Events'. But according to codex, you can't use Category name here, you have to use Category Slug, and a Category slug cannot contain a space within, they have to be connected with an underscore. (Reference)

At second, in get_post_meta() your key is not right. Because a key cannot contain spaces within. You have to connect separate strings with underscore, like start_time(not Start Time). I guess the keys are working where you coded with a single string like Date. (Reference)

And lastly, at the end of each query, you used wp_reset_query(); to reset the query. But according to Codex, for WP_Query() the reset query code is not wp_reset_query();, but wp_reset_postdata(). (Reference)

That's all.

ADDITIONAL

In one of my project I used many custom fields. So I made two reusable functions for using in all-over my project. Just paste the following in your funcitons.php within <?php and ?>.

/**
 * REUSABLE POST_META FUNCTION
 */

function has_custom_field( $fieldName = '' ) {
    global $post;
    $fetchedCustomField = get_post_meta($post->ID, $fieldName, true);
    $CFchecker = ($fetchedCustomField == '') ? '0' : '1';
    return (bool) $CFchecker;
}

function custom_field( $fieldName = '' ) {
    global $post;
    $fetchedCustomField = get_post_meta($post->ID, $fieldName, true);
    return $fetchedCustomField;
}

And use them like:

<?php if(has_custom_field('date')) { echo custom_field('date'); } else { echo 'no date'; } ?>

And one additional tips: whenever you are coding on WordPress always use the WP_DEBUG to 'true' in wp-config.php. And don't forget to set it to 'false' again after the completion of the development. (Reference)

Good luck!

EDIT

To specify Custom Field, just under post/page Add the custom field like this:
custom field str_date

If you custom field is defined like the image, then the get_post_meta() would be:

<?php echo get_post_meta($post->ID, 'start_date', true); ?>

But for using custom field, I love to use Advanced Custom Fields plugin.

3
  • @user45758: if the answer is good, we upvote the answer, and if the answer solved the problem, we click on the tick-mark to mark the answer as accepted. It's not mandatory, but recommended. :) Commented Feb 2, 2014 at 15:07
  • I would be happy to upvote the answer. I am still reviewing it. One thing I don't understand is how do I know the correct way to specify the key on the get_post_meta call. Is it just by default that it is represented as 'start_time'?
    – user45758
    Commented Feb 3, 2014 at 2:47
  • @user45758 See my edit Commented Feb 3, 2014 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.