WordPress:使用自定义字段定义要循环显示的帖子
我正在尝试使用自定义字段,在其中输入我想要显示的帖子的帖子 ID 号,以逗号分隔。但由于某种原因,仅显示帖子 ID 系列的第一篇帖子。有人可以帮忙吗? $nlPostIds 的值为(减去引号):“1542,1534,1546”。这是代码...最重要的部分是第四行 'post__in' =>;数组($nlPostIds)
<?php
$nlPostIds = get_post_meta($post->ID, 'nlPostIds', true);
$args=array(
'post__in' => array($nlPostIds)
);
query_posts($args);
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="entry">
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<div class="allinfos"><span class="date"><?php the_time('F jS, Y') ?></span> | <span class="comments"><?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?> </span> | <span class="category">Posted in <?php the_category(', ') ?></span> <!-- by <?php the_author() ?> --></div>
<?php the_content('More »'); ?>
<?php the_tags('Tags: ', ', ', ' '); ?> <?php edit_post_link('Edit', '[ ', ' ]'); ?>
<div class="clear"></div>
</div></div>
<?php endwhile; endif; ?>
谢谢!
I'm trying to use a custom field in which I input the post ID numbers of the posts I want to show, seperated by commas. For some reason though, only the first post of the series of the post IDs are displaying. Can someone help? The value of $nlPostIds is (minus the quotes): "1542,1534,1546". Here's the code... the most important part is the 4th line 'post__in' => array($nlPostIds)
<?php
$nlPostIds = get_post_meta($post->ID, 'nlPostIds', true);
$args=array(
'post__in' => array($nlPostIds)
);
query_posts($args);
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="entry">
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<div class="allinfos"><span class="date"><?php the_time('F jS, Y') ?></span> | <span class="comments"><?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?> </span> | <span class="category">Posted in <?php the_category(', ') ?></span> <!-- by <?php the_author() ?> --></div>
<?php the_content('More »'); ?>
<?php the_tags('Tags: ', ', ', ' '); ?> <?php edit_post_link('Edit', '[ ', ' ]'); ?>
<div class="clear"></div>
</div></div>
<?php endwhile; endif; ?>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您还需要在
$args
数组中将参数 'posts_per_page' 作为 -1 传递(请参阅 query_posts() 法典)。更新:
抱歉,我刚刚重新阅读了您的问题,我想我知道问题所在。将
$nlPostIds
作为直接参数传递,而不将其放置为数组。仅当每个元素都是 ID 时才传递数组。在这种情况下,您只需传递一个以逗号分隔的字符串。更新:
使用;
I think you need to also pass the argument 'posts_per_page' as -1 in your
$args
array (see the Codex on query_posts()).UPDATE:
Apologies, I've just re-read your question and I think I know the problem. Pass
$nlPostIds
as the direct argument, without placing it an array. You only pass an array when each element is an ID. In this care you're just passing a comma-separated string.UPDATE:
Use;