如何在 Wordpress 上通过评论 ID 获取 attachment_url 图片
How to get attachment_url image by comment id on Wordpress
您好,我正在使用插件 DCO Comment Attachment 来将图像添加到评论中,我使用下面的代码获取列表评论。现在我想在将其显示到屏幕之前获取要处理的评论图像 link。目前根据下面的代码,它只获取附加到评论 post 的所有图像文件,而不是每个评论。
我尝试将 $comment->comment_post_ID
改为 $comment->comment_ID
,但它不起作用。
谢谢大家!
<?php $comments = get_comments($param);?>
<?php foreach ($comments as $comment): ?>
<?php if ($comment->comment_approved != '0'): ?>
<?php
$attachments = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
'post_parent' => $comment->comment_post_ID,
));
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_url($attachment->ID);
}
}
?>
<?php endif;?>
<?php endforeach;?>
经过自学,我知道如何获取评论中的url图片,代码如下,这段代码放在functions.php文件中。
function get_attachment_url_image_comment($comment_id) {
$meta_key = 'attachment_id';
$attachment_id = get_comment_meta( $comment_id, $meta_key, true );
$full_img_url = wp_get_attachment_image_url( $attachment_id, 'full' );
return $full_img_url;
}
剩下要做的就是将所需的评论 ID 传递到函数中并获取附加图像
<?php $comments = get_comments($param);?>
<?php foreach ($comments as $comment): ?>
<?php if ($comment->comment_approved != '0'): ?>
<?php if(get_attachment_url_image_comment($comment->comment_ID)): ?>
<?php echo get_attachment_url_image_comment($comment->comment_ID) ?>
<?php endif; ?>
<?php endif;?>
<?php endforeach;?>
结果:"http://localhost/wp-content/uploads/2020/07/11439468-3x4-xlarge-2.jpg"
效果很好!
您好,我正在使用插件 DCO Comment Attachment 来将图像添加到评论中,我使用下面的代码获取列表评论。现在我想在将其显示到屏幕之前获取要处理的评论图像 link。目前根据下面的代码,它只获取附加到评论 post 的所有图像文件,而不是每个评论。
我尝试将 $comment->comment_post_ID
改为 $comment->comment_ID
,但它不起作用。
谢谢大家!
<?php $comments = get_comments($param);?>
<?php foreach ($comments as $comment): ?>
<?php if ($comment->comment_approved != '0'): ?>
<?php
$attachments = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
'post_parent' => $comment->comment_post_ID,
));
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_url($attachment->ID);
}
}
?>
<?php endif;?>
<?php endforeach;?>
经过自学,我知道如何获取评论中的url图片,代码如下,这段代码放在functions.php文件中。
function get_attachment_url_image_comment($comment_id) {
$meta_key = 'attachment_id';
$attachment_id = get_comment_meta( $comment_id, $meta_key, true );
$full_img_url = wp_get_attachment_image_url( $attachment_id, 'full' );
return $full_img_url;
}
剩下要做的就是将所需的评论 ID 传递到函数中并获取附加图像
<?php $comments = get_comments($param);?>
<?php foreach ($comments as $comment): ?>
<?php if ($comment->comment_approved != '0'): ?>
<?php if(get_attachment_url_image_comment($comment->comment_ID)): ?>
<?php echo get_attachment_url_image_comment($comment->comment_ID) ?>
<?php endif; ?>
<?php endif;?>
<?php endforeach;?>
结果:"http://localhost/wp-content/uploads/2020/07/11439468-3x4-xlarge-2.jpg"
效果很好!