Wordpress短码分页

时间:2016-04-18 12:45:43

标签: wordpress

我使用高级自定义字段为自定义帖子创建了一个短代码。除分页外,所有工作都正常。我已经尝试了其他帖子中我可以看到的所有选项,但没有一个对我有用。分页正在定制帖子页面。

function link_carstwo( $atts ) {
extract(shortcode_atts(array(
    'cartype' => 'porsche',
    'section' => 'make'
), $atts));
$big = 999999999; // need an unlikely integer

echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
$list = ' ';
echo '<div id="car-container">
  <ul id="carlist">';
//Setup the query to retrieve the posts that exist under each term


        global $post;
    $posts = new WP_Query (array(
          'post_type' => 'cars',
          'orderby' => 'menu_order',
          'order' =>  'ASC',
          $section => $cartype,
          'post_status' => 'publish',
          'posts_per_page' => 9,
          'paged' => $paged,
          ));

        // Here's the second, nested foreach loop that cycles through the posts associated with this category
        while ( $posts->have_posts() ) { $posts->the_post();
         ////set up post data for use in the loop (enables the_title(), etc without specifying a post ID--as referenced in the stackoverflow link above)
$price = get_field('price', $post->ID);
$car_image = get_field('car_image', $post->ID);
$image_position = get_field('image_position', $post->ID);
$make = get_field('make', $post->ID);
$year = get_field('year', $post->ID);
$date_purchased = get_field('date_purchased', $post->ID);
$finance_type = get_field('finance_type', $post->ID);
$job_title = get_field('job_title', $post->ID);
$model = get_field('model', $post->ID);

$list .=  '<li class="carbox"> 
<p>TEST</p>
<div class="image2"  style="background-image:url(' . $car_image .');background-position: ' . $image_position . ' center;"></div>
<p class="car"> '.$make.' ' . $model . ' ' . $year . ' </br> £ ' . $price . '        ' . $date_purchased . '</p>
<p class="finance">' . $finance_type . '</p>
<p class="fademeSmall"> ' . $job_title . '</p>
<p class="linked"><a href="'. get_permalink($post->ID) .'" class="orangeButtonRound"></a></p>
</li>';

        } 
'</ul></div>';


return 


 '<div class="navigation centerWidth">' 
 . $list 
 .the_posts_pagination( array(
 'mid_size'  => 2,
 'prev_text' => __( '<', 'textdomain' ),
 'next_text' => __( '>', 'textdomain' ),
 ) ) 
 . '<div class="nav-previous">' . get_next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts' ) ) . '</div>'
 . '<div class="nav-next">' . get_previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>' ) ) . '</div>'
 . 'TEST</div>' .
 wp_reset_query();
 }



add_shortcode( 'car-gridtwo', 'link_carstwo' );

2 个答案:

答案 0 :(得分:0)

试试这个分页

            <?php
        global $wp_query;

        $big = 999999999; 
        echo paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages
            ) );

            ?>

答案 1 :(得分:0)

使用wp_query和全局变量的方式非常错综复杂。

while ( $post->have_posts() ) { $posts->the_post();

在while条件下你应该 $ posts - &gt; have_posts()而不是 $ post - &gt; have_posts()

您也不需要setup_postdata($post);

我不确定这会解决问题,其他一切似乎都很好。&#34;很好&#34;对我来说。

修改

仔细查看代码我刚刚注意到你的return语句在循环中,这是错误的,可能是你遇到分页问题的原因。只需在return语句之前关闭While块(显然在wp_reset_query之前删除最后一个右括号)。不要忘记做我之前建议的改变。