将自定义字段值传递到WordPress循环参数中的数组中应如何实现?

huangapple go评论60阅读模式
英文:

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 $pageidpost_meta中返回的是一个字符串,array($pageid)只创建了一个包含单个元素的数组。你需要使用explode(...)将字符串转换为数组。我建议指导用户用逗号分隔id,如1,2,3,4,5。然后使用以下代码应该可以工作:

'post__in' => explode(',', $pageid)

我还建议进行一些其他调整:

  • 将您的meta键的键名更改为更相关的内容,通常我在与我正在进行的项目相关的任何自定义元数据的开头添加2/3个字符,例如ss_
  • $pageid改为复数形式$pageIds
  • explodetrim结合使用,以确保用户不输入空格。

请注意,将元键名称更改为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:

&#39;post__in&#39; =&gt; explode(&#39;,&#39;, $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 with trim 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.

&lt;?php
$pageIds = get_post_meta(get_the_ID(),&#39;ss_child_page_ids&#39;, true);
$pageIds = array_map(&#39;trim&#39;, explode(&#39;,&#39;, $pageIds ));

$args = array(
    &#39;post_type&#39; =&gt; &#39;page&#39;, 
    &#39;post__in&#39;  =&gt; $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 &#39;1,2,3,4,5&#39; will be [&#39;1&#39;,&#39;2&#39;,&#39;3&#39;,&#39;4&#39;,&#39;5&#39;].

huangapple
  • 本文由 发表于 2023年2月6日 03:03:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定