【问题标题】:How to add featured_image field on all existing posts containing the image如何在包含图像的所有现有帖子上添加 features_image 字段
【发布时间】:2019-12-17 09:45:03
【问题描述】:

我有超过数千个帖子,每个帖子都包含一张特色图片。 由于某些原因,我需要创建一个包含特色图片网址的特色图片自定义字段。

现在是否有可能,或者唯一的方法是在帖子发布之前创建它?

【问题讨论】:

  • 为什么需要为特色图片自定义字段?不能用默认的吗?
  • 因为调用特色图片url的正常代码很长,而我需要用它设置...所以唯一的方法是从自定义字段中获取img url

标签: php wordpress custom-fields


【解决方案1】:

您可以为该帖子类型设置自定义字段,然后您可以使用循环更新所有帖子的元字段并设置特征图像。如下所示

$loop = new WP_Query( array(
    'post_type' => 'Property',
    'posts_per_page' => -1
  )
);
while ( $loop->have_posts() ) : $loop->the_post();

    $imageurl = get_the_post_thumbnail_url(get_the_ID(),'full');
    update_post_meta( get_the_ID(), 'custom_featured_image', $imageurl  );

endwhile; wp_reset_query();

【讨论】:

  • 所以总而言之,我只需要创建一个专用于该目的的自定义字段名称,然后只需将上述代码添加到 functions.php 文件中,它就会在不需要启动任何命令的情况下运行转换?
  • 您需要替换 update_post_meta 中的键并在 WP 查询中发布类型值,然后才能正常工作
  • 你能告诉我怎么做吗?抱歉,我不太习惯这些代码,我是新手 :)
猜你喜欢
  • 1970-01-01
  • 2021-02-17
  • 2014-06-12
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 2021-02-10
  • 2013-10-26
  • 1970-01-01
相关资源
最近更新 更多