Wordpress get_posts(显示所有附件)循环外分页

发布于 2024-12-28 10:02:07 字数 687 浏览 1 评论 0原文

在我的一个 Wordpress 页面(实际上是一个图像博客网站)上,我使用 masonry.js 和 Wordpress 函数 get_posts 将所有附件转储到我的博客文章中,并将它们显示在网格中。这很好用。然而,显然有很多图像,我希望使用 infinitescroll.js 来处理它。唯一的问题是循环外部的 get_posts 函数不保留分页,因此 infinitescroll.js 的功能不起作用。

这是我用来转储所有附件的代码:

<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null ); 
$attachments = get_posts( $args );
if ($attachments) {
    foreach ( $attachments as $post ) {
        setup_postdata($post);
        the_attachment_link($post->ID, true);
        the_excerpt();
    }
}
?>

是否有在循环外部向原始 Wordpress get_posts() 附件转储添加分页,或者有人能想到解决方案吗?

On one of my Wordpress pages (which is really an image blog site) I'm using masonry.js with the Wordpress function get_posts to dump all attachments to my blog posts and display them in a grid. This works fine. However, there's obviously a lot of images and I was hoping to use the infinitescroll.js with this. The only problem is that the get_posts function, outside the loop, doesn't retain the pagination and therefore the functionality of infinitescroll.js doesn't work.

Here is the code I am using to dump all the attachments:

<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null ); 
$attachments = get_posts( $args );
if ($attachments) {
    foreach ( $attachments as $post ) {
        setup_postdata($post);
        the_attachment_link($post->ID, true);
        the_excerpt();
    }
}
?>

Is there anyway of adding in pagination to the original Wordpress get_posts() attachment dump outside of the loop, or can anyone think of a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无语# 2025-01-04 10:02:07

我已经使用“offset”参数做了类似的事情来获取帖子。

基本上,每次新调用获取帖子时,只需将偏移量增加您每次要显示的新缩略图的数量即可。当返回的缩略图数量小于您的偏移量时,您已到达帖子末尾。

另一种解决方案是使用 Wp_Query 类的分页参数。请参阅此处了解这些内容。

I've done something similar using the 'offset' parameter for get posts.

Basically with each new call to get posts, simply increase your offset amount by the amount of new thumbnails you want to display each time. When the number of thumbnails returned is less than your offset amount, you have reached the end of your posts.

Another solution is to use the pagination parameters of the Wp_Query class. See here for what these are.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文