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

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$6
Pagination not showing up on custom portfolio page

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(); ?>

This question has been answered.

Lucas Wynne | 10/09/11 at 2:20pm Edit


The experts have suggested, on average, a prize of $10 for this question.

(5) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    10/09/11
    2:27pm
    Fahd Murtaza says:

    I am on it.

    • 10/09/11 2:46pm

      Fahd Murtaza says:

      Your current query is

      <?php 

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

      ?>

      /code>



      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
      ) ); ?>



      Thats all.

    • 10/09/11 2:47pm

      Fahd Murtaza says:

      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.

    • 10/09/11 3:14pm

      Lucas Wynne says:

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

    • 10/09/11 3:16pm

      Fahd Murtaza says:

      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.

    • 10/09/11 3:33pm

      Lucas Wynne says:

      Does not work.

  • avatar
    Last edited:
    10/09/11
    2:31pm
    Luis Abarca says:

    Change this way


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

    pagination($loop->max_num_pages);

    } ?>

    • 10/09/11 2:35pm

      Lucas Wynne says:

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

    • 10/09/11 2:40pm

      Luis Abarca says:

      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

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

      ?>

      <?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($loop->max_num_pages);

      } ?>



      </div>

      </div>

      </div>

      </div>

      </div>



      <?php get_footer(); ?>

    • 10/09/11 3:33pm

      Lucas Wynne says:

      Does not work.

    • 10/09/11 6:50pm

      Luis Abarca says:

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

    • 10/09/11 6:57pm

      Luis Abarca says:

      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.

  • avatar
    Last edited:
    10/09/11
    6:28pm
    Romel Apuya says:

    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; ?>


  • avatar
    Last edited:
    10/10/11
    11:56am
    Luis Cordova says:

    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

    Previous versions of this answer: 10/10/11 at 11:56am

  • avatar
    Last edited:
    10/10/11
    1:58pm
    Grégory Viguier says:

    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).

This question has expired.



Abdessamad Idrissi voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.