函数paginate_links在一个短代码中没有显示它应该在哪里

时间:2014-05-31 14:42:53

标签: php wordpress pagination shortcode

我创建了一个简短的代码来显示一个项目列表,它们是一个自定义的帖子类型" Projects"。短代码只是显示项目,我想在短代码中包含分页。一切都运行良好,除了分页显示在项目列表之前,它应该在列表之后输出。

如果内置在自定义页面模板中,一切都会有效。但是我使用短代码,它不会输出它应该的方式。

以下是PHP代码的简短代码:

if (!function_exists('shortcode_projects_list')) {

function shortcode_projects_list($atts, $content = null) {

    global $wp_query;
    global $options;

    $args = array(
        "type"                  => "standard", 
        "order_by"              => "date", 
        "order"                 => "DESC", 
        "number"                => "-1", 
        "category"              => "", 
        "selected_projects"     => ""
    );

    extract(shortcode_atts($args, $atts));

    $html = "";

    $_type_class = '';
    $_portfolio_space_class = '';

    if ($type == "standard"){
        $_type_class = " standard";
        $_portfolio_space_class = "portfolio_with_space";
    } elseif ($type == "standard_no_space"){
        $_type_class = " standard_no_space";
        $_portfolio_space_class = "portfolio_no_space";
    }
    if($type != 'standard_no_space') {

    $html .= "<div class='block-grid $_portfolio_space_class cf'>\n";

    if (get_query_var('paged')) {
        $paged = get_query_var('paged');
    } elseif (get_query_var('page')) {
        $paged = get_query_var('page');
    } else {
        $paged = 1;
    }
    if ($category == "") {
        $args = array(
            'post_type' => 'projects',
            'orderby' => $order_by,
            'order' => $order,
            'posts_per_page' => $number,
            'paged' => $paged
        );
    } else {
        $args = array(
            'post_type' => 'projects',
            'projects_cat' => $category,
            'orderby' => $order_by,
            'order' => $order,
            'posts_per_page' => $number,
            'paged' => $paged
        );
    }
    $project_ids = null;
    if ($selected_projects != "") {
        $project_ids = explode(",", $selected_projects);
        $args['post__in'] = $project_ids;
    }
    query_posts($args);
    if (have_posts()) : while (have_posts()) : the_post();
            $terms = wp_get_post_terms(get_the_ID(), 'projects_cat');
            $project_bg_color = get_post_meta(get_the_ID(), 'sbwp_project_bg_color', true);
            $project_text_color = get_post_meta(get_the_ID(), 'sbwp_project_text_color', true);
            $html .= "<article class='block-item third-width third-height cf";
            foreach ($terms as $term) {
                $html .= " portfolio_category_$term->term_id";
            }

            $title = get_the_title();
            $excerpt = get_the_excerpt();
            $post_featured_image = get_post_thumbnail_id(get_the_ID()); 
            if ($post_featured_image) {
                $project_thumbnail = wp_get_attachment_image_src( $post_featured_image, 'full', false);
                if ($project_thumbnail) (string)$project_thumbnail = $project_thumbnail[0]; 
            }
            $custom_portfolio_link = get_post_meta(get_the_ID(), 'qode_portfolio-external-link', true);
            $portfolio_link = $custom_portfolio_link != "" ? $custom_portfolio_link : get_permalink();
            $target = $custom_portfolio_link != "" ? '_blank' : '_self';

            $html .="' style='background-color: ".$project_bg_color."'>";

            $html .= "<a href='".$portfolio_link."' rel='bookmark' title='".$title."'>";
                $html .= "<div class='image' style='background-image: url(".$project_thumbnail.");'></div>";
                $html .= "<div class='text ".$project_text_color."'>";
                    $html .= "<p>".$excerpt."</p>";
                    $html .= "<span class='line ".$project_text_color."'></span>";
                    $html .= "<h1>".$title."</h1>";
                $html .= "</div>";
            $html .= "</a>";

            $html .= "</article>\n";

        endwhile;

        $html .= "</div>"; 
        $html .= "<div class='m-all t-all d-all last_col cf'>"; 
        $html .= bones_page_navi();
        $html .= "</div>";

    else:
    ?>
        <p><?php _e('Sorry, no posts matched your criteria.', 'sbwp'); ?></p>
    <?php
    endif;

    $html .= "</div>";

    wp_reset_query();
    }
    return $html;
} 
}

这是bones_page_navi()函数:

function bones_page_navi() {
    global $wp_query;
    $bignum = 999999999;
        if ( $wp_query->max_num_pages <= 1 )
    return;
    echo '<nav class="pagination">';
        echo paginate_links( array(
        'base'         => str_replace( $bignum, '%#%', esc_url( get_pagenum_link($bignum) ) ),
        'format'       => '',
        'current'      => max( 1, get_query_var('paged') ),
        'total'        => $wp_query->max_num_pages,
        'prev_text'    => '&larr;',
        'next_text'    => '&rarr;',
        'type'         => 'list',
        'end_size'     => 3,
        'mid_size'     => 3
        ) );
    echo '</nav>';
} /* end page navi */

The HTML output when using the short code can been seen here.

分页链接(nav)应该在div下使用块网格类输出。

提前致谢!

1 个答案:

答案 0 :(得分:1)

那是因为导航被回显,而不是返回。

function bones_page_navi() {

    global $wp_query;
    $bignum = 999999999;

     if ( $wp_query->max_num_pages <= 1 )
        return;

    $output = '';

    $output .= '<nav class="pagination">';
        $output .= paginate_links( array(
        'base'         => str_replace( $bignum, '%#%', esc_url( get_pagenum_link($bignum) ) ),
        'format'       => '',
        'current'      => max( 1, get_query_var('paged') ),
        'total'        => $wp_query->max_num_pages,
        'prev_text'    => '&larr;',
        'next_text'    => '&rarr;',
        'type'         => 'list',
        'end_size'     => 3,
        'mid_size'     => 3
        ) );
    $output .= '</nav>';

    return $output;

} /* end page navi */