自定义字段不在WordPress中工作

时间:2014-03-17 16:02:02

标签: wordpress custom-fields

我在侧边栏中创建了一个小时的操作小部件。它不输出在主页模板中设置的值。这位于侧边栏包含文件中。这会影响它吗?

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

-

更新

   
<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内容:

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

2 个答案:

答案 0 :(得分:0)

侧边栏小部件查询可能会影响帖子数据,而不会将其重置回主查询。为了确保你正在访问正确的帖子的met​​a执行重置并获得全局。示例如下:

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

答案 1 :(得分:0)

我明白了。我需要指明它是从一个页面而不是一个帖子中拉出来的:

<?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; ?>