Support » Developing with WordPress » I want the “next” and “previous” buttons to appear in my custom navigation funct

  • I’m looking for a way to display the two buttons “previous” and “next” of the navigation of my page, but with the code below, I can only show one, I do not understand what’s wrong in the code (below):

    <div class="cover--nav">
            <?php
                $currenttPostId = get_the_ID();
                $theCategory = get_the_terms(get_the_ID(),'recipe_category');
    
                    global $wp_query;
    
                    $args = array(
                        'post_type' => 'recipe',
                        'orderby' => 'rand',
                        'post_status' => 'publish',
                        'recipe_category'=> !empty($theCategory)? $theCategory[0]->slug : '',
                        'post__not_in' => array($currenttPostId), 
                        'posts_per_page' => 2,
                        'paged' => $paged  //very important
                    );
                    $wp_query = new WP_Query($args);
    
                    $prevNext = array();
    
                    if ( $wp_query->have_posts() ) :
                        while ( $wp_query->have_posts() ) : $wp_query->the_post();
                            the_title();
                        endwhile;
                    endif;
    
                    wp_reset_postdata();
    
                    $prev_label = "<span class='cover--nav-label'>" . _e('Recette précédente', 'marque') . "</span>";
    
                    $prev_arrow = "<svg class='icon icon-arrow-prev' role='presentation' focusable='false'><use xlink:href='" . get_template_directory_uri() . "/images/symbol-defs.svg#icon-arrow-prev'></use></svg>";
    
                    $prev_text = $prev_label . $prev_arrow;
    
                    $next_label = "<span class='cover--nav-label'>" . _e('Recette suivante', 'marque') . "</span>";
    
                    $next_arrow = "<svg class='icon icon-arrow-next' role='presentation' focusable='false'><use xlink:href='" . get_template_directory_uri() . "/images/symbol-defs.svg#icon-arrow-next'></use></svg>";
    
                    $next_text = $next_label . $next_arrow;
    
                    the_posts_navigation( array(
                    'prev_text' => $prev_text,
                    'next_text' => $next_text,
                    ) );
    
                    ?>
    
            </div>
    • This topic was modified 11 months, 2 weeks ago by frenchywp.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi there!

    Could you please share the URL of the web page where I can check this issue, so I can take a closer look and suggest you a solution for this.

    Thanks,
    Narendra

    Hi,

    I work locally, so I can only communicate code and possibly images, but I also asked the question on the platform WordPress Stack exchange (or you can see what it gives in image) wordpress stack exchange . I also tried to use the functions “previous_posts_link” and “next_posts_link” and I have exactly the same result, there is only the “next” button that works, I have the impression that the problem comes wp-query

    • This reply was modified 11 months, 2 weeks ago by frenchywp.
    Moderator bcworkz

    (@bcworkz)

    You are right, the problem comes from WP_Query, but perhaps not in the way you think. Hardly any of the various pagination functions available in WP will work with custom queries like yours. The functions mostly work off the main query used to get a page and know nothing of your custom query that occurs on the template. The reason you only get “next” is there is no “previous” page in relation to the main query.

    The function you want to use with custom queries is paginate_links() because it does not rely upon WP_Query at all. You pass all the needed data as arguments.

    Hi,

    yes that’s what it seemed to me, thank you for your help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘I want the “next” and “previous” buttons to appear in my custom navigation funct’ is closed to new replies.