Suporte » Outros assuntos » Paginação para tipos personalizados de post (custom post types)

  • Olá amigos entusiastas. Venho aqui trazer outra dúvida referente a tipos de posts personalizados. Estou tentando inserir a função abaixo para fazer a paginação da página inicial de um site cliente meu. O problema é que o link da barra de paginação não passa para a página seguinte. Como eu resolvo isso. Procurei algo no fórum mas não achei nada específico ao meu caso ou que ajudasse a resolver.

    function base_pagination() {
    global $wp_query;

    $big = 999999999; // This needs to be an unlikely integer

    // For more options and info view the docs for paginate_links()
    // http://codex.wordpress.org/Function_Reference/paginate_links
    $paginate_links = paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages,
    'mid_size' => 5
    ) );

    // Display the pagination if more than one page is found
    if ( $paginate_links ) {
    echo '<div class="pagination">';
    echo $paginate_links;
    echo '</div><!--// end .pagination -->';
    }
    }

Visualizando 7 respostas - 1 até 7 (de um total de 7)
  • Criador do tópico Lu Wegner

    (@n85)

    O Arquivo index do template é esse abaixo:

    <?php
    /**
     * The main template file.
     *
     * This is the most generic template file in a WordPress theme
     * and one of the two required files for a theme (the other being style.css).
     * It is used to display a page when nothing more specific matches a query.
     * E.g., it puts together the home page when no home.php file exists.
     * Learn more: http://codex.wordpress.org/Template_Hierarchy
     *
     * @package neogenfmk
     */
    
    get_header(); ?>
    
    <?php
    // WP_Query arguments
    $args = array (
    	       'post_type'              => 'imovel',
               'posts_per_page' => '4',
    	       'order'                  => 'DESC',
    	       'orderby'                => 'date',
    );
    // The Query
    $listing_loop = new WP_Query( $args );?>
    
    <div id="primary" class="content-area row">
    
        <main id="main" class="site-main" role="main">
    
            <?php if ( $listing_loop->have_posts() ) : ?>
    
            <?php while ( $listing_loop->have_posts() ) : $listing_loop->the_post(); ?>
    				<?php get_template_part( 'content', 'imovel' ); ?>
            <?php endwhile; ?>
    
            <?php ngwpfmk_paging_nav(); ?>
            <?php else : ?>
    
    	    <?php get_template_part( 'content', 'none' ); ?>
            <?php endif; ?>
    
        </main><!-- #main -->
    </div><br>
    <?php //get_sidebar(); ?>
    <?php get_footer(); ?>

    Não sei se entendi seu problema…

    No arquivo index.php, faça o seguinte modificação:

    //Pegando a página atual
    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    // WP_Query arguments
    $args = array (
    ‘post_type’ => ‘imovel’,
    ‘posts_per_page’ => ‘4’,
    ‘paged’=>$paged, //Filtra pela página atual
    ‘order’ => ‘DESC’,
    ‘orderby’ => ‘date’,
    );

    Isso deve corrigir a paginação.

    Criador do tópico Lu Wegner

    (@n85)

    Ainda não funciona, o retorno é 404! deixo abaixo a função inclusa no functions.php e a index modificada:

    function base_pagination() {
    global $listing_query;

    $big = 999999999; // This needs to be an unlikely integer

    // For more options and info view the docs for paginate_links()
    // http://codex.wordpress.org/Function_Reference/paginate_links
    $paginate_links = paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
    'current' => max( 1, get_query_var('paged') ),
    'total' => $listing_query->max_num_pages,
    'mid_size' => 5
    ) );

    // Display the pagination if more than one page is found
    if ( $paginate_links ) {
    echo '<div class="pagination">';
    echo $paginate_links;
    echo '</div><!--// end .pagination -->';
    }
    }
    //Fim functions.php
    <?php
    /**
    * The main template file.
    *
    * This is the most generic template file in a WordPress theme
    * and one of the two required files for a theme (the other being style.css).
    * It is used to display a page when nothing more specific matches a query.
    * E.g., it puts together the home page when no home.php file exists.
    * Learn more: http://codex.wordpress.org/Template_Hierarchy
    *
    * @package neogenfmk

    */
    // WP_Query arguments
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array (
    'post_type' => 'imovel',
    'pagination' => true,
    'paged' => $paged,
    'posts_per_page' => '4',
    'ignore_sticky_posts' => true,
    'order' => 'DESC',
    'orderby' => 'date',
    );

    get_header(); ?>
    <?php $listing_query = new WP_Query( $args );?>
    <?php include 'inc/sliderini.php'; ?>
    <div id="primary" class="content-area row">

    <main id="main" class="site-main" role="main">

    <?php if ( $listing_query->have_posts() ) : ?>

    <?php while ( $listing_query->have_posts() ) : $listing_query->the_post(); ?>
    <?php get_template_part( 'content', 'imovel' ); ?>
    <?php endwhile; ?>

    <?php ngwpfmk_paging_nav(); ?>
    <?php else : ?>

    <?php get_template_part( 'content', 'none' ); ?>
    <?php endif; ?>
    <?php base_pagination();?>
    </main><!-- #main -->
    </div>
    <?php //get_sidebar(); ?>
    <?php get_footer(); ?>

    O WP está construindo certo a estrutura de paginação, eu não estou conseguindo entender como estruturar o link para a próxima página.

    Não entendi porque está dando erro 404… Tem como você postar o link que está dando erro (só para eu ver a estrutura do mesmo)?

    Criador do tópico Lu Wegner

    (@n85)

    http://site.webimoveisrs.com.br/

    <br />
    <?php<br />
    /**<br />
     * The main template file.<br />
     *<br />
     * This is the most generic template file in a WordPress theme<br />
     * and one of the two required files for a theme (the other being style.css).<br />
     * It is used to display a page when nothing more specific matches a query.<br />
     * E.g., it puts together the home page when no home.php file exists.<br />
     * Learn more: http://codex.wordpress.org/Template_Hierarchy<br />
     *<br />
     * @package neogenfmk<br />
     */<br />
    // WP_Query arguments<br />
    $args = array (<br />
    	'post_type'              => 'imovel',<br />
    	'pagination'             => true,<br />
    	'paged'                  => get_query_var('paged'),<br />
    	'posts_per_page'         => '4',<br />
    	'ignore_sticky_posts'    => true,<br />
    	'order'                  => 'DESC',<br />
    	'orderby'                => 'date',<br />
    );<br />
    /** START Pagination Function */<br />
    function paginate_posts($total_pages, $current){</p>
    <p>	if ( $total_pages > 1 )  {</p>
    <p>		if ( !$current_page = get_query_var('paged') )<br />
    			$current_page = 1;<br />
    		$big = 999999999;</p>
    <p>		$permalink_structure = get_option('permalink_structure');<br />
    		$format = empty( $permalink_structure ) ? '&page=%#%' : 'page/%#%/';<br />
    		echo paginate_links(array(<br />
    			'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),<br />
    			'format' => $format,<br />
    			'current' => $current_page,<br />
    			'total' => $total_pages,<br />
    			'mid_size' => 2,<br />
    			'type' => 'plain',<br />
    			'prev_text'    => __('«'),<br />
    			'next_text'    => __('»'),<br />
    			));<br />
    	}<br />
    }<br />
    /** END Pagination Function */<br />
    get_header(); ?><br />
    <?php $listing_query = new WP_Query( $args );?><br />
    <?php include 'inc/sliderini.php'; ?><br />
    <div id="primary" class="content-area row"></p>
    <p>    <main id="main" class="site-main" role="main"></p>
    <p>        <?php if ( $listing_query->have_posts() ) : ?></p>
    <p>        <?php while ( $listing_query->have_posts() ) : $listing_query->the_post(); ?><br />
    				<?php get_template_part( 'content', 'imovel' ); ?><br />
            <?php endwhile; ?></p>
    <p>        <?php ngwpfmk_paging_nav(); ?><br />
            <?php else : ?></p>
    <p>	    <?php get_template_part( 'content', 'none' ); ?><br />
            <?php endif; ?></p>
    <p>        <?php if ( !$current_page = get_query_var('paged') )<br />
    		$current_page = 1;<br />
    	    paginate_posts($listing_query->max_num_pages, $current_page);?></p>
    <p>    </main> <!-- #main --><br />
    </div><br />
    <?php //get_sidebar(); ?><br />
    <?php get_footer(); ?><br />

    Criador do tópico Lu Wegner

    (@n85)

    Aqui eu upei o theme completo para apreciação:

    LINK

    Criador do tópico Lu Wegner

    (@n85)

    Depois de buscar ajuda no grupo do facebook WordPress Brasil e encontrar o Matheus Lopes conseguimos(ele conseguiu) resolver o problema. Estava faltando uma condicional que avaliasse a localização do usuário em relação ao sistema. Se o usuário está navegando na home, a função trata o paginador como page, se a navegação está em páginas do escopo, a função trata como paged

    Link para pastebin do código completo

    Meu agradecimento a todos que ajudaram a resolver esse pequeno e incômodo problema em especial ao Matheus Lopes que foi quem realmente resolveu essa questão.

Visualizando 7 respostas - 1 até 7 (de um total de 7)
  • O tópico ‘Paginação para tipos personalizados de post (custom post types)’ está fechado para novas respostas.