Hello,
I have a custom post type archive that needs to show only 1 post with a next_posts_link and previous_posts_link at the bottom.
I've got the link showing up but when it gets clicked it doesn't show the previous post but a 404 page instead.
My permalink is set as: /%category%/%postname%
Here's the link: [[LINK href="http://jacksonsafricansafaris.com/past-trips"]]http://jacksonsafricansafaris.com/past-trips[[/LINK]]
Here's the functions creating the custom post type:
// REGISTER CUSTOM POST TYPE FOR PAST TRIPS
function past_trips_post_type() {
register_post_type('past-trips',array(
'labels' => array(
'name' => 'Past Trips',
'singular_name' => 'Past Trip',
'menu_name' => 'Past Trips',
'add_new_item' => __('Add Past Trip'),
'edit' => __('Edit'),
'edit_item' => __('Edit Past Trip'),
'new_item' => __('New Past Trip'),
'view' => __('View Past Trip'),
'view_item' => __('View Past Trip'),
'search_items' => __('Search Past Trips'),
'not_found' => __('No Past Trips found'),
'not_found_in_trash' => __('No Past Trips found in Trash'),
),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'supports' => array('title','editor','thumbnail', 'custom-fields', 'comments'),
'menu_position' => 25,
'rewrite' => array(
'slug' => 'past-trips',
'with_front' => false
),
'capability_type' => 'post',
'hierarchical' => false,
'query_var' => true,
'has_archive' => 'past-trips',
));
}
add_action('init', 'past_trips_post_type');
Here's the code for the archive-past-trips.php template:
<?php get_header(); ?>
<section id="main-content" class="grid_16 group">
<div class="posts grid_11 alpha group" id="post-<?php the_ID(); ?>">
<h1><?php post_type_archive_title(); ?></h1>
<?php query_posts('post_type=past-trips&posts_per_page=1&paged=>$paged'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $date = get_post_meta($post->ID, 'Past Trip Date', true); ?>
<?php $country = get_post_meta($post->ID, 'Country', true); ?>
<div class="entry group">
<header class="post-header group">
<span class="category-title"><?php echo $country; ?></span>
<h2 class="post-title">
<a title="Permalink to <?php the_title(); ?>" href="<?php the_permalink(); ?>">
<?php the_title(); ?></h2>
</a>
<span class="date floatleft"><?php echo $date; ?></span>
<span class="more-link floatright">
<a title="Permalink to <?php the_title(); ?>" href="<?php the_permalink(); ?>">>More Info</a>
</span>
</header><!-- post-header -->
<div class="post-thumb floatleft">
<?php if ( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail(array(140,100)); ?>
<?php } else { ?>
<img alt="no image available" src="<?php bloginfo('template_url');?>/images/tmp-featured-image-140.jpg" width="140" height="100">
<?php } ?>
</div><!-- post-thumb -->
<div class="the-excerpt floatleft group">
<?php the_excerpt(); ?>
</div><!-- the-excerpt -->
</div><!-- entry -->
<?php endwhile; ?>
<nav class="navigation">
<ul>
<li class="next-posts post-nav"><?php next_posts_link('< Older Past Trips') ?></li>
<li class="prev-posts post-nav"><?php previous_posts_link('Newer Past Trips >') ?></li>
</ul>
</nav>
<?php endif; ?>
</div><!-- post-ID -->
<?php get_sidebar(); ?>
</section><!-- main-content -->
<?php get_footer(); ?>
Maor Barazany answers:
Try to change this line in your query in the archive-past-trips.php file
From -
<?php query_posts('post_type=past-trips&posts_per_page=1&paged=>$paged'); ?>
to this -
<?php $args = array ('post_type'=> 'past-trips', 'posts_per_page' => 1, paged =>$paged);
query_posts($args); ?>
Also, re-save your permalinks structure.
Denis Leblanc comments:
Maor,
Replaced the query with yours and re-saved the permalinks and I still get the 404.
Denis
Denis Leblanc comments:
Is it possible that there is something missing in my function?
d.
Maor Barazany comments:
Try this one, another way for this to work -
<?php global $paged;
query_posts('post_type=past-trips&posts_per_page=1&paged='.$paged); ?>
Maor Barazany comments:
Aslo, try changing in your register_post_type function
From this
'query_var' => true,
to this -
'query_var' => 'past-trips',
Denis Leblanc comments:
This is very odd. Still not working. Changed the query and function and still getting the 404.
d.
Denis Leblanc comments:
This is very odd. Still not working. Changed the query and function and still getting the 404.
d.
Maor Barazany comments:
Try adding this before the query_posts line -
$paged = get_query_var('paged');
$paged = ($paged) ? $paged : 1;
Denis Leblanc comments:
Not working either.
Here's my updated code:
<?php get_header(); ?>
<section id="main-content" class="grid_16 group">
<div class="posts grid_11 alpha group" id="post-<?php the_ID(); ?>">
<h1><?php post_type_archive_title(); ?></h1>
<?php $paged = get_query_var('paged'); ?>
<?php $paged = ($paged) ? $paged : 1; ?>
<?php $args = array ('post_type'=> 'past-trips', 'posts_per_page' => 1, paged =>$paged); ?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $date = get_post_meta($post->ID, 'Past Trip Date', true); ?>
<?php $country = get_post_meta($post->ID, 'Country', true); ?>
<div class="entry group">
<header class="post-header group">
<span class="category-title"><?php echo $country; ?></span>
<h2 class="post-title">
<a title="Permalink to <?php the_title(); ?>" href="<?php the_permalink(); ?>">
<?php the_title(); ?></h2>
</a>
<span class="date floatleft"><?php echo $date; ?></span>
<span class="more-link floatright">
<a title="Permalink to <?php the_title(); ?>" href="<?php the_permalink(); ?>">>More Info</a>
</span>
</header><!-- post-header -->
<div class="post-thumb floatleft">
<?php if ( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail(array(140,100)); ?>
<?php } else { ?>
<img alt="no image available" src="<?php bloginfo('template_url');?>/images/tmp-featured-image-140.jpg" width="140" height="100">
<?php } ?>
</div><!-- post-thumb -->
<div class="the-excerpt floatleft group">
<?php the_excerpt(); ?>
</div><!-- the-excerpt -->
</div><!-- entry -->
<?php endwhile; ?>
<nav class="navigation">
<ul>
<li class="next-posts post-nav"><?php next_posts_link('< Older Past Trips') ?></li>
<li class="prev-posts post-nav"><?php previous_posts_link('Newer Past Trips >') ?></li>
</ul>
</nav>
<?php endif; ?>
</div><!-- post-ID -->
<?php get_sidebar(); ?>
</section><!-- main-content -->
<?php get_footer(); ?>
Maor Barazany comments:
Sorry, had small mistake, This line -
<?php $args = array ('post_type'=> 'past-trips', 'posts_per_page' => 1, paged =>$paged); ?>
should replaced with (I forgot to wrap the first paged with '' ) -
<?php $args = array ('post_type'=> 'past-trips', 'posts_per_page' => 1, 'paged' =>$paged); ?>
Denis Leblanc comments:
Maor,
I changed that line and still not luck. Are you sure this line is correct:
<?php $paged = ($paged) ? $paged : 1; ?>
Maor Barazany comments:
Yes, this line is correct, although in most cases it should work without it also.
Also, you could try Denzel's suggestion to add the $query_string.' to the query.
Maor Barazany comments:
Try to deactivate all of your plugins, maybe you have a plugin that breaks this. This is very weird, since the code I gave you works for me great in many sites.
Denis Leblanc comments:
Maor,
Deactivated all the plugins and re-saved the permalinks. Still not working. I'm thinking of abandoning this idea.
I've been running in to a lot of issues with custom post types with this project. They're still not quite up to speed with posts and pages yet.
This should be easily controllable as they would be with regular posts.
Thanks for the help.
Maor Barazany comments:
Well, I use a lot of Custom Post Types in projects, and they work and function great as expected.
There might be something in your theme causing this. If you would like to check this, maybe try to use a blank theme or the default twenty-ten theme and add there your CPT declaration.
Maor Barazany comments:
It also might be an issue with WP3.1 bug in certain environments, try to upgrade to the new WP3.1.1 , it might also solve your issue.
http://codex.wordpress.org/Version_3.1.1
Sébastien | French WordpressDesigner answers:
if ( get_query_var('paged') )
$paged = get_query_var('paged');
elseif ( get_query_var('page') )
$paged = get_query_var('page');
else
$paged = 1;
$args=array(
'post_type' => 'past-trips',
'posts_per_page' => 1,
'paged' => $paged
);
Denis Leblanc comments:
Thanks Sebastien,
Not having any luck with that code either. Here's my updated code:
<?php
if (get_query_var('paged')) {
$paged = get_query_var('paged');
} elseif (get_query_var('page')) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
?>
<?php $args = array ('post_type'=> 'past-trips', 'posts_per_page' => 1, paged =>$paged); ?>
<?php query_posts($args); ?>
Denzel Chia answers:
Hi,
Please try my version for your query_posts.
I had create a theme that needed custom queries and therefore had come up with the working solution before.
Please try this, the arguments are already included.
<?php
query_posts($query_string.'&post_type=past-trips&posts_per_page=1' . '&paged=' . get_query_var('paged'));
?>
Thanks.
Denzel
Denis Leblanc comments:
Thanks Denzel,
That doesn't seem to work either.
M H answers:
This might be due to this problem - having the "Blog pages show at most" setting under reading settings read something different than your query.
The fix is to have that number in the reading settings less than your desired number in the query( eg. set it to 1).
There are a few more possible issues that cause the 404, for instance, having your Page share the same name as your custom post type.
These are outlined and discussed in the following links along with their solutions:
http://wordpress.org/support/topic/error-404-on-pagination-when-changing-posts_per_page-on-query_posts
http://wordpress.org/support/topic/pagination-with-custom-post-type-listing?replies=23#post-1637753
Christianto answers:
Hi Denis Leblanc,
The reason your custom page post turn to be 404 page is because your custom permalink is set to /%category%/%postname%.
If you use custom permalink wordpress will use it as query strings. For example, Base on your permalink structure if the post url is http://jacksonsafricansafaris.com/past-trips/mypostname it will take post name 'mypostname' and on category 'past-trips'.
so link
http://jacksonsafricansafaris.com/past-trips
will lead you to past-trips category not past-trips custom post type.
so you will have to edit (use custom post-type query) on category-past-trips.php not archive-past-trips.php
pointing to
http://jacksonsafricansafaris.com/past-trips
for wordpress it means show category-past-trips.php and show posts under past-trips post type.
But on the other hand...
if you point your browser to post under 'past-trips' post type for example:
http://jacksonsafricansafaris.com/past-trips/my-begin-journey
it will turn to 404 as well
Like I said it will search for post named 'my-begin-journey' under 'past-trips' category.
I try to create custom taxonomy name 'past-trips' but it failed simply because '%category%' format permalink always refer to Categories under 'post' not under your custom post type 'Past Trips', therefore it will turn to 404 since categories and postname mismatch.
So for showing single post not archive/category the best solution is try to change permalink to default format like http://yourblog.com/?p=123
And for Category archive the format will be http://yourblog.com/?category_name=past-trips
Personally I think this is wordpress bug not because your code.