Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Pagination for foreach WordPress

  • SOLVED

I can not add a pagination. But if I add "page / 2 /" in the address bar I go well with the rest of my content. How to add links "next page" and "previous page".

Here is my current code (it will look for media in the library, the media have categories. I use the plugin "Media Categories")

<?php



$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'attachment',
'cat'=>'34',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null,
'posts_per_page' => 3,
'paged' => $paged
);


$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) :
setup_postdata($post);
echo wp_get_attachment_link( $id, 'featured' );

?>
<?php endforeach; ?>

<?php } ?>

<div class="navigation">
<div class="alignleft"><?php posts_nav_link('','','&laquo; Previous Entries') ?></div>
<div class="alignright"><?php posts_nav_link('','Next Entries &raquo;','') ?></div>
</div>

Answers (4)

2013-10-04

Arnav Joy answers:

try this

<?php


$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(

'post_type' => 'attachment',

'cat'=>'34',

'post_status' => null,

'post_parent' => null,

'posts_per_page' => 3,

'paged' => $paged

);



$wpq = new WP_Query($args);

$attachments = get_posts($args);

if ($wpq->have_posts()) {

while ($wpq->have_posts()) : $wpq->the_post();

// setup_postdata($post);

echo wp_get_attachment_link( $id, 'featured' );



?>

<?php endwhile; ?>



<?php } ?>



<div class="navigation">

<div class="alignleft"><?php next_posts_link( __( '&laquo; Previous Entries', 'wpq' ) ); ?></div>

<div class="alignright"><?php previous_posts_link( __( 'Next Entries &raquo;', 'wpq' ) ); ?></div>

</div>


Robin Ferrari comments:

This is not a "custom post types" called "attachment", looked at my code and tried (removing the 'cat' => '34 '). You will see that it displays all the images in the library.


Arnav Joy comments:

try removing

'numberposts' => -1,

from your code


Robin Ferrari comments:

Don't work…


Robin Ferrari comments:

But if you have an other solution. I just want list all my picture in library and have pagination....


Arnav Joy comments:

try this

it is working on my system

<?php





$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;



$args = array(



'post_type' => 'attachment',

'post_mime_type' =>'image',

'post_status' => 'inherit',



'posts_per_page' => 3,



'paged' => $paged



);



query_posts($args);



if (have_posts()) {



while (have_posts()) : the_post();


echo wp_get_attachment_link( get_the_ID(), 'thumbnail' );


?>



<?php endwhile; ?>



<div class="navigation">



<div class="alignleft"><?php next_posts_link( __( '&laquo; Previous Entries', 'wpq' ) ); ?></div>



<div class="alignright"><?php previous_posts_link( __( 'Next Entries &raquo;', 'wpq' ) ); ?></div>



</div>




<?php } ?>


Robin Ferrari comments:

Thanx a lot it's working


Arnav Joy comments:

Glad to hear that , please vote now and close the question.


Robin Ferrari comments:

I can't vote:

"
3 groups of people are allowed to vote:

The true elite who've earned 1% of all winnings on the site.
The top monthly experts.
The admins of this site.

All votes are public.

If you have already voted, you can not vote again.

Once an answer receives more downvotes than upvotes, then it is no longer displayed on the question page.

You are not allowed to vote.

"


Arnav Joy comments:

check this link to understand how you can vote

http://blog.tailormadeanswers.com/2011/04/22/voting-assign-prize-money/

2013-10-04

Navjot Singh answers:

Replace by

<div class="navigation">
<div class="alignleft"><?php posts_nav_link('','','&laquo; Previous Entries') ?></div>
<div class="alignright"><?php posts_nav_link('','Next Entries &raquo;','') ?></div>
</div>


by

<div class="navigation">
<div class="alignleft"><?php previous_posts_link('&laquo; Previous Entries') ?></div>
<div class="alignright"><?php next_posts_link('Next Entries &raquo;','') ?></div>
</div>


Source: http://codex.wordpress.org/Function_Reference/posts_nav_link The code you posted shouldn't be used.


Robin Ferrari comments:

I've tried before but its not working.

2013-10-04

Balanean Corneliu answers:

Try this http://codex.wordpress.org/Next_and_Previous_Links


Balanean Corneliu comments:

<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}

$current = array_search(get_the_ID(), $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>

<div class="navigation">
<?php if (!empty($prevID)) { ?>
<div class="alignleft">
<a href="<?php echo get_permalink($prevID); ?>"
title="<?php echo get_the_title($prevID); ?>">Previous</a>
</div>
<?php }
if (!empty($nextID)) { ?>
<div class="alignright">
<a href="<?php echo get_permalink($nextID); ?>"
title="<?php echo get_the_title($nextID); ?>">Next</a>
</div>
<?php } ?>
</div><!-- .navigation -->


Robin Ferrari comments:

Don't work


Robin Ferrari comments:

Where do I put the code? Do I have to put in place of the code below?
<div class="navigation">

<div class="alignleft"><?php posts_nav_link('','','&laquo; Previous Entries') ?></div>

<div class="alignright"><?php posts_nav_link('','Next Entries &raquo;','') ?></div>

</div>


If it's that it's doesn't work...


Balanean Corneliu comments:

Change it with <?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-above" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyten' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
</div><!-- #nav-above -->
<?php endif; ?>


Balanean Corneliu comments:

Or you can do this with the plugin : http://wordpress.org/support/view/plugin-reviews/wp-pagenavi


Robin Ferrari comments:

Test before please. No solution is working or i put the code in the wrong place.


Robin Ferrari comments:

i have tried wp page navi like that:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'attachment',
'cat'=>'34',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null,
'posts_per_page' => 3,
'paged' => $paged
);


$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) :
setup_postdata($post);
echo wp_get_attachment_link( $id, 'featured' );

?>
<?php endforeach; ?>
<div class="navigation"><p><?php wp_pagenavi(); ?></p></div>
<?php } ?>


But dont work...


Balanean Corneliu comments:

Can you give us a link, or send me in PM if you whant some acces information to take a look.


Robin Ferrari comments:

http://abouchar.zeckart.com/archives-media/


Balanean Corneliu comments:

Try this plugin i think is the solution for your question:

http://onesmallpixel.com/plugin/better-gallery-shortcode/

2013-10-04

Hariprasad Vijayan answers:

Hello,

Try this,

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'attachment', 'cat'=>'34', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null, 'posts_per_page' => 3, 'paged' => $paged );
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php echo wp_get_attachment_link( get_the_ID(), 'featured' ); ?>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php posts_nav_link('','','&laquo; Previous Entries') ?></div>
<div class="alignright"><?php posts_nav_link('','Next Entries &raquo;','') ?></div>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Let me know.