wordpress远程附件_如何获取WordPress中除特色图片以外的所有帖子附件

wordpress远程附件

Recently while working on a custom project for a client, we had to get all post attachments from a custom post type and display them at one place. Because we were creating a grid display, we had each post’s featured image serving the purpose of a separator. This is why when getting all post attachments, we needed to exclude the featured image, so it doesn’t show up twice. In this article, we will show you how to get all post attachments in WordPress except for the featured image.

最近,在为客户处理自定义项目时,我们不得不从自定义帖子类型中获取所有帖子附件,并将它们显示在一个地方。 因为我们正在创建网格显示,所以每个帖子的特色图片都用作分隔符。 这就是为什么在获取所有帖子附件时,我们需要排除特色图片,因此它不会出现两次。 在本文中,我们将向您展示如何获取WordPress中除特色图片之外的所有帖子附件。

All you have to do is paste the following code inside a loop.

您所要做的就是将以下代码粘贴到循环中。


<?php if ( $post->post_type == 'data-design' && $post->post_status == 'publish' ) {
		$attachments = get_posts( array(
			'post_type' => 'attachment',
			'posts_per_page' => -1,
			'post_parent' => $post->ID,
			'exclude'     => get_post_thumbnail_id()
		) );

		if ( $attachments ) {
			foreach ( $attachments as $attachment ) {
				$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
				$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
				echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>';
			}
			
		}
	}
?>

The code above first checks if the post type is data-design and the post status is published. You may not need the first conditional depending on what you are trying to do. Then we simply run the get_posts query. Everything is pretty self explanatory there. The key that we must highlight is the exclude feature. That line is making sure that featured image does not show up. If you take that line away, then the featured image will show up. After specifying all the parameters, we simply specify what to do when each attachment is pulled. We are pulling attachment type for the class variable. Then the $thumbimg variable is simply using wp_get_attachment_link to pull the image at a specific thumbnail size, and it also hyperlinks the image to the single attachment pages. In the final step, we simply echo it.

上面的代码首先检查发布类型是否为数据设计,并且发布状态为发布。 根据您要执行的操作,您可能不需要第一个条件。 然后,我们只需运行get_posts查询。 那里的一切都很自我解释。 我们必须强调的关键是排除功能。 那条线确保没有显示特色图像。 如果您将那条线取走,则会显示精选图像。 指定所有参数后,我们只需指定拉出每个附件时的操作。 我们为类变量拉附件类型。 然后,$ thumbimg变量仅使用wp_get_attachment_link将图像拉至特定的缩略图大小,并且还将图像超链接到单个附件页面。 在最后一步,我们简单地回声它。

We ran this code inside a loop with a separate call for featured image which links to the individual post. Final outcome looked something like this:

我们在循环中运行此代码,并单独调用了链接到各个帖子的精选图片。 最终结果如下所示:

Attachment Grid

Each featured image served as an album identifying image which you can see. The grey spots were filled with the attachments for the post. We hope that this would help those who are looking to push WordPress beyond a blogging platform.

每个特色图像都充当一个相册识别图像,您可以看到它。 灰色的斑点充满了该职位的附件。 我们希望这将对那些希望将WordPress超越博客平台的人们有所帮助。

翻译自: https://www.wpbeginner.com/wp-themes/how-to-get-all-post-attachments-in-wordpress-except-for-featured-image/

wordpress远程附件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值