我试图从侧边栏小部件中的自定义帖子类型中获取(日期)元字段:http://www.oransje.com/utvikling/ngnf/kurs/
不知何故,日期在小部件中没有正确显示(无论我放在哪里,也可以手动在侧栏中显示,代码工作正常)。有人知道线索吗?
代码如下所示:
<?php $loop = new WP_Query(array(
\'post_type\' => \'kurs\',
\'posts_per_page\' => \'5\',
\'meta_key\' => \'dato\',
\'meta_value\' => date(\'Y-m-d\'),
\'meta_compare\' => \'>=\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\'
)); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $display_date = date(\'d.m.Y\', strtotime(get_post_meta($post->ID, "dato", true))); ?>
<li><?php echo $display_date; ?> - <?php the_title( \'<a href="\' . get_permalink() . \'" title="\' . the_title_attribute( \'echo=0\' ) . \'" rel="bookmark">\', \'</a>\' ); ?></li>
<?php endwhile; ?>
</ul>
最合适的回答,由SO网友:Tom Boersma 整理而成
我通过添加以下内容修复了此问题:
global $post;
更具体地说:我无法解释为什么会这样,这超出了我的WordPress技能,但我在查询开始后就添加了它,因此代码如下所示:
<?php global $post;
$loop = new WP_Query( array(
\'post_type\' => \'kurs\',
\'posts_per_page\' => \'5\',
\'meta_key\' => \'dato\',
\'meta_value\' => date(\'Y-m-d\'),
\'meta_compare\' => \'>=\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\'
) ); ?>