如何在自动精选图片帖子中添加alt标签?



我是PHP的初学者,所以很抱歉,如果我的问题听起来很愚蠢。

因此,我正在使用一个名为"自动特色图像帖子"的WordPress插件。它在从新上传的图像创建草稿帖子方面做得很好。除了插件之外,我还将此过滤器添加到函数.php文件中,该文件将图像添加到帖子中:

add_filter( 'afip_new_post_content', 'myprefix_change_afip_post_content', 10, 2 );
/* Grabs the image source for the newly created image and inserts it
* into the new post content along with a one line paragraph. */
function myprefix_change_afip_post_content( $post_content, $attachment_id ) {
$my_uploaded_image = wp_get_attachment_image_src( $attachment_id );
$post_content = '<p>This is my new uploaded image....</p>';
$post_content .= '<img src="' . $my_uploaded_image[0] . '">';
return $post_content;
}

所以,我的问题是 - 如何在此代码中添加替代图像?替代图像应与新帖子的标题相同。我必须为此制作另一个功能吗?我知道我要做的第一件事就是将 alt=" 添加到标签中。但是,如何让帖子的标题出现在 alt 中呢?这是插件代码的链接 - 在此处输入链接说明

正如承诺的那样,图像的"alt"文本以字符串形式存储,wp_postmeta meta_key为"_wp_attachment_image_alt"。

您可能已经知道,您可以使用简单的get_post_meta((加载它,如下所示:

$alt_text = get_post_meta($post->ID, '_wp_attachment_image_alt', true);

最新更新