Wordpress get_posts (show all attachments) pagination outside of the loop - wordpress

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?

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.

Related

wordpress count image attachments

I'm looking for some help, for some reason this code isn't working when trying to display total image attachments on the main index.php page.
// Get all the attachments
$attachments = get_posts ( array(
'numberposts' => -1,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => get_the_ID(),
'post_status' => 'inherit',
) );
// Count all the attachments
$total = count( $attachments );
The problem is, the loop is in the main index.php page, but it's calling a post.php template so I have placed this code there instead (I'm assuming that's still in the loop?) I am then calling $total just below it where I want to show "View all # images".
Any ideas why this is just displaying the number as 0 even though I have added an image gallery to the post using the basic Wordpress gallery media library?
Thanks
The attachment count only increases if it's attached to a post. You can force the count by adding this:
$wp_taxonomies['category']->update_count_callback = '_update_generic_term_count';
in a function in your functions.php something like this:
function change_category_arg() {
global $wp_taxonomies;
if ( ! taxonomy_exists('category') )
return false;
$wp_taxonomies['category']->update_count_callback = '_update_generic_term_count';
}
add_action( 'init', 'change_category_arg' );
Explanation:
This is significant in the case of attachments. Because an attachment
is a type of post, the default _update_post_term_count() will be used.
However, this may be undesirable, because this will only count
attachments that are actually attached to another post (like when you
insert an image into a post). This means that attachments that you
simply upload to WordPress using the Media Library, but do not
actually attach to another post will not be counted. If your intention
behind associating a taxonomy with attachments was to leverage the
Media Library as a sort of Document Management solution, you are
probably more interested in the counts of unattached Media items, than
in those attached to posts. In this case, you should force the use of
_update_generic_term_count() by setting '_update_generic_term_count' as the value for update_count_callback.
from Wordpress Codex on register_taxonomy

Update all posts - wordpress

I have looked at this thread and tried to implement the given code sample there;
//code snippet to mass update all posts
add_action('init','mass_update_posts');
function mass_update_posts(){
$all_posts = get_posts('numberposts=');
$my_posts = get_posts( array('post_type' => 'post', 'numberposts' => $all_posts ) );
foreach ( $my_posts as $my_post ):
wp_update_post( $my_post );
endforeach;
}
I put the code in my footer.php but it doesn't seem to do anything? What am I missing? Initially I used;
$my_posts = get_posts( array('post_type' => 'post', 'numberposts' => -1 ) );
But that didn't help either...
Let me see if I understand what you are trying to do with this code...
First every time someone loads the page the footer.php will fire so you want to mass update all of your posts with a loop of the post itself?
There about a million things wrong with what you are trying to do with this code.
Never add actions in a footer file they belong in the function.php file of a theme.
Your $all_posts variable is probably empty because you are sending a function expecting an array of arguments a string (please read the get_post() function documentation)
$all_posts is not an integer as you are using it the next line (the get_post() function returns a list of WP_Post objects.
Your loop goes through all your posts and updates them with the same post, changing nothing and effectively accomplishing nothing.
So I guess the real question is what exactly are you trying to accomplish?

Wordpress twitter bootstrap image carousel from specific category

I'm using the Bootstrap theme from 320press and currently struggling with the image slider carousel. It displays the posts of every featured image on the site, when I just want a specific category. I guess it's the following code I have to tweak som way...?
<?php
global $post;
$tmp_post = $post;
$show_posts = of_get_option('slider_options');
$args = array( '2' => $show_posts ); // set this to how many posts you want in the carousel
$myposts = get_posts( $args );
$post_num = 0;
foreach( $myposts as $post ) : setup_postdata($post);
$post_num++;
$post_thumbnail_id = get_post_thumbnail_id();
$featured_src = wp_get_attachment_image_src( $post_thumbnail_id, 'wpbs-featured-carousel' );
?>
Try replacing the $args for get_posts() with the following. You'll need to put in the slug of the category that you want to show. Since I don't know exactly how this plugin works, I can't guarantee it'll work as expected.
$args = array(
'posts_per_page' => $show_posts,
'category_name' => 'your_category_slug'
);
You can see a complete list of the arguments available for get_posts() on the WP_Query page of the WordPress codex: http://codex.wordpress.org/Class_Reference/WP_Query.
I'm a little confused by the counter that your code is setting. It seems like setting the number of posts to fetch as an argument to get_posts() would make a lot more sense.

List most recent posts and pages

I'm making my own template for my portfolio. This is my first from scratch up theme in Wordpress so bear over with me, if I don't talk the lingo or know what things are called... ;-)
I've made a frontpage where I want to loop through the last posts in category "Featured" and the last pages with parent page "Cases", limited with 5.
I know how to loop through a list of category posts, but how do I combine that with latest pages?
Here is a loop to get posts that are a child of:
$args = array(
'post_type' => 'page',
'numberposts' => 5,
'post_status' => 'publish',
'post_parent' => 33, // change this to the ID of the page you need
);
$posts = get_posts($args);
if ($posts) {
foreach ($posts as $post) {
setup_postdata($post);
// Your PHP code here.
}
}
All of the Wordpress post getting functions will return the posts in an array. If you want to "combine" them you can either do an array_merge and put the posts from the category loop and the page loop in the same array and iterate through them, or you can do multiple foreach or while loops.

enabling pagination inside wordpress post

i have a specific single page which display a single post.
the thing is that i want to display below it all the other posts which have the same special meta data and i made that to work as well.
the problem starts when i try to make pagination to the list of the posts below.
the single post url is something like that:
blog.com/somepost
and the pagination link to the second page of posts below looks someting like this
blog.com/somepost/page/2
and wordpress automatically redirects me back to
blog.com/somepost
how can i prevent it from redirecting me back?
btw, i"m using something like that:
i"m doing something like this:
while( have_posts() ): the_post();
//here printing the single post
endwhile;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'meta_key' => '_btree_project_id',
'meta_value' => $post->ID,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 8
);
$temp = $wp_query;
$wp_query = new WP_Query( $args );
while( have_posts() ): the_post();
//looping through the related posts here
endwhile;
The reading I've done about WordPress pagination gives me the impression that it is an imperfect feature. It requires the global var $wp_query which stems from the WP_Query object. WP_Query holds the global $wp_query which is necessary for making even basic pagination work. Custom queryies don't have access to $wp_query, nor do they own a var to control pagination. I assume you are using a custom query to grab that single post, and as this article points out, with custom queryies:
the “fix” is to trick WordPress into
using the global $wp_query variable
when using our own custom loops.
The article gives an example of utiilizing the global var in your custom query so that you have access to the query_vars that make pagination possible.
I expect that your permalink structure and a custom query that I'm guessing you are using might not be working because the global $wp_query var isn't available during your loop to show related posts.
What does your code to get, display, and paginate the related posts look like? Can you post?

Resources