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

Paginate this post query. WordPress

  • SOLVED

I am trying to create a theme page template that basically will work as a site map for my site.

For some reason, this code doesn't show 25 post like I thought it would, and I can't get it to paginate.

<?php
/*
Template Name: Sitemap Extreme
*/
foreach( get_post_types( array('public' => true) ) as $post_type ) {
if ( in_array( $post_type, array('post','page','attachment') ) )
continue;

$pt = get_post_type_object( $post_type );

echo '<h2>'.$pt->labels->name.'</h2>';
echo '<ul>';
query_posts( array( 'post_type' => $post_type, 'paged' => get_query_var('page') ) );
//query_posts('post_type='.$post_type.'&posts_per_page=25');
while( have_posts() ) {
the_post();
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
}

echo '</ul>';
}
?>

Answers (4)

2012-01-04

Gabriel Reguly answers:

Hi scotthack,

Something in your code looks wrong, how about removing this


if ( in_array( $post_type, array('post','page','attachment') ) )
continue;


So it would be like this:

--edit:added pagination


<?php
/*
Template Name: Sitemap Extreme
*/
foreach( get_post_types( array('public' => true) ) as $post_type ) {
$pt = get_post_type_object( $post_type );
echo '<h2>'.$pt->labels->name.'</h2>';
echo '<ul>';
$page = (get_query_var('page')) ? get_query_var('page') : 1;
// get 25 posts per page
query_posts('post_type='.$post_type.'&posts_per_page=25&paged=' . $page);
while( have_posts() ) {
the_post();
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
}
echo '</ul>';
}
?>


Regards,
Gabriel


Scott Hack comments:

Gabriel,

Thanks for taking an interest. I think that code was there to only show a specific taxonomy of posts that I want to show. I've also tried to play around with...
wp_link_pages(); to paginate the results, and I'm not having any luck.


Gabriel Reguly comments:

Hi scotthack,

Sorry, I understood that the sitemap feature was working and just pagination was the issue.

Why not try something like this plugin [[LINK href="http://wordpress.org/extend/plugins/html-sitemap/"]]http://wordpress.org/extend/plugins/html-sitemap/[[/LINK]]?

Regards,
Gabriel


Scott Hack comments:

That plugin will not work, as these are posts in a custom taxonomy that I am trying to display.

Your second code is paginating - but it is now displaying all posts types, pages etc instead of just the custom taxonomy like my original code was showing.


Gabriel Reguly comments:

Hi scotthack,

Glad to hear that pagination is working, then you only need to get back your first lines as in:


<?php
/*
Template Name: Sitemap Extreme
*/
foreach( get_post_types( array('public' => true) ) as $post_type ) {
if ( in_array( $post_type, array('post','page','attachment') ) )
continue;
$pt = get_post_type_object( $post_type );
echo '<h2>'.$pt->labels->name.'</h2>';
echo '<ul>';
$page = (get_query_var('page')) ? get_query_var('page') : 1;
// get 25 posts per page
query_posts('post_type='.$post_type.'&posts_per_page=25&paged=' . $page);
while( have_posts() ) {
the_post();
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
}
echo '</ul>';
}
?>


Hopefully that will be what you are looking for.

I'll be offline, time to sleep now. Tomorrow I can help you again, if needed.

Regards,
Gabriel


Scott Hack comments:

Gabriel,

Your code works great above! It is indeed paginating, but there are no pagenation ( is that a word?? ) controls. Something like <next> and <previous> or Page 1 of 12 etc.

Thoughts?


Gabriel Reguly comments:

Hi scotthack,

Please try this code for pagination controls (source: [[LINK href="http://codex.wordpress.org/Function_Reference/paginate_links"]]http://codex.wordpress.org/Function_Reference/paginate_links[[/LINK]])


<?php
/*
Template Name: Sitemap Extreme
*/
foreach( get_post_types( array('public' => true) ) as $post_type ) {
if ( in_array( $post_type, array('post','page','attachment') ) )
continue;
$pt = get_post_type_object( $post_type );
echo '<h2>'.$pt->labels->name.'</h2>';
echo '<ul>';
$page = (get_query_var('page')) ? get_query_var('page') : 1;
// get 25 posts per page
query_posts('post_type='.$post_type.'&posts_per_page=25&paged=' . $page);
while( have_posts() ) {
the_post();
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
}
echo '</ul>';
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => $page,
'total' => $wp_query->max_num_pages
) );

}


