为帖子的特色图片添加额外的字段

时间:2017-04-12 作者:Michelle

我想在特色图片中添加一些额外的字段,并将它们放在网站的HTML输出中。现在有alt文本、标题等字段。我希望能够为添加一个输入框data-pin-description 并将其输出到图像,如data-pin-description="PIN DESCRIPTION GOES HERE"

因此,它将输出到特征图像中,如:

<img src="smiley.gif" data-pin-description="PIN DESCRIPTION GOES HERE" alt="Smiley face" height="42" width="42">

有没有办法做到这一点?

1 个回复
SO网友:ciaika

在您的情况下,需要将自定义字段添加到元数据库中。过滤器的完整参考代码如下:“admin\\u post\\u thumbnail\\u html”https://developer.wordpress.org/reference/hooks/admin_post_thumbnail_html/.

让我们添加一个图像标题:

<?php
add_filter(\'admin_post_thumbnail_html\', \'custom_featured_image_field\', 10, 2);
function custom_featured_image_field($content, $post_id) {
    $custom_name    = ‘custom_featured_image_title’;
    $custom_value = esc_attr(get_post_meta($post_id, $custom_name, true));

    $output = sprintf(
        ‘<br><input type=“text” id="%1$s" name="%1$s" value="%2$s">\',
        $custom_name, $custom_value
    );

    return $content .= $output;
}?>

Let’s save the newly added custom field

在这里,我们将清理用户数据并保存或更新自定义特征图像标题字段。

<?phpadd_action(\'save_post\', \'save_custom_featured_image_field\', 10, 3);
function save_custom_featured_image_field($post_ID, $post, $update) {
    $custom_name    = \'custom_featured_image_title\';
    $custom_value = isset($_REQUEST[ $custom_name ]) ? 1 : 0;
    update_post_meta($post_ID, $custom_name, $custom_value);
}?>

相关推荐