Pagination Wordpress

Asked

Viewed 47 times

0

Good morning, you guys!

I’m having a problem some time to make a pagination with wordpress.

I’m trying to implement a stylized pagination, but I’m having a hard time making it work in a certain way.

function wp_pagination( $query=null, $wpcpn_posts=null )
{

    $big = 999999999;

    $published_posts = wp_count_posts()->publish;
    $posts_per_page = get_option('posts_per_page');
    $page_number_max = ceil($published_posts / $posts_per_page);


    $paginate = paginate_links(
        array(
            'base'      => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'type'      => 'array',
            'total'     => $page_number_max,
            'format'    => '?paged=%#%',
            'current'   => max( 1, get_query_var('paged') ),
            'prev_text' => __('«'),
            'next_text' => __('»'),
        )
    );
    if ( $page_number_max > 1 && $paginate ) {
        echo '
        <nav aria-label="Navegação da página" id="paginacao">
                            <ul class="pagination justify-content-center">
        ';
        foreach ( $paginate as $page ) {                        
            echo '<li class="page-item active"><a class="page-link rounded-circle" href="'. $page .'">'. $page .'</a></li>';
        }
        echo '
            </ul>
        </nav>
        ';
    }}



                        $args = array( 'numberposts' => '1' );
                        $recent_posts = wp_get_recent_posts( $args );
                        foreach( $recent_posts as $recent ){ 



                        <div class="card rounded" id="post-card">
                            <div class="row">
                                <a href="<?= get_permalink($recent["ID"]); ?>" class="col-12 col-sm-6"><img  src="<?= get_the_post_thumbnail_url($recent["ID"], 'full'); ?>" class="card-img-top" height="232" alt="Imagem de capa do card"></a>
                                <div class="card-body col-12 col-sm-6">
                                    <p><span><?= (get_the_category()[0]->name); ?></span></p>
                                    <a href="<?= get_permalink($recent["ID"]); ?>"><h5 class="card-title"><?= $recent['post_title']; ?></h5></a>
                                    <!-- Lembrar que a descrição do blog deve terno maximo 153 char-->
                                    <p class="card-text"><?= limitar_caracteres($recent['post_excerpt'], 153); ?></p>
                                    <a href="<?= get_permalink($recent["ID"]); ?>" class="card-text verMais">Ver mais</a>
                                </div>
                            </div>
                        </div>

The result is this:

inserir a descrição da imagem aqui

Could someone give me a hand, please :/

1 answer

1


Good night Moises!

A few days ago I wrote something on https://wordpress.stackexchange.com/questions/357161/problem-with-pagination-link-error-404/357197#357197

But basically this is it: In the list your template...

<?php
$actual_page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args= array(
    'post_type' => 'seu_post_type_aqui',
    'post_status' => 'publish',
    'posts_per_page' => get_option('posts_per_page'),
    'paged' => $actual_page
);
$query = new WP_Query($args);
?>

<?php wp_pagination($query->max_num_pages);?>

In your functions.php file :

<?php
if( !function_exists('wp_pagination') ): 
    function wp_pagination($pages = '', $range = 2) {
        $showitems = ($range * 2)+1;

        global $actual_page;
        if(empty($actual_page)) $actual_page = 1;

        if($pages == '') {
            global $wp_query;
            $pages = $wp_query->max_num_pages;
            if(!$pages)
            {
                $pages = 1;
            }
        }

        if(1 != $pages) {
            echo '<ul class="pagination">';
            if($actual_page > 2 && $actual_page > $range+1 && $showitems < $pages) echo '<li><a href="' . esc_url(get_pagenum_link(1)) . '"><span class="fa fa-angle-double-left"></span></a></li>';
            if($actual_page > 1 && $showitems < $pages) echo '<li><a href="' . esc_url(get_pagenum_link($actual_page - 1)) . '"><span class="fa fa-angle-left"></span></a></li>';

            for ($i = 1; $i <= $pages; $i++) {
                if (1 != $pages &&( !($i >= $actual_page + $range + 1 || $i <= $actual_page - $range - 1) || $pages <= $showitems )) {
                    echo ($actual_page == $i)? '<li class="active"><a href="#">' . esc_html($i) . '</a></li>' : '<li><a href="' . esc_url(get_pagenum_link($i)) . '">' . esc_html($i) . '</a></li>';
                }
            }

            if ($actual_page < $pages && $showitems < $pages) echo '<li><a href="' . esc_url(get_pagenum_link($actual_page + 1)) . '" class="next"><span class="fa fa-angle-right"></span></a></li>';
            if ($actual_page < $pages - 1 &&  $actual_page + $range - 1 < $pages && $showitems < $pages) echo '<li><a href="' . esc_url(get_pagenum_link($pages)) . '"><span class="fa fa-angle-double-right"></span></a></li>';
            echo '</ul>';
        }
    }
endif;
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.