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

WordPress Custom Post Type Pagination 404 on in index.php :( WordPress

  • REFUNDED

Hey Guys,

First off, here is my test site [[LINK href="http://www.eirestudio.net/themeforest/delicious-wp/"]][[/LINK]]

I have a custom loop in my index.php homepage that displays a custom post type called <strong>recipe</strong>. These recipe posts display as expected and so do the pagination links but when I try to navigate the pagination links, I get a 404.

<strong>Here is my homepage loop (Yes, I am using query_posts, my new WP_Query loop wouldn't display the pagination...): </strong>

<ul class="recipes">

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'recipe',
'paged' => $paged,
'posts_per_page' => 2
);

query_posts($args);
if(have_posts()):
while (have_posts()) : the_post();
the_title();
endwhile;
else: ?>
<p>Nothing was found.</p>
<?php endif;

paginate(); ?>


<strong>And here is my code to register the custom post type</strong>

add_action( 'init', 'register_cpt_recipe' );

function register_cpt_recipe()
{
$labels = array
(
'name' => _x( 'Recipes', 'recipe' ),
'singular_name' => _x( 'Recipe', 'recipe' ),
'add_new' => _x( 'Add New', 'recipe' ),
'add_new_item' => _x( 'Add New Recipe', 'recipe' ),
'edit_item' => _x( 'Edit Recipe', 'recipe' ),
'new_item' => _x( 'New Recipe', 'recipe' ),
'view_item' => _x( 'View Recipe', 'recipe' ),
'search_items' => _x( 'Search Recipes', 'recipe' ),
'not_found' => _x( 'No recipes found', 'recipe' ),
'not_found_in_trash' => _x( 'No recipes found in Trash', 'recipe' ),
'parent_item_colon' => _x( 'Parent Recipe:', 'recipe' ),
'menu_name' => _x( 'Recipes', 'recipe' ),
);
$args = array
(
'labels' => $labels,
'hierarchical' => false,

'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
'taxonomies' => array( 'recipe_categories', 'recipe_tags' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,

'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => 'recipes',
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug'=>'recipes','with_front'=>false),
'capability_type' => 'post'
);
register_post_type( 'recipe', $args );
}


Any ideas?

Answers (9)

2012-07-23

webGP answers:

Hi,

try to use

$wp_query = new WP_Query($args);

instead of

query_posts($args);


Keith Donegan comments:

I already tried that as stated above and nothing...

<ul class="recipes">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
'post_type' => 'recipe',
'paged' => $paged,
);

$recipes_loop = new WP_Query($args);
if($recipes_loop->have_posts()):

while ($recipes_loop->have_posts()) : $recipes_loop->the_post(); ?>
<li class="clearfix">
<div class="thumbnail">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail_160_160'); ?></a>
</div>
<div class="content">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="meta"><?php echo __('Posted in'); ?> <?php echo get_the_term_list( $post->ID, 'recipe-category', ' ', ', ', '' ); ?> <?php echo __('on the'); ?> <?php the_time('jS F, Y'); ?></div>
<div class="entry">
<?php the_excerpt_rereloaded('20','Read More','','p','yes'); ?>
<a class="view" href="<?php the_permalink(); ?>"><?php echo __('View Recipe &rarr;'); ?></a>
</div>
</div>
</li>
<?php endwhile;
else: ?>
<p><?php echo __('Nothing was found.'); ?></p>
<?php endif; ?>
</ul>
<?php paginate(); ?>


webGP comments:

Do not use $recipes_loop variable, it is important to use $wp_query variable. Just replace
query_posts($args); with $wp_query = new WP_Query($args);


Keith Donegan comments:

<blockquote>Do not use $recipes_loop variable, it is important to use $wp_query variable. Just replace
query_posts($args); with $wp_query = new WP_Query($args);</blockquote>

That wouldn't make a difference because $wp_query is just another name for create a new WP_Query.


webGP comments:

$wp_query it's not another name for create a new WP_Query, it's a predefined Wordpress variable where WP store query data by default.


Keith Donegan comments:

Same story, 404

2012-07-23

Navjot Singh answers:

I am guessing there are other loops as well apart from this on the homepage. So add the following line

wp_reset_query();

before this line in your custom post type loop

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


Keith Donegan comments:

Thanks for the reply. No other loops on the page. just want to display recipes on the homepage with pagination.


Navjot Singh comments:

Try changing then

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

to

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


Keith Donegan comments:

Yeah, already tried that to no avail :(


Navjot Singh comments:

Can you show the paste the whole code of paginate() function?


Keith Donegan comments:

Just to note, the pagination works fine on non home 'pages': [[LINK href="http://www.eirestudio.net/themeforest/delicious-wp/recipes/"]][[/LINK]]

function paginate()
{
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
$pagination = array
(
'base' => @add_query_arg('page','%#%'),
'format' => '',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' => true,
'type' => 'plain'
);
if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
if ( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) );
echo paginate_links( $pagination );


Keith Donegan comments:

[[LINK href="http://www.eirestudio.net/themeforest/delicious-wp/recipes/"]]Recipes[[/LINK]]


Navjot Singh comments:

Pagination is working fine for me. No 404. Please check again. Delete cache?


Keith Donegan comments:

Yeah... but not on the homepage it's not. As I said on other pages/archives etc, pagination is fine.

2012-07-23

John Cotton answers:

<blockquote>That wouldn't make a difference because $wp_query is just another name for create a new WP_Query.</blockquote>

Did you try

global $wp_query; $wp_query= new WP_Query($args);

?


Keith Donegan comments:

Thanks for the reply. Just added but no dice :(


John Cotton comments:

<blockquote> but when I try to navigate the pagination links, I get a 404</blockquote>

I've had similar issues before with themes that do odd stuff on the home page (and hence mess up the rewrites).

In those situations, what I tend to do is adjust the base to be the archive link for the custom post type. So your base would

http:///www.domain.com/recipes/%_%

resulting in a link

http:///www.domain.com/recipes/page/2

or whatever.

Then create an archive-recipes.php and make sure it correctly handles pagination.

2012-07-23

Dbranes answers:

Sometimes this has worked for me:


<?php
flush_rewrite_rules();
?>


in the theme or


<?php
add_action('init', 'dotheflush');
function dotheflush() {
flush_rewrite_rules();
}
?>


in functions.php

(just remember to delete afterwards)

check this out:

[[LINK href="http://codex.wordpress.org/Function_Reference/flush_rewrite_rules"]]http://codex.wordpress.org/Function_Reference/flush_rewrite_rules[[/LINK]]

2012-07-23

Sabby Sam answers:

Hi Dear,

I have test this on sabir.co.in/test

Try this one :

paste this code in function.php

add_action( 'init', 'create_recipe' );

function create_recipe() {

$labels = array(
'name' => __('Recipes', 'FoodRecipe'),
'singular_name' => __('Recipe', 'FoodRecipe'),
'add_new' => __('Add New', 'FoodRecipe'), __('Recipe', 'FoodRecipe'),
'add_new_item' => __('Recipe', 'FoodRecipe'),
'edit_item' => __('Edit Recipe', 'FoodRecipe'),
'new_item' => __('New Recipe', 'FoodRecipe'),
'view_item' => __('View Recipe', 'FoodRecipe'),
'search_items' => __('Search Recipes', 'FoodRecipe'),
'not_found' => __('No Recipes found', 'FoodRecipe'),
'not_found_in_trash' => __('No Recipes found in Trash', 'FoodRecipe'),
'parent_item_colon' => ''
);

$supports = array(
'title',
'editor',
'custom-fields',
'thumbnail',
'categories',
'comments',
'excerpt'
);

register_post_type( 'recipe',
array(
'labels' => $labels,
'public' => true,
'menu_position' => 5,
'hierarchical' => false,
'supports' => $supports,
'taxonomies' => array('recipe-type', 'post_tag'),
'rewrite' => array( 'slug' => __('recipe', 'FoodRecipe') )
)
);

}


// Custom Texonomy Recipe Types for Recipe
add_action( 'init', 'build_taxonomies', 0 );

function build_taxonomies() {

// Recipe Type Custom Taxonomy
$recipe_type_labels = array(
'name' => __('Recipe Types', 'FoodRecipe'),
'singular_name' => __('Recipe Type', 'FoodRecipe'),
'search_items' => __('Search Recipe Types', 'FoodRecipe'),
'all_items' => __('All Recipe Types', 'FoodRecipe'),
'parent_item' => __('Parent Recipe Type', 'FoodRecipe'),
'parent_item_colon' =>__('Parent Recipe Type:', 'FoodRecipe'),
'edit_item' => __('Edit Recipe Type', 'FoodRecipe'),
'update_item' => __('Update Recipe Type', 'FoodRecipe'),
'add_new_item' => __('Add New Recipe Type', 'FoodRecipe'),
'new_item_name' => __('Recipe Type Name', 'FoodRecipe'),
'menu_name' => __('Recipe Types', 'FoodRecipe')
);


register_taxonomy(
'recipe_type',
'recipe',
array(
'hierarchical' => true,
'labels' => $recipe_type_labels,
'query_var' => true,
'rewrite' => array( 'slug' => __('recipe-type', 'FoodRecipe') )
)
);

function theme_pagination($pages = ''){
global $paged;
$paged = get_query_var( 'paged' );

if(empty($paged))$paged = 1;

$prev = $paged - 1;
$next = $paged + 1;
$range = 3; // only change it to show more links
$showitems = ($range * 2)+1;

if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}


if(1 != $pages){
echo "<div id='pagination'>";
echo ($paged > 2 && $paged > $range+1 && $showitems < $pages)? "<a href='".get_pagenum_link(1)."' class='btn'>&laquo; ".__('First', 'FoodRecipe')."</a> ":"";
echo ($paged > 1 && $showitems < $pages)? "<a href='".get_pagenum_link($prev)."' class='btn'>&laquo; ". __('Previous', 'FoodRecipe')."</a> ":"";


for ($i=1; $i <= $pages; $i++){
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
echo ($paged == $i)? "<a href='".get_pagenum_link($i)."' class='btn current'>".$i."</a> ":"<a href='".get_pagenum_link($i)."' class='btn'>".$i."</a> ";
}
}

echo ($paged < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($next)."' class='btn'>". __('Next', 'FoodRecipe') ." &raquo;</a> " :"";
echo ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($pages)."' class='btn'>". __('Last', 'FoodRecipe') ." &raquo;</a> ":"";
echo "</div>";
}
}


and paste your code where you want, like in any page

<?php

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

$args = array(

'post_type' => 'recipe',

'paged' => $paged,

'posts_per_page' => 2

);



query_posts($args);

if(have_posts()):

while (have_posts()) : the_post();

the_title();

endwhile;

else: ?>

<p>Nothing was found.</p>

<?php endif;



theme_pagination( $wp_query->max_num_pages); ?>

and you are done.

If you need help then let me know.

2012-07-24

nrisondewi answers:

Shipping &amp; Returns :

2012-07-24

gspringmaange answers:

Fünf Nadelmaschine

2012-07-25

ktigking answers:

Red diamond the audience 包邮! Repair of the table tool demolition - $7.00 :

2012-07-25

trvanterwi answers:

Liebo corta T -shirt