Support » Developing with WordPress » Pagination for custom page

  • Hi all,
    i’m in trouble about to develope a pagination to a custom page, and i kindly would ask your big support for it.
    I need to put a numeric pagination but after several code found on internet like:
    https://gist.github.com/wpscholar/1243396
    i failed
    Can someone developer help me to add this pagination system?
    You can see below the page code, this show 10 posts_per_page.
    Thanks for attention

    
    
    <?php
    
    if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }
    
    if (isset($_REQUEST['anno']))
    	$Anno=(int)$_REQUEST['anno'];
    else
    	$Anno=date("Y");
    $Mese=0;
    if (isset($_REQUEST['mese']))
    	$Mese=(int)$_REQUEST['mese'];
    $ret=Lista_Circolari($Anno,$Mese);
    
    function Lista_Circolari($Anno,$Mese){
    $Contenuto="";
    $IdCircolari=get_option('Circolari_Categoria');
    $mesecorrente = date('n');
    if (isset($_REQUEST['Anno']))
    	$annocorrente = (int)$_REQUEST['Anno'];
    else
    	$annocorrente = date('Y');
    if (isset($_REQUEST['Mese']))
    	$mesecorrente=(int)$_REQUEST['Mese'];
    elseif(isset($_REQUEST['Anno']))
    	$mesecorrente="";
    else
    	$mesecorrente=date('n');
    $args = array( 'category' => $IdCircolari,
    		       'post_type' => array('post','circolari'),
    			   'posts_per_page'  => 10,
    			   'post_status' => 'publish');
    $Circolari = get_posts($args);
    
    if (empty($Circolari)){
    	$Contenuto.='<h3 class="mescirc">Non risultano circolari per '.circ_MeseLettere($mesecorrente).' '.$annocorrente.' verranno visualizzate le ultime 50</h3>';
    	$args = array( 'category' => $IdCircolari,
    	       'post_type' => array('post','circolari'),
    		   'posts_per_page'  => -1,
    		   'post_status' => 'publish');	
    	$Circolari = get_posts($args);
    }
    
    $Contenuto.=' <div>';
    foreach($Circolari as $post) {
    	$visibilita=get_post_meta($post->ID, "_visibilita");
     	if(count($visibilita)==0)
     		$visibilita="p";
     	else 
     		$visibilita=$visibilita[0];
    	if ((Is_Circolare_per_User($post->ID) and $visibilita=="d") or $visibilita=="p"){
    		$fgs = wp_get_object_terms($post->ID, 'gruppiutenti');
    		$Elenco="";
    		if(!empty($fgs)){
    			foreach($fgs as $fg){
    				$Elenco.=$fg->name." - ";
    			}
    			$Elenco=substr($Elenco,0,strlen($Elenco)-3);
    		}
    
    		$Contenuto.='
    		<div class="circosx"><img src="IMG.jpg"  /></div><div style="float:left; width:70%;">';
    		$numero=get_post_meta($post->ID, "_numero",TRUE);
    		$anno=get_post_meta($post->ID, "_anno",TRUE);
    		$Contenuto.='
    			<div class="circodx"><h4><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a>
    			</h4>
    			<div style="font-weight: bold;font-size:0.8em;">Del '.FormatDataItaliano($post->post_date).' Numero '.$numero.'_'.$anno.'</div> 
    			<div style="min-height:30px;margin-top:5px;margin-bottom:5px; padding: 5px 3px;">
    					<!--<img src="'.Circolari_URL.'img/tipo.png" style="border:0;" alt="Icona tipo post" />
    					<p style="font-style:italic;font-size:0.8em;display:inline;margin-top:3px; margin-right:15px;">
    						<a href="LINK">'.$post->post_type.'</a>
    					</p>  -->
    				
    			';
    
    	
    		if ($post->post_type=="circolari")
     
    
    			$Contenuto.='
    					<img src="'.Circolari_URL.'img/gruppo.png" style="border:0; float:left;" alt="Icona destinatari"/>
    					<p style="font-style:italic;font-size:0.8em;float:left;display:inline;margin:-2px 0px 0px 5px;">'.$Elenco.'
    					<!--<a href="LINK'.strtolower(str_replace(' ', '-', $Elenco)).'"></a>-->
    					</p>';
    
     
    
    		if (!post_password_required( $post->ID ))
    			$riassunto=	$post->post_excerpt;
    		else{
    			$riassunto="";
    		}
    		if (!empty($post->post_password))
    			$Contenuto.='
    					<img src="'.Circolari_URL.'img/protetto.png" style="border:0;" alt="Icona protezione"/>
    					<p style="font-style:italic;font-size:0.8em;display:inline;margin-top:3px;">Contenuto Protetto</p>';	
    //		if (Is_Circolare_per_User($post->ID))
    //	echo ">>>>>>>>>>>>>>>>>>>>>>>>>><<".$post->ID."<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><br />";return;
    			if (Is_Circolare_Da_Firmare($post->ID))
    				if (!Is_Circolare_Firmata($post->ID)) {
    					$ngiorni=Get_scadenzaCircolare($post->ID,"",True);					
    					if(Is_Circolare_Scaduta($post->ID)){
    						$Contenuto.='
    							<img src="'.Circolari_URL.'/img/firmabe.png" style="border:0;" alt="Icona firma o presa visione"/>
    							<p style="font-style:italic;font-size:0.8em;display:inline;margin-top:3px;color:red;">Scaduta e non Firmata</p>';						
    					}else{
    						switch ($ngiorni){
    							case -1:							
    								$entro="";							
    								break;													
    							case 0:
    								$entro="entro OGGI";
    								break;
    							case 1:
    								$entro="entro DOMANI";
    								break;
    							default:
    								$entro="entro $ngiorni giorni";
    								break;
    						}
    						if (get_post_meta($post->ID, "_sciopero",TRUE)=="Si")
    							$Tipo="Esprimere adesione $entro";
    						else
    							if (get_post_meta($post->ID, "_firma",TRUE)=="Si")
    								$Tipo="Firmare $entro";
    						$Contenuto.='
    							<img src="'.Circolari_URL.'/img/firmabe.png" style="border:0;" alt="Icona firma o presa visione"/>
    							<p style="font-style:italic;font-size:0.8em;display:inline;margin-top:3px;color:red;">'.$Tipo.'</p>';	
    				}			
    			}else{
    				$Contenuto.='
    					<img src="'.Circolari_URL.'/img/firmabe.png" style="border:0;" alt="Icona firma o presa visione"/>
    					<p style="font-style:italic;font-size:0.8em;display:inline;margin-top:3px;color:blue;">Firmata</p>';				
    			}
    			$Contenuto.='	
    			</div>
    			<div style="margin-bottom:5px;">
    				<em>'.$riassunto .'</em>
    			</div>
            
    			
    			<div style="height:30px;"></div>
    		</div></div><hr class="separacirc" />';
    	}
    }
    
    $Contenuto.= '
    		</div>
    		<div style="clear:both"></div>';
    
    return $Contenuto;
    }
    ?>
    
     
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @bluekid,

    I am assuming that you need to have numeric pagination for your any custom query, if not please me know so I can help you in other way, here is sample code for you as below.

    
    
    <?php
    $no = 20;
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if ($paged == 1) {
        $offset = 0;
    } else {
        $offset = ($paged - 1) * $no;
    }
    
    // calculate total results
    $query_count = "SELECT * FROM Table WHERE status = '1'  ";
    $wpdb->get_results($query_count);
    $total_results = $wpdb->num_rows;
    
    // run actual query for your data
    $query = "SELECT * FROM Table WHERE status = '1' ORDER BY updation_time DESC  LIMIT $offset, $no ";
    $data  = $wpdb->get_results($query);
    
    // display your query data in table etc
    if ($data) {
        foreach ($data as $p):
        // deal your query
        endforeach;
        
    } else {
        // deal with no data
    }
    ?>
    
    // here is code for pagination
    
    <?php
    if ($no > 0) {
    ?>
         <nav>
      <ul class="pagination"> 
         <?php
        
        $total_pages = ceil($total_results / $no);
        
        $pagination = paginate_links(array(
            
            
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '?paged=%#%',
            'current' => $paged,
            'total' => $total_pages,
            'prev_next' => True,
            'prev_text' => __('<span aria-hidden="true">&laquo;</span>'),
            'next_text' => __('<span aria-hidden="true">&raquo;</span>'),
            'type' => 'plan',
            'add_args' => False,
            
            'before_page_number' => '',
            'after_page_number' => ''
        ));
        
        echo $pagination;
        
    ?>
          </ul>
    </nav>  
        <?php
    }
    ?>
    
    Thread Starter Bluekid

    (@bluekid)

    Hi Umar, thanks for your kindness and support 🙂
    Yes, i need to add a pagination system.
    The problem is that i don’t know how to integrate pagination code in this code:

    
    <?php
    
    if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }
    
    if (isset($_REQUEST['anno']))
    	$Anno=(int)$_REQUEST['anno'];
    else
    	$Anno=date("Y");
    $Mese=0;
    if (isset($_REQUEST['mese']))
    	$Mese=(int)$_REQUEST['mese'];
    $ret=Lista_Circolari($Anno,$Mese);
    
    function Lista_Circolari($Anno,$Mese){
    $Contenuto="";
    $IdCircolari=get_option('Circolari_Categoria');
    $mesecorrente = date('n');
    if (isset($_REQUEST['Anno']))
    	$annocorrente = (int)$_REQUEST['Anno'];
    else
    	$annocorrente = date('Y');
    if (isset($_REQUEST['Mese']))
    	$mesecorrente=(int)$_REQUEST['Mese'];
    elseif(isset($_REQUEST['Anno']))
    	$mesecorrente="";
    else
    	$mesecorrente=date('n');
    $args = array( 'category' => $IdCircolari,
    		       'post_type' => array('post','circolari'),
    			   'posts_per_page'  => 10,
    			   'post_status' => 'publish');
    $Circolari = get_posts($args);
    
    if (empty($Circolari)){
    	$Contenuto.='<h3 class="mescirc">Non risultano circolari per '.circ_MeseLettere($mesecorrente).' '.$annocorrente.' verranno visualizzate le ultime 50</h3>';
    	$args = array( 'category' => $IdCircolari,
    	       'post_type' => array('post','circolari'),
    		   'posts_per_page'  => -1,
    		   'post_status' => 'publish');	
    	$Circolari = get_posts($args);
    }
    
    $Contenuto.=' <div>';
    foreach($Circolari as $post) {
    	$visibilita=get_post_meta($post->ID, "_visibilita");
     	if(count($visibilita)==0)
     		$visibilita="p";
     	else 
     		$visibilita=$visibilita[0];
    	if ((Is_Circolare_per_User($post->ID) and $visibilita=="d") or $visibilita=="p"){
    		$fgs = wp_get_object_terms($post->ID, 'gruppiutenti');
    		$Elenco="";
    		if(!empty($fgs)){
    			foreach($fgs as $fg){
    				$Elenco.=$fg->name." - ";
    			}
    			$Elenco=substr($Elenco,0,strlen($Elenco)-3);
    		}
    
    		$Contenuto.='
    		
    		
    		// H E R E   S T A R T   L O O P
    		
    		
    		<div class="circosx"><img src="IMG.jpg"  /></div><div style="float:left; width:70%;">';
    		$numero=get_post_meta($post->ID, "_numero",TRUE);
    		$anno=get_post_meta($post->ID, "_anno",TRUE);
    		$Contenuto.='
    			<div class="circodx"><h4><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a>
    			</h4>
    			<div style="font-weight: bold;font-size:0.8em;">Del '.FormatDataItaliano($post->post_date).' Numero '.$numero.'_'.$anno.'</div> 
    			<div style="min-height:30px;margin-top:5px;margin-bottom:5px; padding: 5px 3px;">
    					<!--<img src="'.Circolari_URL.'img/tipo.png" style="border:0;" alt="Icona tipo post" />
    					<p style="font-style:italic;font-size:0.8em;display:inline;margin-top:3px; margin-right:15px;">
    						<a href="LINK">'.$post->post_type.'</a>
    					</p>  -->
    				
    			';
    
    		if ($post->post_type=="circolari")
    
    			$Contenuto.='
    					<img src="'.Circolari_URL.'img/gruppo.png" style="border:0; float:left;" alt="Icona destinatari"/>
    					<p style="font-style:italic;font-size:0.8em;float:left;display:inline;margin:-2px 0px 0px 5px;">'.$Elenco.'
    					<!--<a href="LINK'.strtolower(str_replace(' ', '-', $Elenco)).'"></a>-->
    					</p>';
    
    		if (!post_password_required( $post->ID ))
    			$riassunto=	$post->post_excerpt;
    		else{
    			$riassunto="";
    		}
    		if (!empty($post->post_password))
    			$Contenuto.='
    					<img src="'.Circolari_URL.'img/protetto.png" style="border:0;" alt="Icona protezione"/>
    					<p style="font-style:italic;font-size:0.8em;display:inline;margin-top:3px;">Contenuto Protetto</p>';	
    //		if (Is_Circolare_per_User($post->ID))
    //	echo ">>>>>>>>>>>>>>>>>>>>>>>>>><<".$post->ID."<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><br />";return;
    			if (Is_Circolare_Da_Firmare($post->ID))
    				if (!Is_Circolare_Firmata($post->ID)) {
    					$ngiorni=Get_scadenzaCircolare($post->ID,"",True);					
    					if(Is_Circolare_Scaduta($post->ID)){
    						$Contenuto.='
    							<img src="'.Circolari_URL.'/img/firmabe.png" style="border:0;" alt="Icona firma o presa visione"/>
    							<p style="font-style:italic;font-size:0.8em;display:inline;margin-top:3px;color:red;">Scaduta e non Firmata</p>';						
    					}else{
    						switch ($ngiorni){
    							case -1:							
    								$entro="";							
    								break;													
    							case 0:
    								$entro="entro OGGI";
    								break;
    							case 1:
    								$entro="entro DOMANI";
    								break;
    							default:
    								$entro="entro $ngiorni giorni";
    								break;
    						}
    						if (get_post_meta($post->ID, "_sciopero",TRUE)=="Si")
    							$Tipo="Esprimere adesione $entro";
    						else
    							if (get_post_meta($post->ID, "_firma",TRUE)=="Si")
    								$Tipo="Firmare $entro";
    						$Contenuto.='
    							<img src="'.Circolari_URL.'/img/firmabe.png" style="border:0;" alt="Icona firma o presa visione"/>
    							<p style="font-style:italic;font-size:0.8em;display:inline;margin-top:3px;color:red;">'.$Tipo.'</p>';	
    				}			
    			}else{
    				$Contenuto.='
    					<img src="'.Circolari_URL.'/img/firmabe.png" style="border:0;" alt="Icona firma o presa visione"/>
    					<p style="font-style:italic;font-size:0.8em;display:inline;margin-top:3px;color:blue;">Firmata</p>';				
    			}
    			$Contenuto.='	
    			</div>
    			<div style="margin-bottom:5px;">
    				<em>'.$riassunto .'</em>
    			</div>
            
    			
    			<div style="height:30px;"></div>
    		</div></div><hr class="separacirc" />';
    		
    		
    		// H E R E   E N D   L O O P
    		
    	}
    }
    
    $Contenuto.= '
    		</div>
    		<div style="clear:both"></div>';
    
    return $Contenuto;
    }
    ?>
    

    I market on this code where START and END the loop so you can understand.
    I hope I explained myself

    Thread Starter Bluekid

    (@bluekid)

    someone could help me please 🙁

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Pagination for custom page’ is closed to new replies.