Skip to main content
Removing links to my working environment
Source Link

This website A website I'm a dev for uses a Wordpress theme (Salient by ThemeNectar) that includes the visual composer plugin. My job is essentially to make sure the site is just functional, so a while ago I wrote an extension for the visual composer. This extension uses WP_Query() to create a new query, looks for posts in specific categories (with a default established), and then returns the output of that query

This website I'm a dev for uses a Wordpress theme (Salient by ThemeNectar) that includes the visual composer plugin. My job is essentially to make sure the site is just functional, so a while ago I wrote an extension for the visual composer. This extension uses WP_Query() to create a new query, looks for posts in specific categories (with a default established), and then returns the output of that query

A website I'm a dev for uses a Wordpress theme (Salient by ThemeNectar) that includes the visual composer plugin. My job is essentially to make sure the site is just functional, so a while ago I wrote an extension for the visual composer. This extension uses WP_Query() to create a new query, looks for posts in specific categories (with a default established), and then returns the output of that query

Notice removed Draw attention by Morklympious
Bounty Ended with Pieter Goosen's answer chosen by Morklympious
added 1 character in body; edited title
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

WP_Query and Pagination on a Static front page (Extending Visual Composer...)

This is on a Saticstatic front page and I'm aware that the rules are different with 'page' and 'paged'

WP_Query and Pagination on a Static front page (Extending Visual Composer...)

This is on a Satic front page and I'm aware that the rules are different with 'page' and 'paged'

WP_Query and Pagination on a Static front page

This is on a static front page and I'm aware that the rules are different with 'page' and 'paged'

Notice added Draw attention by Morklympious
Bounty Started worth 50 reputation by Morklympious
Edited links out and included code for life of the question and ease of understanding the problem
Source Link

Here is the code for Here's the functioncode that generates the content with the query.renders posts and pagination inside that visual composer component

public function renderPosts( $atts, $content = null ) {
    global $post;
    setup_postdata($post);

     extract( shortcode_atts( array(
            'foo' => 5, //default of 5
            'categoryslug' => 'news-views' // Currently news and views.
    ), $atts) );
   
    // For getting the Query variable on a statcci front page, you have
    // to use 'page' and not 'paged'. Weird.          
    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;


    // Two things needed: Not using the game library,
    // Definitely using the supplied slug.
    // These are two objects.

    $dontUse = get_category_by_slug('game-library');
    $catUsed = get_category_by_slug($atts->categoryslug);
     
    // Args for the custom query
     $query_args = array(
       'posts_per_page'   => intval($foo),
       'category__not_in' => $dontUse->term_id,
       'cat' => $catUsed->term_id,
       'page' => $paged
     );
   
    $custom_query = new WP_Query($query_args);                      

            $output = '<div id="blogroll">';  
            while ( $custom_query->have_posts() ) : $custom_query->the_post();
                   
                $output .= "<div class='home_post col span_12 clear-both'>";
                $output .=      "<div class='col span_3'><a href='" . get_the_permalink() . "'>" . get_the_post_thumbnail(get_the_ID(), 'home_post_thumb') . "</a></div>";
                $output .=      "<div class='col span_9 col_last right-edge'>";
                $output .=        "<h2 class='home_post_header'>";
                $output .=           '<a href="' . get_the_permalink() . '">' . get_the_title() . "</a>";
                $output .=        "</h2>";
                $output .=        get_the_excerpt();
                $output .=      '<a class="home-more-link" href="' . get_the_permalink() . '"><span class="continue-reading">Read More</span></a>';
                $output .=      "</div>";
                $output .= "</div>";
                   
            endwhile;

            wp_reset_postdata();
            
            // Pagination not working, but it outputs just fine?
            $output .= '<div id="pagination" class="blogroll-pagination">' . home_pagination($custom_query) . '</div></div>';
            wp_reset_query();              

            return $output;  
 }
function home_pagination($query = null) {
    /*if ( !$query ) {
            global $wp_query;
            $query = $wp_query;
    } commented out because do I need this? */
   
    $big = 999999999; // need an unlikely integer

    $pagination = paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var( 'page' ) ),
            'total' => $query->max_num_pages,
            'prev_text' => '&laquo; Previous',
            'next_text' => 'Next &raquo;',
                                   
            ) );
   
    return $pagination;

} 

Here is the code for the function that generates the content with the query.

Here's the code that renders posts and pagination inside that visual composer component

public function renderPosts( $atts, $content = null ) {
    global $post;
    setup_postdata($post);

     extract( shortcode_atts( array(
            'foo' => 5, //default of 5
            'categoryslug' => 'news-views' // Currently news and views.
    ), $atts) );
   
    // For getting the Query variable on a statcci front page, you have
    // to use 'page' and not 'paged'. Weird.          
    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;


    // Two things needed: Not using the game library,
    // Definitely using the supplied slug.
    // These are two objects.

    $dontUse = get_category_by_slug('game-library');
    $catUsed = get_category_by_slug($atts->categoryslug);
     
    // Args for the custom query
     $query_args = array(
       'posts_per_page'   => intval($foo),
       'category__not_in' => $dontUse->term_id,
       'cat' => $catUsed->term_id,
       'page' => $paged
     );
   
    $custom_query = new WP_Query($query_args);                      

            $output = '<div id="blogroll">';  
            while ( $custom_query->have_posts() ) : $custom_query->the_post();
                   
                $output .= "<div class='home_post col span_12 clear-both'>";
                $output .=      "<div class='col span_3'><a href='" . get_the_permalink() . "'>" . get_the_post_thumbnail(get_the_ID(), 'home_post_thumb') . "</a></div>";
                $output .=      "<div class='col span_9 col_last right-edge'>";
                $output .=        "<h2 class='home_post_header'>";
                $output .=           '<a href="' . get_the_permalink() . '">' . get_the_title() . "</a>";
                $output .=        "</h2>";
                $output .=        get_the_excerpt();
                $output .=      '<a class="home-more-link" href="' . get_the_permalink() . '"><span class="continue-reading">Read More</span></a>';
                $output .=      "</div>";
                $output .= "</div>";
                   
            endwhile;

            wp_reset_postdata();
            
            // Pagination not working, but it outputs just fine?
            $output .= '<div id="pagination" class="blogroll-pagination">' . home_pagination($custom_query) . '</div></div>';
            wp_reset_query();              

            return $output;  
 }
function home_pagination($query = null) {
    /*if ( !$query ) {
            global $wp_query;
            $query = $wp_query;
    } commented out because do I need this? */
   
    $big = 999999999; // need an unlikely integer

    $pagination = paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var( 'page' ) ),
            'total' => $query->max_num_pages,
            'prev_text' => '&laquo; Previous',
            'next_text' => 'Next &raquo;',
                                   
            ) );
   
    return $pagination;

} 
Source Link
Loading