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

Pagination not showing up on custom portfolio page WordPress

  • SOLVED

I am using a custom page template for my portfolio. The code is calling the correct number of posts per page but for some reason the pagination links won't show up :-S

My query
<?php
$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 2));
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$screenshot_url = $custom["screenshot_url"][0];
$website_url = $custom["website_url"][0];
?>


My portfolio page (code used in functions)
//////////////////////////////////////// Create portfolio
add_action('init', 'create_portfolio');
function create_portfolio() {
$portfolio_args = array(
'label' => __('Portfolio'),
'singular_label' => __('Portfolio'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'thumbnail')
);
register_post_type('portfolio',$portfolio_args);
}

//////////////////////////////////////// Input fields
add_action("admin_init", "add_portfolio");
add_action('save_post', 'update_website_url');
function add_portfolio(){
add_meta_box("portfolio_details", "Portfolio Options", "portfolio_options", "portfolio", "normal", "low");
}
function portfolio_options(){
global $post;
$custom = get_post_custom($post->ID);
$website_url = $custom["website_url"][0];
?>
<div id="portfolio-options">
<label>Website URL:</label><input name="website_url" value="<?php echo $website_url; ?>" />
</div><!--end portfolio-options-->
<?php
}
function update_website_url(){
global $post;
update_post_meta($post->ID, "website_url", $_POST["website_url"]);
}

add_filter("manage_edit-portfolio_columns", "portfolio_edit_columns");
add_action("manage_posts_custom_column", "portfolio_columns_display");

function portfolio_edit_columns($portfolio_columns){
$portfolio_columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Project Title",
"description" => "Description",
);
return $portfolio_columns;
}

function portfolio_columns_display($portfolio_columns){
switch ($portfolio_columns)
{
case "description":
the_excerpt();
break;
}
}

////////////////////////////////////////
add_filter('parse_query', 'wpq_parse_query');
function wpq_parse_query($query)
{
if($query->is_archive())
{
$query->query_vars['posts_per_page'] = get_option('to_count_archives', 2);
}
return $query;
}


My pagination, in functions
//////////////////////////////////////// Pagination
function pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";

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

if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
echo "</div>\n";
}
}


My entire markup
<?php
/*
Template Name: Portfolio
*/
?>

<?php get_header(); ?>

<div id="full_container">
<div id="portfolio_content">
<div id="portfolio_wrap">
<div id="content">

<?php
$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 2));
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$screenshot_url = $custom["screenshot_url"][0];
$website_url = $custom["website_url"][0];
?>


<a href="<?php the_permalink() ?>">

<span class="img">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'thmb-portfolio' ); } ?>

<span class="under">
<!-- Excerpt title -->
<span class="title"><?php the_title(); ?></span>

<!-- Excerpt description -->
<span class="desc">
<?php my_excerpt('short'); ?>
</span>
</span>
</span>
</a>


<?php endwhile; ?>

<!-- Next/Previous Posts -->
<?php if (function_exists("pagination")) {
pagination($additional_loop->max_num_pages);
} ?>

</div>
</div>
</div>
</div>
</div>

<?php get_footer(); ?>

Answers (5)

2011-10-09

Fahad Murtaza answers:

I am on it.


Fahad Murtaza comments:

Your current query is

<?php

$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 2));

?>

/code>



Change it to

<code>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array(
'post_type' => 'portfolio',
'posts_per_page' => 2,
'paged'=>$paged
) ); ?>



Thats all.


Fahad Murtaza comments:

Your current query code is


<?php
$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 2));
?>


Change it to

<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array(
'post_type' => 'portfolio',
'posts_per_page' => 2,
'paged'=>$paged
) ); ?>



Sorry for messed up post above.


Lucas Wynne comments:

When I go to the second page: http://themeforward.com/demo2/portfolio/page/2/, I get a 404 error.


Fahad Murtaza comments:

OK,

Did you save the permalinks after you created the custom post type. If not, please go to options > Permalinks and that's where you can save again. Let me know if it helps.


Lucas Wynne comments:

Does not work.

2011-10-09

Luis Abarca answers:

Change this way


<?php if (function_exists("pagination")) {

pagination($loop->max_num_pages);

} ?>


Lucas Wynne comments:

When I click to the second page via pagination I get a 404 error. This did make it appear though... just doesn't work.


Luis Abarca comments:

I add, paged arg to your custom query


<?php

/*

Template Name: Portfolio

*/

?>



<?php get_header(); ?>



<div id="full_container">

<div id="portfolio_content">

<div id="portfolio_wrap">

<div id="content">



<?php
<strong>
$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 2, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);
</strong>
?>

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

<?php

$custom = get_post_custom($post->ID);

$screenshot_url = $custom["screenshot_url"][0];

$website_url = $custom["website_url"][0];

?>





<a href="<?php the_permalink() ?>">



<span class="img">

<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'thmb-portfolio' ); } ?>



<span class="under">

<!-- Excerpt title -->

<span class="title"><?php the_title(); ?></span>



<!-- Excerpt description -->

<span class="desc">

<?php my_excerpt('short'); ?>

</span>

</span>

</span>

</a>





<?php endwhile; ?>



<!-- Next/Previous Posts -->

<?php if (function_exists("pagination")) {
<strong>
pagination($loop->max_num_pages);
</strong>
} ?>



</div>

</div>

</div>

</div>

</div>



<?php get_footer(); ?>


Lucas Wynne comments:

Does not work.


Luis Abarca comments:

you have to call flush_rewrite_rules after custom post type registration or visit Setting > permalinks, just go and try again


Luis Abarca comments:

I just duplicate your env, and it's working, i just visit Settings > Permalinks and now its working the pagination and no more 404 error code.

Send me your login details if you have any issue, i can help you.

2011-10-09

Romel Apuya answers:

have you tried adding

global $wp_query;

before

$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 2));

and

<?php wp_reset_postdata(); ?>

after

<?php endwhile; ?>

2011-10-10

Luis Cordova answers:

do not expect wp pagination to work automatically on custom template

rather make sure you do something like this:



//global $wp;

$pagenum = get_query_var('paged');

if (empty($pagenum)) {
$pagenum = 1;
}

$year_value1 = get_query_var('yearr');//$wp->query_vars['year'];

if (empty($year_value1)) {
$year_value1 = '2008';
}

//142 md,6 a,7 t
query_posts("posts_per_page=10&paged=$pagenum&meta_key=release_year&meta_value=$year_value1");
//echo "posts_per_page=10&paged=$pagenum&meta_key=release_year&meta_value=$year_value1";

?>


I have introduced some of the commented code to give you an idea how to output information to check if it is working correctly.

Once you go once through this test you are set for success.


I have used old syntax above to ensure you get the result. This is the equivalent of the newer syntax which is what you are using:


$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 2, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )


The problem is that for some reason :

1. either the keyword you are choosing is conflicting

2. this piece is not working properly 'paged' => get_query_var('paged') ? get_query_var('paged') : 1

so you need to var_export or var_dump this line until you see it working

it would be good to know what the get_query_var('paged') returns when you are on page 2 etc

please let me know, thanks

2011-10-10

Grégory Viguier answers:

Hi there.

Try my plugin : http://wordpress.org/extend/plugins/sf-pages-for-custom-posts/
It still in beta and has some bugs I haven't fixed yet, but it works in most cases.
Create archive-portfolio.php with a normal loop (no need of a custom loop), and go to the plugins settings (choose archive for the template).