2016-04-18 18 views
0

私は、高度なカスタムフィールドを持つカスタム投稿用のショートコードを作成しました。すべてが正しくページ分割を除いて動作します。私は他の投稿で見ることができるすべてのオプションを試しましたが、私のために働くものはありません。ページ設定はカスタムの投稿ページで機能しています。ワードプレスショートコードページ区切り

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'); 
+1

これはすべての車を一度に表示しているか、ページングしていて、次の/前のコントロールを表示していないか、それとも何か他のものですか? – Fabio

+0

こんにちは、車を表示しています。ページネーションではありません。私が生成されたコードを見ると、divはそこにありますが、それ以上の車がないかのように、読み込まれませんが、より多くのものがあります。 – Ruth

答えて

0

あなたはwp_queryとグローバル変数を使用している方法は非常に複雑と間違っている

  <?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 
      )); 

      ?> 
+0

残念ながら、これは機能しませんでした。 – Ruth

0

あなたのページネーションのためにこれを試してみてください。 > have_posts()

あなたはまた、setup_postdata($post);

を必要としない - $がを投稿するのではなく> have_posts() - while条件で

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

あなたは $投稿を持つべきです私はこれが問題を解決することを確信していません、他のすべては私にとって "良い"ようです。私はちょうどあなたのreturn文は、ループ内にあることに気づいたコードをよく見を持つ

EDIT

、それは間違っているとあなたがページネーションの問題を抱えている理由かもしれません。 returnステートメントの前にWhileブロックを閉じて(そして最後に閉鎖ブラケットを削除してから、wp_reset_queryの前に明示的に削除してください)。私が前に提案した変更を行うことを忘れないでください。

+0

私はforeach($ posts as $ post)を使ってみましたが、それでも動作しますが、ページ番号はまだありません – Ruth

+0

この回答の更新を確認し、問題が解決したら教えてください。 – Fabio

+0

まだ運がありません。 divを返しています。改ページはありません。私もthe_posts_paginationを試しました – Ruth