【问题标题】:Function paginate_links within a short code not showing where it should be短代码中的函数 paginate_links 没有显示它应该在哪里
【发布时间】:2014-07-21 04:49:20
【问题描述】:

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

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

这里是短代码 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)应该在带有block-grid类的div下的div内输出。

提前致谢!

【问题讨论】:

    标签: php wordpress pagination shortcode


    【解决方案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 */
    

    【讨论】:

    • 非常感谢你,救命!
    • 我仍然很好奇为什么......我在哪里可以阅读更多关于这种行为的信息?
    • 这是 PHP 基础知识。 "echo" 会立即显示内容,"return" 只会将其传递回调用它的地方。
    • 这里有一个很好的解释。 stackoverflow.com/questions/9387765/…
    • 很好,我现在明白了!干杯:)
    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 2010-12-25
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多