Php 如何获取所有评论和附件?
通过运行如下查询,我可以获取所有帖子附件并将其显示在页面上:Php 如何获取所有评论和附件?,php,wordpress,Php,Wordpress,通过运行如下查询,我可以获取所有帖子附件并将其显示在页面上: $attachments = get_posts( array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ) ); if ( $attachments ) { foreach ( $attachment
$attachments = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
?>
<li><?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
<p><?php echo apply_filters( 'the_title', $attachment->post_title ); ?></p>
</li>
<?php
}
}
这将检查注释是否包含附件:
public function has_attachment( $comment_id = 0 ) {
if ( ! $comment_id ) {
$comment_id = get_comment_ID();
}
$attachment_id = $this->get_attachment_id( $comment_id );
if ( ! $attachment_id ) {
return false;
}
// Check the attachment exists.
if ( ! wp_get_attachment_url( $attachment_id ) ) {
return false;
}
return true;
}
这将为注释分配一个附件:
public function assign_attachment( $comment_id, $attachment_id ) {
$meta_key = $this->get_attachment_meta_key();
return update_comment_meta( $comment_id, $meta_key, $attachment_id );
}
最后,这里生成HTML标记,它显示带有附加图像的注释。在这段代码中使用DCO注释附件
,实际上给了我自己的想法:
public function get_attachment_preview( $attachment_id ) {
$url = wp_get_attachment_url( $attachment_id );
if ( ! $url ) {
return false;
}
$embed_type = $this->get_embed_type( $attachment_id );
switch ( $embed_type ) {
case 'image':
$thumbnail_size = $this->get_option( 'thumbnail_size' );
if ( is_admin() ) {
$thumbnail_size = 'medium';
}
$img = DCO Comment Attachment( $attachment_id, $thumbnail_size );
if ( ! is_admin() && $this->get_option( 'link_thumbnail' ) ) {
$full_img_url = wp_get_attachment_image_url( $attachment_id, 'full' );
$img = '<a href="' . esc_url( $full_img_url ) . '">' . $img . '</a>';
}
$attachment_content = '<p class="dco-attachment dco-image-attachment">' . $img . '</p>';
break;
case 'video':
$attachment_content = '<div class="dco-attachment dco-video-attachment">' . do_shortcode( '[video src="' . esc_url( $url ) . '"]' ) . '</div>';
break;
case 'audio':
$attachment_content = '<div class="dco-attachment dco-audio-attachment">' . do_shortcode( '[audio src="' . esc_url( $url ) . '"]' ) . '</div>';
break;
case 'misc':
$title = get_the_title( $attachment_id );
$attachment_content = '<p class="dco-attachment dco-misc-attachment"><a href="' . esc_url( $url ) . '">' . esc_html( $title ) . '</a></p>';
}
return $attachment_content;
}
公共函数获取附件预览($attachment\u id){
$url=wp\u get\u attachment\u url($attachment\u id);
如果(!$url){
返回false;
}
$embed\u type=$this->get\u embed\u type($attachment\u id);
交换机($embed\u类型){
案例“图像”:
$thumbnail\u size=$this->get\u选项('thumnail\u size');
if(is_admin()){
$缩略图大小='中等';
}
$img=DCO注释附件($Attachment\u id,$thumbnail\u size);
如果(!is_admin()&&$this->get_选项('link_缩略图')){
$full_img_url=wp_get_attachment_image_url($attachment_id,'full');
$img='';
}
$attachment_content='。$img.
';
打破
案例“视频”:
$attachment_content=''.do_快捷码('[video src=“'.esc_url($url)。'']');
打破
“音频”案例:
$attachment_content=''.do_快捷码('[audio src=“'.esc_url($url)。'']');
打破
“杂项”案例:
$title=获取标题($attachment\u id);
$attachment_content=';
}
返回$attachment_内容;
}
我想我明白了!:)我把它放在这里,以防它能帮助别人
为了完整起见:此解决方案与awesome插件DCO评论附件
配合使用,该插件允许用户在评论中添加附件(如图像或视频)
通过上面发布的代码,我在一个页面中显示所有注释。通过添加以下代码,通过插件添加的图像将显示相应的注释
我使用get_comment_meta()
从数据库中获取名为附件ID
的元值。插件将字段meta_值添加到数据库wp_commentmeta
。然后我将附件ID传递到wp\u get\u attachment\u image\u url()
,它获取附加到附件ID的图像的url
这是我现在的代码:
<div class="review-images">
<?php
//get meta_key 'attachment_id' and according meta_value from database wp_commentmeta
//where comment_id == $comment->comment_ID;
$attachment_id = get_comment_meta( $comment->comment_ID, 'attachment_id', true );
$image_url = wp_get_attachment_image_url( $attachment_id, 'full' );
// output image to screen
echo '<img loading="lazy" src="' . $image_url . '" alt="attachment image" /> ';
?>
</div>
默认wordpress不提供附件功能。因此,请首先解释您是如何在评论中添加附件的(提供代码)-这取决于如何在Front上获取附件。我正在使用免费插件DCO comment attachment
,它提供了附件功能。我已经浏览了代码,并在我的帖子中添加了一些代码。
public function get_attachment_preview( $attachment_id ) {
$url = wp_get_attachment_url( $attachment_id );
if ( ! $url ) {
return false;
}
$embed_type = $this->get_embed_type( $attachment_id );
switch ( $embed_type ) {
case 'image':
$thumbnail_size = $this->get_option( 'thumbnail_size' );
if ( is_admin() ) {
$thumbnail_size = 'medium';
}
$img = DCO Comment Attachment( $attachment_id, $thumbnail_size );
if ( ! is_admin() && $this->get_option( 'link_thumbnail' ) ) {
$full_img_url = wp_get_attachment_image_url( $attachment_id, 'full' );
$img = '<a href="' . esc_url( $full_img_url ) . '">' . $img . '</a>';
}
$attachment_content = '<p class="dco-attachment dco-image-attachment">' . $img . '</p>';
break;
case 'video':
$attachment_content = '<div class="dco-attachment dco-video-attachment">' . do_shortcode( '[video src="' . esc_url( $url ) . '"]' ) . '</div>';
break;
case 'audio':
$attachment_content = '<div class="dco-attachment dco-audio-attachment">' . do_shortcode( '[audio src="' . esc_url( $url ) . '"]' ) . '</div>';
break;
case 'misc':
$title = get_the_title( $attachment_id );
$attachment_content = '<p class="dco-attachment dco-misc-attachment"><a href="' . esc_url( $url ) . '">' . esc_html( $title ) . '</a></p>';
}
return $attachment_content;
}
<div class="review-images">
<?php
//get meta_key 'attachment_id' and according meta_value from database wp_commentmeta
//where comment_id == $comment->comment_ID;
$attachment_id = get_comment_meta( $comment->comment_ID, 'attachment_id', true );
$image_url = wp_get_attachment_image_url( $attachment_id, 'full' );
// output image to screen
echo '<img loading="lazy" src="' . $image_url . '" alt="attachment image" /> ';
?>
</div>