WordPress post->ID 问题
这是一个wordpress问题。我试图在我的内部页面模板上使用一些在我的主页上运行良好的代码:
query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
$cal_date_j = date('j', intval(get_post_meta($post->ID, 'date_value', true)));
$cal_date_n = date('n', intval(get_post_meta($post->ID, 'date_value', true)));
$my_array[] = date('j, n', intval(get_post_meta($post->ID, 'date_value', true)));
$issetdate = get_post_meta($post->ID, 'date_value', true);
if (isset($issetdate)) {
echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
echo '<a href="' . get_permalink() . '">';
the_title();
echo '</a></li>';
}
endwhile;
echo '</ul></div>';
但是,这似乎不适用于内部页面。所有标题链接都正确输出,但它不会正确打印 get_post_meta 部分。
列表项都显示类似
我认为我尝试使用 $post->ID 的方式可能存在一些问题,但我不确定这是怎么回事。有什么想法吗?
This is a wordpress question. I am trying to use a bit of code that works just fine on my home page on my inner page templates:
query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
$cal_date_j = date('j', intval(get_post_meta($post->ID, 'date_value', true)));
$cal_date_n = date('n', intval(get_post_meta($post->ID, 'date_value', true)));
$my_array[] = date('j, n', intval(get_post_meta($post->ID, 'date_value', true)));
$issetdate = get_post_meta($post->ID, 'date_value', true);
if (isset($issetdate)) {
echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
echo '<a href="' . get_permalink() . '">';
the_title();
echo '</a></li>';
}
endwhile;
echo '</ul></div>';
However, this doesn't seem to work on the inner-pages. All the titles links are being outputted correctly but it won't print the get_post_meta part correctly.
The list items all display something like <li class="cal_event_li list_item_1_1">
I think there is perhaps some issue with the way I have tried to use $post->ID but Im not sure whats going on here. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用
query_posts
时,您必须调用global $post
来获取post_meta。如果您只调用一个类别,为什么不直接使用存档模板呢?另外,如果您要使用
query_posts
,请确保重置查询后缀,以便插件、侧边栏等仍然可以与条件循环等进行交互。When you use
query_posts
you have to callglobal $post
to get the post_meta. If you're only calling one category why don't you just use an archive template?Also if you're going to use
query_posts
make sure you reset the query afterwords so plugins, sidebars, etc can still interact with the loop for conditionals etc..尝试将内页中的 $post->ID 替换为 the_ID() 。像这样的东西
try replacing $post->ID with the_ID() in the inner pages. something like this