[Support request] Add pagination to shortcode

Home Forums Support [Support request] Add pagination to shortcode

Home Forums Support Add pagination to shortcode

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1793567
    Alberto

    Hello
    I’m trying to add a standard pagination to a shortcode of mine.
    In that shortcode I’m using WP_Query to list the post in certain CPT, and I want to add the pagination after that list.
    I’ve tried different suggestions of yours, like this one or this other, but I’ve not been able to display the pagination, so I understand that I’m doing something wrong.

    This is the URL: https://circuloesceptico.org/novo/noticias (where noticias is a page)
    And this is the current shortcode:

    function dom_listado_noticias( $atts, $content = null ){
        extract(shortcode_atts(array(
          'items' => 8,
          'tipo'  => 'noticias'
        ), $atts));
    
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        if( $tipo == 'noticias'):
            $consulta = new WP_Query( array(
                    'post_type'     => 'post',
                    'posts_per_page'    => $items,
                    'tax_query' => array(
                                array (
                                    'taxonomy' => 'category',
                                    'field' => 'slug',
                                    'terms' => 'noticias',
                                )
                            ),
                    'paged'         => $paged,
                    'offset'        => 1,
                ));
        elseif( $tipo == 'evento' ):
            $consulta = new WP_Query( array(
                    'post_type'     => 'evento',
                    'posts_per_page'    => $items,
                    'paged'         => $paged,
                    'offset'        => 1,
                ));
        endif;
    
        ob_start();
        ?>
    
        <?php if( $consulta->have_posts() ): ?>
            <div class="listado-artig row">  
        
            <?php while ( $consulta->have_posts() ) : $consulta->the_post();
                $id = get_the_ID();
                $imx = get_the_post_thumbnail_url($id,'large');
                $tit_aux = get_the_title();
                $tit = wp_trim_words( strip_tags($tit_aux), 11);
                if( has_excerpt($id) ){
                    $extracto = get_the_excerpt( $id );
                } else {
                    $extracto = wp_trim_words( strip_tags(get_the_content()), 20 );
                } ?>
                
                <div class="listado-artig-item">
                    <div class="listado-artig-item__imx"><img src="<?php echo $imx; ?>" /></div>
                    <div class="listado-artig-item-txt">
                        <a href="<?php the_permalink(); ?>"><div class="listado-artig-item-txt__tit"><?php echo $tit; ?></div></a>
                        <div class="listado-artig-item-txt__txt"><?php echo $extracto; ?></div>
                        <a class="button btn-cies" href="<?php the_permalink(); ?>"><?php _e('Leer más', 'circuloesceptico'); ?></a>
                    </div>
                </div>
                
            <?php endwhile; ?>
            </div>
    
        <?php else: ?>
            <div class=""><?php _e('No hay información de ese tipo', 'circuloesceptico'); ?></div>
        <?php endif;?>
    
        <div class="paxinador">
            <?php the_posts_pagination( array(
                'mid_size'  => 2,
                'prev_text' => __( 'Prev', 'circuloesceptico' ),
                'next_text' => __( 'Next', 'circuloesceptico' ),
            ) ); ?>
        </div>
        <!--div class="paxinador">
            <a href="https://circuloesceptico.org/novo/category/noticias/"><?php _e('Ver todas las ', 'circuloesceptico'); ?><span class="violeta"><?php _e('noticias y artículos', 'circuloesceptico'); ?> <i class="fas fa-arrow-right"></i></span></a>
        </div-->
    
        <?php
        wp_reset_postdata();
        return ob_get_clean();
    }
    add_shortcode('noticias', 'dom_listado_noticias');

    I’ll appreciate your help

    TIA

    #1794539
    David
    Staff
    Customer Support

    Hi there,

    those other methods provided were for adding pagination to the default theme archives loop.
    For it to work inside the shortcode you would need ajax pagination to update the list on the current page – which is how the WP Show Post plugin works – why not just use WPSP ?

    #1795810
    Alberto

    Hi, David
    Thanks for replying!
    Is there not a way to add the WP pagination to a shortcode?
    I would prefer not to use plugins except those strictly necessary. I’ve figured that there would be a way to add pagination to a shortcode via the WP functions…

    Do you recommend using the WPSP plugin?

    TIA

    #1795836
    David
    Staff
    Customer Support

    It can only be added using an Ajax call.
    Personally i would use the WPSP plugin 🙂

    #1811122
    Alberto

    Hi, David!
    Sorry for taking so long to reply.
    I was trying with WPSP but it doesn’t work fine if I try to use the offset option: The pagination doesn’t work.
    Also, I can’t use it with custom post types 🙁

    Any suggestions?

    Thanks in advance

    #1811452
    David
    Staff
    Customer Support

    There should be no problem with WPSP using a CPT – does it now show in the Post Type select box when creating the WPSP List ?

    Offset will break all pagination, and there is no easy workaround for that as it always disrupts the count value of page 2 onwards. Something that WP didn’t really consider when adding that function. The alternatives is to use a taxonomy term to exclude the posts you don’t want displayed.

    #1813822
    Alberto

    Hi David
    Thanks for your reply!
    Yes, your right and the CPT is showed when I create a new WPSP list.

    Now the problem is with that offset error, because I need to list all posts except the most recent one.

    Any suggestion?

    TIA

    #1814135
    Elvin
    Staff
    Customer Support

    Hi Alberto,

    the offset + pagination is a known bug for wp_query function. ALL plugins that uses this, not just WPSP suffers from this.

    You can browse around stackoverflow or wpsp forums to look for the workaround. 🙂

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.