Regards,
Gabriel


Scott Hack comments:

Gabriel,

That did indeed add links for next and previous etc -- but they are linking correctly. This is the page I am using the code on, so you can see what I mean.

http://www.louisvillecondosandlofts.com/listing-sitemap

That works...

http://www.louisvillecondosandlofts.com/listing-sitemap/2 works.
http://www.louisvillecondosandlofts.com/listing-sitemap/page/2 does not work.

Thoughts?


Gabriel Reguly comments:

Hi scotthack,

Seems we have 2 issues with the code.

'format' => '?paged=%#%',
'current' => $page,

Would you mind giving me access to your site?

If not, please send me login details via PM.

Regards,
Gabriel


Gabriel Reguly comments:

Hi scotthack,

Thanks for site access, code is fixed now.

$page was being used somewhere else and being set to 1, so I changed the variable name to $my_page.

Also $base, from arguments was plain wrong and simply removing it solved the issue.


Follows amended code, for the record and benefit of other users of the site:

<?php
/*
Template Name: Sitemap Extreme
*/
foreach( get_post_types( array('public' => true) ) as $post_type ) {
if ( in_array( $post_type, array('post','page','attachment') ) )
continue;
$pt = get_post_type_object( $post_type );
echo '<h2>'.$pt->labels->name.'</h2>';
echo '<ul>';
$my_page = (get_query_var('page')) ? get_query_var('page') : 1;
// get 25 posts per page
query_posts('post_type='.$post_type.'&posts_per_page=25&paged=' . $my_page);
while( have_posts() ) {
the_post();
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
}
echo '</ul>';
global $wp_query;
$big = 999999999; // need an unlikely integer
$args = array(
'format' => '?page=%#%',
'current' => $my_page,
'total' => $wp_query->max_num_pages);
echo paginate_links( $args );
}
?>


Please do not forget to vote for me. ;-)

[[LINK href="http://wpquestions.com/question/pickAWinner/id/3649"]]Vote to award prize [[/LINK]]

Regards,
Gabriel


2012-01-04

Arnav Joy answers:

try this

<?php

$firma_terms = get_the_terms( 0, 'yourtermname' );

if ( $firma_terms )

$firma_term_slugs = wp_list_pluck( $firma_terms, 'slug' );



$args = array(

'post_type' => 'ansatte',

'order' => 'ASC',

'posts_per_page' => 25,

'tax_query' => array(

array(

'taxonomy' => 'yourtermname',

'field' => 'slug',

'terms' => $firma_term_slugs

)

)

);?>



<?php $my_query = new WP_Query(); $my_query->query($args); ?>



<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></h3>


<?php wp_reset_postdata(); ?>

<?php endwhile; ?>


Arnav Joy comments:

try this , it is your edited code

<?php

/*

Template Name: Sitemap Extreme

*/

foreach( get_post_types( array('public' => true) ) as $post_type ) {

if ( in_array( $post_type, array('post','page','attachment') ) )

continue;



$pt = get_post_type_object( $post_type );



echo '<h2>'.$pt->labels->name.'</h2>';

echo '<ul>';
$paged = (get_query_var('page')) ? get_query_var('page') : 1;

wp_reset_query();

query_posts( array( 'post_type' => $post_type, 'showposts=25', 'paged' => $paged ) );

//query_posts('post_type='.$post_type.'&posts_per_page=25');

while( have_posts() ) {

the_post();

echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';

?>

<div id="nav-below" class="navigation">
<div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentyten' ) . '</span> %title' ); ?></div>
<div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'twentyten' ) . '</span>' ); ?></div>
</div>

<?php

}
echo '</ul>';

}

?>

2012-01-04

Julio Potier answers:

Please use CODE tags.

2012-01-04

ej_emman answers:

Hello scotthack,


I understand your problem I encounter this problem before. I edited your version and you can try this.



<?php

/*

Template Name: Sitemap Extreme

*/

foreach( get_post_types( array('public' => true) ) as $post_type ) {

if ( in_array( $post_type, array('post','page','attachment') ) )

continue;



$pt = get_post_type_object( $post_type );



echo '<h2>'.$pt->labels->name.'</h2>';

echo '<ul>';

query_posts( array( 'post_type' => $post_type, 'paged' => get_query_var('paged') ) );

//query_posts('post_type='.$post_type.'&posts_per_page=25');

while( have_posts() ) {

the_post();

echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';

}



echo '</ul>';

}

?>


Hope this help...