英文:
How to pass custom field value into array in wordpress loop argument?
问题
这只返回第一个输入的数字(ID)的页面。如何传递这些值,以便根据它们的ID返回页面?
英文:
There is a custom text field, where the user is prompt to enter page IDs to be displayed in the side bar. I am looking for a way to pass those numbers entered in the custom field into "post__in" argument as array.
Code so far:
<?php $pageid = get_post_meta(get_the_ID(),'key', true);
$args = array(
'post_type' => 'page',
'post__in' => array($pageid)
);
$childrens = new WP_Query($args);
if ($childrens->have_posts()) : ?>
<div class="sidebar-post-container">
<?php while ($childrens->have_posts()) : $childrens->the_post();?>
<div class="staff-thumbnail" id="staff-<?php the_ID(); ?>">
<figure id="attachement_<?php the_ID();?>">
<?php the_post_thumbnail(array(200,200,true),array('loading' => 'lazy', "alt"=> '', 'aria-hidden'=> 'true', 'tabindex' =>"-1"));?></figure>
<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
</div>
<?php endwhile; ?>
</div>
<?php endif; wp_reset_query(); ?>
This only returns one page with first number (ID) entered.
How can we pass these values so it returns pages by their IDs?
答案1
得分: 0
Your $pageid
从post_meta
中返回的是一个字符串,array($pageid)
只创建了一个包含单个元素的数组。你需要使用explode(...)
将字符串转换为数组。我建议指导用户用逗号分隔id,如1,2,3,4,5
。然后使用以下代码应该可以工作:
'post__in' => explode(',', $pageid)
我还建议进行一些其他调整:
- 将您的meta键的键名更改为更相关的内容,通常我在与我正在进行的项目相关的任何自定义元数据的开头添加2/3个字符,例如
ss_
。 - 将
$pageid
改为复数形式$pageIds
。 - 将
explode
与trim
结合使用,以确保用户不输入空格。
请注意,将元键名称更改为ss_child_page_ids
还需要您更新代码中存储此字段的其余部分,并且还将断开任何现有值的关联,因为您不再引用key
。
<?php
$pageIds = get_post_meta(get_the_ID(), 'ss_child_page_ids', true);
$pageIds = array_map('trim', explode(',', $pageIds ));
$args = array(
'post_type' => 'page',
'post__in' => $pageIds
);
并且$childrens
也可以更改为$children
,因为它已经是复数形式。
现在,$pageIds
应该是一个包含每个id项的数组,例如'1,2,3,4,5'
将变成['1', '2', '3', '4', '5']
。
英文:
Your $pageid
from the post_meta
is returning a string and array($pageid)
is only creating an array with a single element. You need to convert the string to an array using explode(...)
. I recommend instructing users to separate the ids by comma, such as 1,2,3,4,5
.
Then using the following should work:
'post__in' => explode(',', $pageid)
I also recommend a couple more tweaks:
- Change the key of your meta key to something more relevant, I usually add 2/3 characters at the start of any custom meta related to a project I'm working on, such as
ss_
. - Make
$pageid
plural to$pageIds
. - Combine the
explode
withtrim
to ensure the user enters no spaces.
Note that chaning the meta key name to ss_child_page_ids
would require you to also update the rest of your code where this field is being stored, and and will also unlink any existing values as you are no longer referencing key
.
<?php
$pageIds = get_post_meta(get_the_ID(),'ss_child_page_ids', true);
$pageIds = array_map('trim', explode(',', $pageIds ));
$args = array(
'post_type' => 'page',
'post__in' => $pageIds
);
And $childrens
can also be changed to $children
, it is already in plural form.
Now $pageIds
should be an array with each id item, such as '1,2,3,4,5'
will be ['1','2','3','4','5']
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论