Question

I've created an hours of operation widget in the sidebar. It's not outputting the values that are set within the Home page template. This is located in the sidebar include file. Could that be affecting it?

<div class="widget" id="hours">
    <?php $hours = get_post_meta($post->ID, 'hours', false); ?>
    <div class="widget-title">Business Hours:</div>
    <ul>
        <?php foreach($hours as $hour) {
            echo '<li>'.$hour.'</li>';
          } ?>
    </ul>
</div>

--

UPDATE:

<div class="row">
    <div class="large-12 column">
        <div class="slider"><?php echo do_shortcode("[metaslider id=4]"); ?></div>
      </div>
   </div> 

<div class="wrapper">

    <div class="row">
        <div class="large-9 column"> 
            <article>
                    <h3 class="section-title">What's On</h3>

                      <?php query_posts ('posts_per_page=3'); ?>
                      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

                     <section>
                        <div class="row clearfix">
                                <div class="large-4 column"> 
                                    <?php if ( has_post_thumbnail()) : ?>
                                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                                        <?php the_post_thumbnail('post-thumb'); ?>
                                    </a>
                                  <?php endif; ?>
                                </div>

                            <div class="large-8 column"> 
                                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                                    <div class="post-details">
                                    <p class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
                                    <p class="meta" id="date"><?php the_time('F j, Y'); ?></p> 
                                    <p class="entry"><?php the_excerpt(); ?></p> 
                                    </div>
                                </div>
                             </div>
                        </div><!--row-->
                     </section>

                    <?php endwhile; else: ?>
                    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
                    <?php endif; ?>

           </article>
        </div>

        <div class="large-3 hide-for-small column"> 
           <aside>
              <?php include('sidebar-home.php'); ?>
           </aside>
        </div>
    </div>

    <div class="row">
        <div class="large-12 column">
            <div id="weekly-promotions">
                <p class="extra hide-for-small">Every week we hand select our favourite finds.</p>
                <h3 class="section-title">Weekly Promotions</h3> 
               <?php echo do_shortcode("[supercarousel id=26]"); ?>
           </div>
        </div>
    </div>

</div><!-- #wrapper -->

SIDEBAR CONTENTS:

<h3 class="section-title">Find Us</h3>
<div class="widget" id="hours">
<?php wp_reset_query();
global $post;
$hours = get_post_meta($post->ID, 'hours', false); ?>
<div class="widget-title">Business Hours:</div>
    <ul>
        <?php foreach($hours as $hour) {
            echo '<li>'.$hour.'</li>';
          } ?>
    </ul>
</div>
<?php echo '<pre>'; print_r($hours); echo '</pre><br />'; echo $post->ID; ?>
<div class="widget" id="directions">
<div class="widget-title">Directions:</div>
    <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2878.098955645896!2d-79.05700300000001!3d43.833046099999954!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x89d4de53bb478629%3A0x23b9414d4c475d10!2s1400+Squires+Beach+Rd!5e0!3m2!1sen!2sca!4v1394214160873" width="100%" height="300" frameborder="0" style="border:0"></iframe>      
</div>
<div class="widget" id="map">
    <div class="widget-title">Download Our<br /> Floor Map!</div>                              
</div>  
Was it helpful?

Solution 2

I figured it out. I needed to specify that it was pulling from a page not a post:

<?php query_posts('post_type=page'); ?>
   <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <?php $hours = get_post_meta($post->ID, 'hours', false); ?>
        <ul>
            <?php foreach($hours as $hour) {
                echo '<li>'.$hour.'</li>';
            } ?>
        </ul>
   <?php endwhile; ?>
   <?php endif; ?>

OTHER TIPS

It could be that a sidebar widget query has affected the post data and not reset it back to the main query. To be sure you're accessing the correct post's meta perform a reset and get the global. Example below:

<div class="widget" id="hours">
    <?php wp_reset_postdata();
    global $post;
    $hours = get_post_meta($post->ID, 'hours', false); ?>
    <div class="widget-title">Business Hours:</div>
    <ul>
        <?php foreach($hours as $hour) {
            echo '<li>'.$hour.'</li>';
          } ?>
    </ul>
</div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top