I've been fighting with this for about an hour now, and I'm throwing in the towel.
I even have had help with this issue before, but I still can't figure it out.
I have a custom taxonomy named 'mls'
I need a query that pulls all posts in the mls taxonomy, displays the excerpt and paginates with 15 posts per page. I'd like the previous and next links to be there.
EDIT : To anyone else who comes along looking for this answer. The code below works. I am calling this from a short code, so this might need to be tweaked slightly with a php tag somewhere in your code, but this works. This was a combo of some of the submissions below. Thank you to everyone who attempted to help.
$args = array(
'post_type' => 'mls',
'posts_per_page' => 5,//get_option( 'posts_per_page' ), // you can assign 15
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
);
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
the_excerpt();
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'prev_next' => True,
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
else: ?>
Nothing found
<?php endif ?>
Navjot Singh answers:
Try this then
Save the following code in a file taxonomy-mls.php
<?php get_header(); ?>
<h1><?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?> Movie Page</h1>
<div class="tag-desc"><?php echo $term->description; ?></div>
<br />
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link() ?></div>
<div class="alignright"><?php previous_posts_link() ?></div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
and make sure 'rewrite' => true is set while creating the taxonomy. If you want to load a specific term under mls taxonomy, save the above code in a file taxonomy-mls-term.php
Scott Hack comments:
Getting nothing found.
Navjot Singh comments:
Try this
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = 15;
$args=array(
'tax_query' => array(
array(
'taxonomy' => 'mls',
'field' => 'slug',
'terms' => 'termsinmls'
)
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => $post_per_page
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('< Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries >') ?></div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php endif;
$wp_query = $temp; //reset back to original query
?>
</div>
Navjot Singh comments:
Final Solution:
Try this
Save the following code in a file taxonomy-mls.php
<?php get_header(); ?>
<h1><?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?> Movie Page</h1>
<div class="tag-desc"><?php echo $term->description; ?></div>
<br />
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link() ?></div>
<div class="alignright"><?php previous_posts_link() ?></div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
and make sure 'rewrite' => true is set while creating the taxonomy. If you want to load a specific term under mls taxonomy, save the above code in a file taxonomy-mls-term.php
Arnav Joy answers:
check this plugin
http://wordpress.org/extend/plugins/ambrosite-nextprevious-post-link-plus/
and this link
http://themeforest.net/forums/thread/wordpress-custom-page-type-taxonomy-pagination/43010
Luis Abarca answers:
You need this things
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = 15; // -1 shows all posts
$tax_terms = get_terms( 'mls' );
$terms_slugs = wp_list_pluck( $tax_terms, 'slug');
$args = array(
'tax_query' => array(
array('taxonomy' => 'mls',
'field' => 'slug',
'terms' => $terms_slugs
)
),
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => $post_per_page
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post();
// your code here
the_excerpt();
<?php endwhile; ?>
<div class="alignleft"><?php next_posts_link('< Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries >') ?></div>
<?php else: ?>
Nothing found
<?php endif ?>
<?php $wp_query = $tmp; ?>
Scott Hack comments:
Getting a "Nothing Found" -- so something isn't right with the query.
Luis Abarca comments:
Try again
First, get the terms, then get the slugs in an array and pass to the query
<?php
$taxonomy = 'mls'
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = 15; // -1 shows all posts
$tax_terms = get_terms( $taxonomy );
$terms_slugs = wp_list_pluck( $tax_terms, 'slug');
$args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $terms_slugs
)
),
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => $post_per_page
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post();
// your code here
the_excerpt();
<?php endwhile; ?>
<div class="alignleft"><?php next_posts_link('< Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries >') ?></div>
<?php else: ?>
Nothing found
<?php endif ?>
<?php $wp_query = $tmp; ?>
Scott Hack comments:
Fatal error: Cannot use object of type WP_Error as array
Martin Pham answers:
creat file taxonomy-mls.php in theme
& change custom-post-type-name
$args = array(
'post_type' => 'custom-post-type-name',
'posts_per_page' => get_option( 'posts_per_page' ), // you can assign 15
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
);
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
the_excerpt();
endwhile;
?>
<div class="alignleft"><?php next_posts_link('< Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries >') ?></div>
<?php else: ?>
Nothing found
<?php endif ?>
Martin Pham comments:
example:
Creat custome post type
add_action( 'init', 'custom_post_type' );
function custom_post_type() {
register_post_type( 'news',
array(
'labels' => array(
'name' => __( 'News' ),
'singular_name' => __( 'News' ),
),
'menu_position' => 6,
'has_archive' => true,
'query_var' => true,
'public' => true,
'rewrite' => array( 'slug' => 'read-news' ),
'supports' => array( 'title', 'editor', 'thumbnail' ),
'show_in_menu' => true,
'show_in_nav_menus' => false,
'hierarchical' => true
)
);
register_taxonomy('newstax','news',array(
'hierarchical' => true,
'rewrite' => array( 'slug' => 'tax-news' )
));
register_taxonomy_for_object_type('newstax', 'news');
}
creat taxonomy for newstax
// file name taxonomy-newstax.php
$args = array(
'post_type' => 'news',
'posts_per_page' => get_option( 'posts_per_page' ), // you can assign 15
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
);
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
the_excerpt();
endwhile;
?>
<div class="alignleft"><?php next_posts_link('< Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries >') ?></div>
<?php else: ?>
Nothing found
<?php endif ?>
Scott Hack comments:
This somewhat worked. Issue I have is I am trying to call the query from a short code, so it can be theme independent and included in a plugin.
Martin Pham comments:
Did you mean
add_shortcode( 'get_post_taxonomy', 'get_post_custom_taxonomy' );
function get_post_custom_taxonomy($att) {
global $wp_query;
$defaults = array(
'per_page' => 5,
'post_type' => 'post',
);
$atts = shortcode_atts( $defaults, $atts );
$args = array(
'post_type' => $atts['post_type'],
'posts_per_page' => $atts['per_page'],
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
);
// do query
....
echo .... OR ... return
}
and
// echo ....
do_shortcode('[get_post_taxonomy post_type="mls" posts_per_page="15"]');
She Codes answers:
Please answer the following questions:
1) to what post type is your taxonomy assigned?
2) in which template is your query - is it taxonomy.php? Or maybe index.php?
3) can you paste the code from this template?
4) when do you get the error - when you go to the second page?
Scott Hack comments:
I am actually trying to use it in a short code, so the answers above with the file make it a bit tricky.
Jatin Soni answers:
Sorry was wrong post last time. I found it for you try this and let me know if it works. This is not my code but found on net. :) may be it works for you.
<?php
/*
Template Name: CustomTax
*/
get_header(); ?>
<div id="content" class="narrowcolumn">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = 1; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array(
'genre' => 'mystery',
'post_type' => 'book',
'paged' => $paged,
'posts_per_page' => $post_per_page
);
$temp = $wp_query; // assign orginal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments ', '1 Comment ', '% Comments '); ?></p>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('< Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries >') ?></div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif;
$wp_query = $temp; //reset back to original query
?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
oslemmonseste answers:
Die neue Frühjahr 2012 Liebo Frau weibliche hemden langarm 21120 - €42.93 :
Arthur Araújo answers:
Paste in the functions.php:
function fix_category_pagination($qs){
if(isset($qs['category_name']) && isset($qs['paged'])){
$qs['post_type'] = get_post_types($args = array(
'public' => true,
'_builtin' => false
));
array_push($qs['post_type'],'post');
}
return $qs;
}
add_filter('request', 'fix_category_pagination');