1

我正在尝试为我们的 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 -->
4

2 回答 2

0

希望下面的代码对您有所帮助.. 循环通过帖子,然后为选项卡打印 div,然后尝试将剩余的选项卡放入变量中,并在 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;
?>
于 2012-10-27T19:21:03.727 回答
0

尝试这个。笔记:

  • 您只需要查询一次,但将帖子数设置为 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>
    

于 2012-10-27T16:16:45.393 回答