WordPress.org

Ready to get started?Download WordPress

Forums

Pagination & 404 Error Custom Template (1 post)

  1. jhonb
    Member
    Posted 1 year ago #

    Hi Guys,

    What's up?

    I have a little problem that I can't fix... Maybe someone have the solution !
    I made a pagination trough my functions.php which looks like this

    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=\"pagi\"><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";
         }
    }

    To display the pagination I use this:

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

    My problem is that when I want to go to the page 2 I have a 404 error...

    Here is my custom template:

    <?php
    /*
    Template Name: Offres
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="content">
    	<!-- Wrapper START-->
    	<div class="wrapper">
    		<div class="main_content">
    			<h2><span>Les offres de nos partenaires</span></h2>
    			<div class="content_left">
    				<section>
    					<h2><?php the_title(); ?></h2>
    					<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    						<ul class="list_elements">
    							<?php global $wp_query; ?>
    
    							<?php $loop = new WP_Query(array('post_type' => 'offres', 'posts_per_page' => 2, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1 ) ); ?>
    							<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    							<li class="clearfix"><a href="<?php the_permalink(); ?>">
    								<?php the_post_thumbnail("thumbs_list"); ?>
    								<h3><?php the_title(); ?></h3>
    								<p><?php echo excerpt(40); ?></p>
    							<p class="more"><span>+</span></p>
    							</a></li>
    
    						<?php endwhile; ?>
    
    						<?php wp_reset_query(); ?>
    						</ul>
    						<?php if (function_exists("pagination")) {
    						    pagination($loop->max_num_pages);
    					    } ?>
    						<?php endwhile; else: ?>
    							<p><?php _e('Désolé, nous ne trouvons pas la page demandé'); ?></p>
    							<a href="<?php bloginfo( 'url' ); ?>" class="btn"><?php _e('Retour'); ?></a>
    							<div class="clear"></div>
    						<?php endif; ?>
    				</section>
    			</div>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    And my register post type:

    add_action('init', 'offres_init');
    function offres_init()
    {
      $labels = array(
        'name' => 'Offre',
        'singular_name' => 'Offre',
        'add_new' => 'Ajouter une offre',
        'add_new_item' => 'Ajouter une nouvelle offre',
        'edit_item' => 'Modifier une offre',
        'new_item' => 'Nouvelle offre',
        'view_item' => 'Voir loffre',
        'search_items' => 'Rechercher une offre',
        'not_found' =>  'Aucune offre trouvée',
        'not_found_in_trash' => 'Aucune offre dans la poubelle',
        'parent_item_colon' => '',
        'menu_name' => 'Offres'
    
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 5,
    
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields')
      );
      register_post_type('offres',$args);
    }
    
    add_action( 'init', 'create_taxonomy_offres', 0 );
    
    function create_taxonomy_offres()
    {
      $labels = array(
        'name' => 'Categories',
        'singular_name' => 'Categorie',
        'search_items' =>  'Rechercher une categorie',
        'all_items' => 'Toutes les categories',
        'edit_item' => 'Editer une categorie',
        'update_item' => 'Modifier une categorie',
        'add_new_item' => 'Ajouter une categorie',
        'new_item_name' => 'Nouvelle categorie',
        'menu_name' => 'Categories'
      ); 	
    
      register_taxonomy('offers',array('offres'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'offers' )
      ));
    }

    If anyone knows it will help me a lot!

    Many thanks !

    Jhon

Topic Closed

This topic has been closed to new replies.

About this Topic