在Wordpress Divi主题中组合项目的偏移和分页一起工作

时间:2018-05-26 15:00:28

标签: php wordpress

我正在使用wordpress网站,该网站必须使用Divi主题。由于某些原因,他们已经允许从模块前端抵消博客帖子,但他们不会为项目组合项目做。我的网站上有一个页面,其中应包含所有案例研究,基本上看起来像这样: enter image description here

所以,我基本上做的是我把第一行放在最新的投资组合项目上,然后我在第二行放了两个投资组合项目,我偏移了1个帖子,第3行是我的推荐内容。我的问题是关于第4排 - 我需要将投资组合项目抵消3个帖子,我必须有分页。问题是,当我偏移分页停止工作的那一刻。我尝试在互联网上收集片段和信息,但似乎没有任何效果。这是我尝试过的:

listoflist_member(Xss, X) :-
   ( Xs = [_] ; Xs = [_,_|_] ),           % new
   member(Xs, Xss),
   member(X, Xs).

投资组合项目被3个帖子抵消,但当我点击较旧的条目时,它不起作用,我仍然显示第一页中的相同项目(偏移3)。这是pastebin of the whole module's code

1 个答案:

答案 0 :(得分:1)

没有人想帮助我,所以我必须阅读wordpress文档2天,但这是值得的,我已经设法学习了许多新东西!如果有人需要,请输入以下代码:

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;
}

在我的孩子的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;
}

干杯!