簡體   English   中英

PHP顯示類別中的帖子

[英]PHP to show posts from category

我正在嘗試為我們的Wordpress網站編寫此滑塊,以在每個框中顯示類別中第一條,第二條和第三條最新帖子(在本例中為體育)。 無論我如何使用count變量,我都無法顯示其他帖子,只有第一個。 這是我正在使用的循環和代碼,頁面為smeharbinger.net/category/sports

<div class="tabbed">

<!-- tab 1 -->
<div class="t1">
<ul>
    <?php
$count = 1; 
$tabbedSportsQuery = new WP_Query('cat='.get_query_var('cat').'&showposts=1');
while ($tabbedSportsQuery->have_posts()) : $tabbedSportsQuery->the_post(); 
echo '<div class="t'.$count.'">';
echo the_post_thumbnail(array(665,500), array ('class' => 'alignnone'));
$count++; 
endwhile;

?>

</ul>
</div>

<!-- tab 2 -->
<div class="t2">
<ul>
    <?php
  $count = 2; 
$tabbedSportsQuery = new WP_Query('cat='.get_query_var('cat').'&showposts=1');
while ($tabbedSportsQuery->have_posts()) : $tabbedSportsQuery->the_post(); 
echo '<div class="t'.$count.'">';
echo the_post_thumbnail(array(665,500), array ('class' => 'alignnone'));
$count = 2; 
endwhile;

?>

</ul>
</div>

<!-- tab 3 -->
<div class="t3">
<ul>
    <?php
$count = 3; 
$tabbedSportsQuery = new WP_Query('cat='.get_query_var('cat').'&showposts=1');
while ($tabbedSportsQuery->have_posts()) : $tabbedSportsQuery->the_post(); 
echo '<div class="t'.$count.'">';
echo the_post_thumbnail(array(665,500), array ('class' => 'alignnone'));
$count++; 
endwhile;

?>

</ul>
</div>

<!-- The tabs -->
<ul class="tabs">
<li class="t1"><a class="t1 tab" ><h10><?php echo get_the_title($ID); ?></h10></a>        </li>
<li class="t2"><a class="t2 tab" ><h10><?php echo get_the_title($ID); ?></h10></a>  </li>
<li class="t3"><a class="t3 tab" ><h10><?php echo get_the_title($ID); ?></h10></a>  </li>
</ul>

</div><!-- tabbed -->

嘗試這個。 注意:

  • 你只需要查詢一次,但職位的數量設置為3, while循環也就那么通過這些給你循環。
  • $count++將計數從1增加到2到3。

     <?php $count = 1; $tabbedSportsQuery = new WP_Query(array( 'cat' => get_query_var('cat'), 'posts_per_page' => 3 )); while ($tabbedSportsQuery->have_posts()) : $tabbedSportsQuery->the_post(); ?> <div class="t<?php echo $count?>"> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(array(665,500), array ('class' => 'alignnone')); ?></a> </div> <?php $count++; endwhile; ?> <!-- The tabs --> <ul class="tabs"> <li class="t1"><a class="t1 tab" ><h10><?php echo get_the_title($ID); ?></h10></a> </li> <li class="t2"><a class="t2 tab" ><h10><?php echo get_the_title($ID); ?></h10></a> </li> <li class="t3"><a class="t3 tab" ><h10><?php echo get_the_title($ID); ?></h10></a> </li> </ul> 

希望下面的代碼對您有所幫助。循環遍歷帖子,然后為tab打印div,然后嘗試將其余的tab放入變量,並在while循環結束時打印該變量。

<?php
    $count = 1;
    $tabbedSportsQuery = new WP_Query(array(
        'cat' => get_query_var('cat'),
        'posts_per_page' => 3
    ));
    $out = '';
    while ($tabbedSportsQuery->have_posts()) : $tabbedSportsQuery->the_post();
?>
    <div class="t<?php echo $count?>">
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(array(665,500), array ('class' => 'alignnone')); ?></a>
    </div>
    <?php 
        if($count == 1) {   
             $out .= '<ul class="tabs"><li class="t'.$count.'">';
            } else {
                $out .= '<li class="t'.$count.'">';
            }   
            $out .= '<a class="t'.$count.' tab"><h10>'.get_the_title($ID).'</h10></a>'; 
            if($count == 3) {   
                $out .= '</li></ul>';
            } else {
                $out .= '</li>';
        } 
    ?>
<?php
    $count++;
    endwhile;
    print $out;
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM