简单的 php 图像数组,来自 WordPress 自定义字段

发布于 2024-11-29 06:39:02 字数 1028 浏览 1 评论 0原文

我正在尝试对一堆图像使用 1 个自定义字段 - 对所有图像执行相同的操作。我可以将它们存储在自定义字段中,但是建议这样做,但我认为这种格式是最好的,因为我认为这就是 PHP 数组的内容:

'http://images.domain.com/image1-Th.jpg',
'http://images.domain.com/image1-Th.jpg',
'http://images.domain.com/image3-Th.jpg'

所以,一旦我为帖子输入了自定义字段值,这是我的非-正在运行的 PHP 代码:

<?php //og images
    $ogimagepre = '<meta property="og:image" content="';
    $ogimagepost = '"/>';
    global $wp_query; $postID = $wp_query->post->ID;
    $photosfull = array(get_post_meta($postID, 'custom_field_name', true));
    echo $ogimagepre.$photosfull.$ogimagepost
?>

您可以看到我正在尝试获得此结果:

<meta property="og:image" content="http://images.domain.com/image1-Th.jpg"/>
<meta property="og:image" content="http://images.domain.com/image2-Th.jpg"/>
<meta property="og:image" content="http://images.domain.com/image3-Th.jpg"/>

这是第 1 步。理想情况下,我能够使用同一个数组做其他事情。例如将“-Th.jpg”替换为“-X3.jpg”,因为这是同一图像的更大尺寸。和其他东西;需要先通过Step1。

谢谢!

I'm trying to use 1 custom field for a bunch of images - to do the same thing to all the images. I can store them in the custom field however is advisable, but I thought this format would be best, since I think that's what a PHP array goes into:

'http://images.domain.com/image1-Th.jpg',
'http://images.domain.com/image1-Th.jpg',
'http://images.domain.com/image3-Th.jpg'

So, once I have my custom field values entered for a post, here's my non-working PHP code:

<?php //og images
    $ogimagepre = '<meta property="og:image" content="';
    $ogimagepost = '"/>';
    global $wp_query; $postID = $wp_query->post->ID;
    $photosfull = array(get_post_meta($postID, 'custom_field_name', true));
    echo $ogimagepre.$photosfull.$ogimagepost
?>

You can see I'm trying to get this result:

<meta property="og:image" content="http://images.domain.com/image1-Th.jpg"/>
<meta property="og:image" content="http://images.domain.com/image2-Th.jpg"/>
<meta property="og:image" content="http://images.domain.com/image3-Th.jpg"/>

That's Step1. Ideally, I'd be able to do other things using the same array. Such as replace "-Th.jpg" with "-X3.jpg", since that's a larger size of the same image. And other stuff; need to get past Step1 first.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

亚希 2024-12-06 06:39:02

我遇到了类似的问题,我希望单个元键下的图像作为唯一元素返回。试试这个:

$ogimagepre = '<meta property="og:image" content="';
$ogimagepost = '"/>';
global $wp_query; $postID = $wp_query->post->ID;
$photos =get_post_meta($postID, 'custom_field_name', true);

foreach ($photos as $photo){

    echo $ogimagepre.$photo.$ogimagepost
}

I had a similar problem where I wanted images under a single meta key to be returned as unique elements. Try this:

$ogimagepre = '<meta property="og:image" content="';
$ogimagepost = '"/>';
global $wp_query; $postID = $wp_query->post->ID;
$photos =get_post_meta($postID, 'custom_field_name', true);

foreach ($photos as $photo){

    echo $ogimagepre.$photo.$ogimagepost
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文