skip to Main Content

I’m working on a wordpress website, which has to use the Divi theme. For some reasons they’ve enabled offsetting the blog posts from the modules frontend, but they don’t do it for portfolio items. There’s a page on my site which should contain all the case studies and it basicly looks like that:
enter image description here

So, what I basicly did was that I’ve put on the first row the latest portfolio item, then I put on the second row two more portfolio items which I offset by 1 post, 3rd row is my testimonials content. My question is about the 4th row – I need to offset the portfolio items by 3 posts and I have to have pagination. The problem is that the moment I offset the items the pagination stops working. I tried gathering snippets and info all around the internet, but nothing seems to work. Here is what I’ve tried:

    // Native conditional tag only works on page load. Data update needs $conditional_tags data
    $is_front_page = et_fb_conditional_tag( 'is_front_page', $conditional_tags );
    $is_search     = et_fb_conditional_tag( 'is_search', $conditional_tags );
    $offset_number = 3;

    // Prepare query arguments
    $query_args    = array(
        'posts_per_page' => (int) $args['posts_number'],
        'post_type'      => 'project',
        'post_status'    => 'publish',
    );

    // Conditionally get paged data
    if ( defined( 'DOING_AJAX' ) && isset( $current_page[ 'paged'] ) ) {
        $et_paged = intval( $current_page[ 'paged' ] );
    } else {
        $et_paged = $is_front_page ? get_query_var( 'page' ) : get_query_var( 'paged' );
        $query_args['offset'] = ( ( $paged - 1 ) * intval( $posts_number ) ) + intval( $offset_number );
    }

The portfolio items get offsetted by 3 posts, but when I click older entries it doesn’t work and I’m still shown the same items from the first page (offsetted by 3). Here is a pastebin of the whole module’s code.

2

Answers


  1. Chosen as BEST ANSWER

    Nobody wanted to help me, so I had to read wordpress documentations for 2 days, but it was worth it, I've managed to learn many new stuff! Here's the code if someone ever needs it:

    static function get_portfolio_item( $args = array(), $conditional_tags = array(), $current_page = array() ) {
        global $et_fb_processing_shortcode_object;
    
        $global_processing_original_value = $et_fb_processing_shortcode_object;
    
        $defaults = array(
            'posts_number'       => 10,
            'include_categories' => '',
            'fullwidth'          => 'on',
            'offset_number'      => 3,
        );
    
        $args = wp_parse_args( $args, $defaults );
    
        // Native conditional tag only works on page load. Data update needs $conditional_tags data
        $is_front_page = et_fb_conditional_tag( 'is_front_page', $conditional_tags );
        $is_search     = et_fb_conditional_tag( 'is_search', $conditional_tags );
    
        // Prepare query arguments
        $query_args    = array(
            'posts_per_page' => (int) $args['posts_number'],
            'post_type'      => 'project',
            'post_status'    => 'publish',
            'offsetreduced'  => true,
        );
    
        // Conditionally get paged data
        if ( defined( 'DOING_AJAX' ) && isset( $current_page['paged'] ) ) {
            $et_paged = intval( $current_page[ 'paged' ] );
        } else {
            $et_paged = $is_front_page ? get_query_var( 'page' ) : get_query_var( 'paged' );
        }
    
        if ( $is_front_page ) {
            global $paged;
            $paged = $et_paged;
        }
    
        // support pagination in VB
        if ( isset( $args['__page'] ) ) {
            $et_paged = $args['__page'];
        }
    
        if ( ! is_search() ) {
            $query_args['paged'] = $et_paged;
        }
    
        if ( '' !== $args['offset_number'] && ! empty( $args['offset_number'] ) ) {
            /**
             * Offset + pagination don't play well. Manual offset calculation required
             * @see: https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
             */
            if ( $et_paged > 1 ) {
                $query_args['offset'] = ( ( $et_paged - 1 ) * intval( $args['posts_number'] ) ) + intval( $args['offset_number'] );
            } else {
                $query_args['offset'] = intval( $args['offset_number'] );
            }
        }
    
        // Passed categories parameter
        $include_categories = self::filter_invalid_term_ids( explode( ',', $args['include_categories'] ), 'project_category' );
    
        if ( ! empty( $include_categories ) ) {
            $query_args['tax_query'] = array(
                array(
                    'taxonomy' => 'project_category',
                    'field'    => 'id',
                    'terms'    => $include_categories,
                    'operator' => 'IN',
                )
            );
        }
    
        // Get portfolio query
        $query = new WP_Query( $query_args );
    
        // Format portfolio output, and add supplementary data
        $width     = 'on' === $args['fullwidth'] ?  1080 : 400;
        $width     = (int) apply_filters( 'et_pb_portfolio_image_width', $width );
        $height    = 'on' === $args['fullwidth'] ?  9999 : 284;
        $height    = (int) apply_filters( 'et_pb_portfolio_image_height', $height );
        $classtext = 'on' === $args['fullwidth'] ? 'et_pb_post_main_image' : '';
        $titletext = get_the_title();
    
        // Loop portfolio item data and add supplementary data
        if ( $query->have_posts() ) {
            $post_index = 0;
            while( $query->have_posts() ) {
                $query->the_post();
    
                $categories = array();
    
                $categories_object = get_the_terms( get_the_ID(), 'project_category' );
    
                if ( ! empty( $categories_object ) ) {
                    foreach ( $categories_object as $category ) {
                        $categories[] = array(
                            'id' => $category->term_id,
                            'label' => $category->name,
                            'permalink' => get_term_link( $category ),
                        );
                    }
                }
    
                // need to disable processnig to make sure get_thumbnail() doesn't generate errors
                $et_fb_processing_shortcode_object = false;
    
                // Get thumbnail
                $thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
    
                $et_fb_processing_shortcode_object = $global_processing_original_value;
    
                // Append value to query post
                $query->posts[ $post_index ]->post_permalink    = get_permalink();
                $query->posts[ $post_index ]->post_thumbnail    = print_thumbnail( $thumbnail['thumb'], $thumbnail['use_timthumb'], $titletext, $width, $height, '', false, true );
                $query->posts[ $post_index ]->post_categories   = $categories;
                $query->posts[ $post_index ]->post_class_name   = get_post_class( '', get_the_ID() );
    
                $post_index++;
            }
    
            $query->posts_next = array(
                'label' => esc_html__( '« Older Entries', 'et_builder' ),
                'url' => next_posts( $query->max_num_pages, false ),
            );
    
            $query->posts_prev = array(
                'label' => esc_html__( 'Next Entries »', 'et_builder' ),
                'url' => ( $et_paged > 1 ) ? previous_posts( false ) : '',
            );
    
            // Added wp_pagenavi support
            $query->wp_pagenavi = function_exists( 'wp_pagenavi' ) ? wp_pagenavi( array(
                'query' => $query,
                'echo' => false
            ) ) : false;
        } else if ( wp_doing_ajax() ) {
            // This is for the VB
            $query = array( 'posts' => self::get_no_results_template() );
        }
    
        wp_reset_postdata();
    
        return $query;
    }
    

    And in my childs function.php:

    add_filter('found_posts', 'adjust_offset_pagination', 1, 2 );
    function adjust_offset_pagination($found_posts, $query) {
      if ( $query->get( 'offsetreduced' ) ) {
        return $found_posts - 3;
      }
      return $found_posts;
    }
    

    Cheers!


  2. You just need to use blog module to do this!
    Set it on "Projects".
    Good Luck!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search