Ask your WordPress questions! Pay money and get answers fast! (more info)

Display all Posts from Taxononmy with Pagination WordPress

  • SOLVED

I have a post type called "Speakers" and a taxonomy called "Celebrities"

I have my taxonomy-celebrities.php file that displays posts from a term within the Celebrities taxonomy.

What I need is a page called "Celebrities" and this page would display all of the posts within that taxonomy (all celebrity terms).

I am looking to get 6 posts per page and also have pagination.

What is the best way to accomplish this?

Answers (2)

2012-12-09

Dbranes answers:

Hi here is a page template tpl_celebriets.php with pagination links.

<?php
/*
Template Name: Celebrities
*/
get_header(); ?>

<div id="content">

<h1 class="page-title">
<?php the_title(); ?>
</h1>

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>

<?php if ( have_posts() ) :

$temp = $wp_query;
$wp_query = null;

$my_query_args=array(
'post_type'=>'speakers',
'posts_per_page' => 6,
'offset' => 0,
'orderby'=>'date',
'order'=>'DESC',
'paged'=>$paged,
'tax_query' => array(
array(
'taxonomy' => 'celebrities',
'field' => 'slug',
'terms' => 'sweden'
)
)
);

$wp_query = new WP_Query($my_query_args);

while ($wp_query->have_posts()) : $wp_query->the_post();?>
<div id="post-<?php the_ID(); ?>" <?php post_class();?>>
<h2 class="entry-title"><?php the_title();?></h2>
<p><?php the_content();?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>


<div class="pagination" id="pg">
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => __('&laquo; Previous'),
'next_text' => __('Next &raquo;')
));
?>
</div>

<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>

</div><!-- end #content-->

<?php get_footer(); ?>


you can then adjust "$my_query_args" to your needs.

cheers

<strong>update:</strong>

if you want all the posts in the speakers post type use:


$my_query_args=array(
'post_type'=>'speakers',
'posts_per_page' => 6,
'offset' => 0,
'orderby'=>'date',
'order'=>'DESC',
'paged'=>$paged,
);





Anthony Moore comments:

In your first solution, how would I get post from all the terms from the taxonomy "celebrities" and not just "sweden"?


Dbranes comments:

I guess you mean like this one:


$my_query_args=array(
'post_type'=>'speakers',
'posts_per_page' => 6,
'offset' => 0,
'orderby'=>'date',
'order'=>'DESC',
'paged'=>$paged,
'taxonomy_name'=>'celebrities'
);


Anthony Moore comments:

Sweet. For the pagination on my other pages I have it set up like so "Page 1 of 10 < >" With the "<" being an icon for previous posts and ">" being an icon for next post.

My function to do that is:

function prev_blog_page() {
$prev_page = get_previous_posts_link('Previous');
if ($prev_page ):
echo '<span class="left prev">'. $prev_page .'</span>';
else :
echo '<span class="left prev-disabled"></span>';
endif;
}


function next_blog_page() {
$next_page = get_next_posts_link('Next');
if ($next_page):
echo '<span class="next left">'. $next_page .'</span>';
else :
echo '<span class="left next-disabled"></span>';
endif;
}


Am I able to replicate something similar using the paginate_links() function?


Anthony Moore comments:

And then in my page template I have

<div id="pagination">
<p><span class="left"><?php echo 'Page '. ( get_query_var('paged') ? get_query_var('paged') : 1 ) . ' of ' . $wp_query->max_num_pages; ?></span><?php prev_blog_page(); ?><?php next_blog_page(); ?></p>
</div>


Dbranes comments:

cool ;-)

you might try put this after "new WP_Query" and before "while", like this:

$wp_query = new WP_Query($my_query_args);
?>

<?php
//comment this line out if you have it elsewhere:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
?>

<div id="pagination">
<p>
<span class="left">
<?php echo 'Page '. $paged . ' of ' . $wp_query->max_num_pages; ?>
</span>
<?php prev_blog_page(); ?>
<?php next_blog_page(); ?>
</p>
</div>

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




Anthony Moore comments:

Perfect thanks!


Anthony Moore comments:

I just realized that the page is displaying ALL of the posts and not just the ones with a term in the "celebrities" taxonomy.

I guess "taxonomy_name" doesn't do anything. These is my args

$my_query_args=array(
'post_type'=>'speakers',
'posts_per_page' => 5,
'offset' => 0,
'orderby'=>'title',
'order'=>'ASC',
'paged'=>$paged,
'taxonomy_name'=>'celebrity_types'
);


Any suggestions?

2012-12-09

phppoet answers:

Try this and tell me if it didn't work .

<?php query_posts( array( 'post_type' => 'Speakers', 'taxonomy' => 'Celebrities' 'showposts' => 10 ) ); ?>
<p>Celebrities</p>
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


<a href="<?php echo get_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

<?php the_post_thumbnail(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>


Anthony Moore comments:

It displays 10 posts, but how do I get pagination to work?


phppoet comments:

if you have created custom post type via theme then it is possible . you can follow the model of twenty ten theme for archive.php and functions.php

but if you want to create it in plugin then it looks difficult to me .


Anthony Moore comments:

I have a page called Celebrities that has your query. If I go to "celebrities/page/2" I get a 404 error. Shouldn't that display the next page of posts?


phppoet comments:

Just change your permalink structure and change again . if it works then you need to edit rewrite code rule for the plugin which has created custom post type for you . change permalink structure and try .


phppoet comments:

for your query (how would I get post from all the terms from the taxonomy "celebrities" and not just "sweden") regarding code supplied by Dbranes .


Just remove

'terms' => 'sweden'


from array(

'taxonomy' => 'celebrities',

'field' => 'slug',

'terms' => 'sweden'

)


your new code would be like

<?php

/*

Template Name: Celebrities

*/

get_header(); ?>



<div id="content">



<h1 class="page-title">

<?php the_title(); ?>

</h1>



<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>



<?php if ( have_posts() ) :



$temp = $wp_query;

$wp_query = null;



$my_query_args=array(

'post_type'=>'speakers',

'posts_per_page' => 6,

'offset' => 0,

'orderby'=>'date',

'order'=>'DESC',

'paged'=>$paged,

'tax_query' => array(

array(

'taxonomy' => 'celebrities',

'field' => 'slug',


)

)

);



$wp_query = new WP_Query($my_query_args);



while ($wp_query->have_posts()) : $wp_query->the_post();?>

<div id="post-<?php the_ID(); ?>" <?php post_class();?>>

<h2 class="entry-title"><?php the_title();?></h2>

<p><?php the_content();?></p>

</div>

<?php endwhile; ?>

<?php endif; ?>





<div class="pagination" id="pg">

<?php

global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(

'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),

'format' => '?paged=%#%',

'current' => max( 1, get_query_var('paged') ),

'total' => $wp_query->max_num_pages,

'prev_text' => __('&laquo; Previous'),

'next_text' => __('Next &raquo;')

));

?>

</div>



<?php

$wp_query = null;

$wp_query = $temp; // Reset

?>



</div><!-- end #content-->



<?php get_footer(); ?>