如何在get_posts函数中显示分页?

时间:2016-06-16 11:33:15

标签: wordpress

在我目前的项目中,我正在使用get_posts,并希望显示分页,但我无法实现这一目标。有人建议使用WP_Query,但我现在不想更改我的代码,是否有人知道如何在get_posts函数中显示分页?

function law_cp_gallery_shortcode( $atts, $content = null ) {
        $result = shortcode_atts( array(
            'width' => '1/2',
            'el_position' => '',
            'element_title' => '',
            'element_caption' => '',
            'show_title' => '',
            'num_posts' => '',
            'read_more' => '',
            'excerpt' => '',
            'num_characters' => '',
            'category_name' => '',
            'display_styles' => 'blog_gallery',
            'post_columns' => '',
            'thumbnail_position' => '',
            'menu_filter' => '',
            'show_post_title' => '',
            'show_description' => '',
            'pretty_photo' => ''
        ), $atts );

        extract( $result );
        global $wpdb,$post;
        $args = array();
        $stack = array();
        $stack_cat_all = array();
        $category_id = 0;

        if($category_name != 'All' && !empty($category_name)){
            $term = get_term_by('name', $category_name, 'category');
            $category_id = $term->term_id;

            $stack_cat_all = array('tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'terms' => $category_id,
                    'field' => 'term_id',
                )
            ),
            );
        }
        //Setting posts count
        if($num_posts == ""){
            $num_posts = 8;
        } else {
            $num_posts = $num_posts;
        }

        if($num_characters == ""){
            $num_characters = 120;
        } else {
            $num_characters = $num_characters;
        }

        $args = array(
            'post_type' => 'portfolio',
            'post_status' => 'publish',
            'orderby' => 'date',
            'order' => 'DESC',
            'posts_per_page' => $num_posts,
        );
        $push_args = array_slice($args, 0, 2, true) + $stack + $stack_cat_all + array_slice($args, 2, count($args) - 1, true) ;

        query_posts($push_args);
        if($show_title == 'yes'){
            $output = '<div class="section-title">';
            $output .= '<h3>'.$element_title.'</h3>';
            $output .= '<small>'.$element_caption.'</small>';
            $output .= '</div>';
        }
        if($menu_filter != 'hide'){
            $output .= '<div class="tnit-gallery-filter"><div class="row"><div id="filters" class="button-group"><button class="button is-checked" data-filter="*">All</button>';
            //$tax_terms = get_terms($taxonomy,$term_args);
            $args = array(
                'orderby'           => 'name',
                'order'             => 'ASC',
                'hide_empty'        => false,
                'exclude'           => array(),
                'exclude_tree'      => array(),
                'include'           => array(),
                'number'            => '',
                'fields'            => 'all',
                'slug'              => '',
                'parent'            => '',
                'hierarchical'      => true,
                'child_of'          => 0,
                'childless'         => false,
                'get'               => '',
                'name__like'        => '',
                'description__like' => '',
                'pad_counts'        => false,
                'offset'            => '',
                'search'            => '',
                'cache_domain'      => 'core'
            );
            $terms = get_terms('portfolio_category', $args);
            foreach($terms as $term) {
                $output .= '<button class="button" data-filter="'.'.'.$links = str_replace(" ", "-", $term->name) .'">' . $term->name .'</button>';
            }
            $output .= '</div></div></div>';
        }
        $output .= '<div class="isotope row">';
        if($post_columns == ''){
            $post_cols = "col-md-12";
        } else if($post_columns == 'two_column') {
            $post_cols = "col-md-6";
        } else if($post_columns == 'three_column') {
            $post_cols = "col-md-4";
        } else if($post_columns == 'four_column') {
            $post_cols = "col-md-3";
        }
        $loop = get_posts(array('post_type' => 'portfolio', 'posts_per_page' => $num_posts, 'post_status' => 'publish'));
        if ( !empty($loop) ) {

            foreach($loop as $lop) { setup_postdata( $post );
                $terms = get_the_terms( $lop->ID, 'portfolio_category' );
                $links = array();
                if (is_array($terms) || is_object($terms)) {
                    foreach ($terms as $term) {
                        $links[] = $term->name;
                    }
                }
                $links = str_replace(' ', '-', $links);
                $tax = join( " ", $links );
                //$feat_image = get_the_post_thumbnail($lop->ID, array(180,180));
                $output .= '<div class="element-item '. $tax. ' '.$post_cols.'" data-category="transition">';

                /*if($custom_url == ''){
                    $custom_url = $lop->guid;
                } else {
                    $custom_url = $custom_url;
                }*/
                if($thumbnail_position != 'below_title'){
                    if($pretty_photo != 'no') {
                        $output .= '<a href="' . $post_image = wp_get_attachment_url(get_post_thumbnail_id($lop->ID)) . '" rel="prettyPhoto[gallery1]"><img src="' . $post_image = wp_get_attachment_url(get_post_thumbnail_id($lop->ID)) . '"></a>';
                    } else {
                        $output .= '<a href="' .$lop->guid . '"><img src="' . $post_image = wp_get_attachment_url(get_post_thumbnail_id($lop->ID)) . '"></a>';
                    }
                }
                if($show_post_title != 'no') {
                    $output .= '<h3><a href="' . $lop->guid . '">' . $lop->post_title . '</a></h3>';
                }
                if($show_description != 'no') {
                    $output .= '<p>' . $content = substr($lop->post_content, 0, $num_characters) . '</p>';
                }
                if($thumbnail_position == 'below_title') {
                    if($pretty_photo != 'no') {
                        $output .= '<a href="' . $post_image = wp_get_attachment_url(get_post_thumbnail_id($lop->ID)) . '" rel="prettyPhoto[gallery1]"><img src="' . $post_image = wp_get_attachment_url(get_post_thumbnail_id($lop->ID)) . '"></a>';
                    } else {
                        $output .= '<a href="' . $lop->guid . '"><img src="' . $post_image = wp_get_attachment_url(get_post_thumbnail_id($lop->ID)) . '"></a>';
                    }
                }
                /*if($excerpt != '') {
                    $excerpt = $excerpt;
                } else {
                    $excerpt = '...';
                }*/
                if($read_more != '') {
                    $read_more = $read_more;
                } else {
                    $read_more = 'Read More';
                }
                if(($show_post_title != 'no') && ($show_description != 'no') ) {
                    $output .= '<a href="' . get_the_permalink() . '">' . $read_more . '</a>';
                }
                $output .= '</div>';
            }
        }

        $output .= '</div>';

        //$output .= '</div>';
        //$output .= '</div>';
        wp_reset_query();
        wp_reset_postdata();
        return $output;
    }

它庞大的代码,这就是为什么我无法在WP_Query中工作的原因:(

1 个答案:

答案 0 :(得分:0)

尝试更改查询参数,例如$ args:

$args = array( 
'posts_per_page' => 3,
'post_type' => 'post',
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )
   );

然后在循环后把这个:

if (function_exists('wp_pagenavi')) {
    wp_pagenavi();
}