【问题标题】:Wordpress – applying a class to the first and last post of each rowWordpress – 将一个类应用于每行的第一个和最后一个帖子
【发布时间】:2014-01-23 22:16:34
【问题描述】:

我正在使用 960 网格系统构建一个 WordPress 主题。我有一个起始页,每行显示三个缩略图。这些缩略图位于带有父包装器的子网格中。

所以,我需要用一个名为“alpha”的类来包装每一行的第一篇文章,并用一个名为“omega”的类来包装每一行的最后一篇文章(在我的例子中是第三篇)。

知道如何解决这个问题吗?

这是我当前的代码,感谢任何帮助!

<?php get_header(); ?>


<div class="grid_12 projects"><!-- PROJECTS BEGINS -->

<?php
if (have_posts()) :
while (have_posts()) :
?>

<div class="grid_4 project">

<?php
the_post();
?>

<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>

<h2>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>

</div><!-- PROJECT ENDS -->

<?php
endwhile;
endif;
?>

</div><!-- PROJECT-CONTAINER ENDS -->

<?php get_footer(); ?>

【问题讨论】:

    标签: php wordpress 960.gs


    【解决方案1】:

    注意:如果您不希望最后一个元素始终包含 omega,但前提是它确实是一行中的第三个元素,请删除下面的 ||$ix==$wp_query-&gt;post_count

    <?php get_header(); ?>
    
    <div class="grid_12 projects"><!-- PROJECTS BEGINS -->
    
    <?php
    $ix = 0; // <<< add this
    if (have_posts()) :
    while (have_posts()) :
    
    $ix++; // <<< add this
    
    ?>
    
    <div class="grid_4 project<? // << add these lines
    echo ($ix == 1 || ($ix-1)%3)?' alpha':'';
    echo ($ix%3||$ix==$wp_query->post_count)?' omega':'';
    ?>">
    
    <?php
    the_post();
    ?>
    
    // nothing else changed
    

    如果您可以使用 CSS3,另一个可能更好的解决方案是 nth-child selector.

    .grid_12.projects .project:nth-child(3n+1) {
        /* alpha styling here */
    }
    
    .grid_12.projects .project:nth-child(3n+3) {
        /* omega styling here*/
    }
    

    【讨论】:

    • 通常你会这样做,将班级放在第一个孩子和最后一个孩子身上。但在这种情况下,我需要它是每行帖子的项目+阿尔法、项目、项目+欧米茄。否则会弄乱边距...
    • 我在发布答案后重新阅读了这个问题,并意识到我没有回答这个问题。我相信我现在已经这样做了。
    【解决方案2】:

    好的,试试这个:

    //Add thiscmets

    完整代码:

    <?php get_header(); ?>
    
    
    <div class="grid_12 projects">
    
    <?php
    if (have_posts()) :
    
    $i = 1; //Add this
    
    while (have_posts()) :
    
    //Add this
    if ($i === 1) {
        $new_class = "alpha";
      }
      elseif ($i % 3 == 0) {
        $new_class = "omega";
        $i = 0;
      } else {
        $new_class = "";
      }
    //End
    ?>
    
    <div class="grid_4 project <?php echo $new_class; ?>">
                               <!-- ^^^^^^^^^^^^^^^^^^^-->
    <?php
    the_post();
    ?>
    
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
    <?php the_post_thumbnail(); ?>
    </a>
    
    <h2>
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
    <?php the_title(); ?>
    </a>
    </h2>
    
    </div><!-- PROJECT ENDS -->
    
    <?php
    $i++; //Add this
    endwhile;
    endif;
    ?>
    
    </div><!-- PROJECT-CONTAINER ENDS -->
    
    <?php get_footer(); ?>
    

    【讨论】:

    • 这会将 alpha 放在第一个帖子上,将 omega 放在第二个帖子上。我需要一个中间没有 alpha/omega 类的东西。所以帖子编号 1:project+alpha 2:project 3:project+omega 4:project+alpha。等等……
    猜你喜欢
    • 2016-11-30
    • 2023-03-31
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多