WordPress 从媒体库添加 PDF 列表
我一直在尝试想出一种方法来添加 WordPress 媒体库中的 PDF 列表。它用于新闻通讯列表,从最新的新闻通讯开始,列出大约 5 个。我希望它列出一个标题,并且该标题是文档的超链接。因此,当访问者单击链接时,它将下载 PDF。
我尝试过“get_posts”路线,但根本无法弄清楚。目前我正在做的是将 PDF 插入到“时事通讯”类别的帖子中,然后使用单独的循环将它们拉到页面上。我几乎已经可以正常工作了,但它没有正确链接。所以我希望得到一些帮助。
到目前为止,我的代码如下所示:
<h3>Newsletters</h3>
<ul>
<?php query_posts('category_name=newsletter&posts_per_page=5'); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php echo wp_get_attachment_url(the_ID()); ?>" title="<?php echo esc_attr( get_the_title() ); ?>" rel="attachment">
<?php echo esc_attr( get_the_title() ); ?></a>
</li>
<?php endwhile; ?>
</ul>
我似乎无法正确理解的部分是:
I've been trying to come up with a way to add a list of PDF's from the Media Library in WordPress. It's for a list of newsletters which will start with the most recent, and list about 5. I want it to list with a title and that title be a hyperlink to the document. So when a visitor clicks the link it will download the PDF.
I have tried the "get_posts" route but can't figure it out at all. Currently what I'm doing is inserting the PDF's into a post with a category of "newsletter" and then having a separate loop to pull them up on the page. I've almost got this working, but it's not linking properly. So I was hoping for some help.
My code so far looks like this:
<h3>Newsletters</h3>
<ul>
<?php query_posts('category_name=newsletter&posts_per_page=5'); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php echo wp_get_attachment_url(the_ID()); ?>" title="<?php echo esc_attr( get_the_title() ); ?>" rel="attachment">
<?php echo esc_attr( get_the_title() ); ?></a>
</li>
<?php endwhile; ?>
</ul>
The part I just can't seem to get right is:<?php echo wp_get_attachment_url(the_ID()); ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您上传到媒体库的唯一 PDF 是新闻通讯,那么您可以完全绕过此步骤并查询 5 个最新的 PDF ?
您目前遇到的问题是您应该将附件的ID传递给
wp_get_attachment_url()
,而不是发布ID。If the only PDFs you upload to your media library are the newsletter, then you can by-pass this step altogether and query the 5 most recent PDFs?
The trouble you're having at the moment is you should be passing the ID of the attachment to
wp_get_attachment_url()
, not the post ID.