WordPress:使用自定义字段定义要循环显示的帖子

发布于 2024-09-03 04:57:24 字数 1409 浏览 2 评论 0原文

我正在尝试使用自定义字段,在其中输入我想要显示的帖子的帖子 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 &raquo;'); ?>

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花落人断肠 2024-09-10 04:57:24

我认为您还需要在 $args 数组中将参数 'posts_per_page' 作为 -1 传递(请参阅 query_posts() 法典)。

更新:

抱歉,我刚刚重新阅读了您的问题,我想我知道问题所在。将 $nlPostIds 作为直接参数传递,而不将其放置为数组。仅当每个元素都是 ID 时才传递数组。在这种情况下,您只需传递一个以逗号分隔的字符串。

更新:

使用;

$args = array('post__in' => @explode(',', $nlPostIds), 'posts_per_page' => -1);

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;

$args = array('post__in' => @explode(',', $nlPostIds), 'posts_per_page' => -1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文