Php wordpress循环,每第三篇文章就应用一些新的东西

Php wordpress循环,每第三篇文章就应用一些新的东西,php,html,css,wordpress,Php,Html,Css,Wordpress,我有一个循环,它基本上在页面上显示一个块,其中有一个图像,classgallerypic在右边有一个20px的边距,它还有规则float:left,问题是每次创建第三个div时,它都会从新行开始,因为边距会将其推到那里。因此,理想情况下,每发第三篇文章我都不想留任何边距,而是使用divgallerypicright或其他什么 我想知道有没有人能解决这个问题?可能是一个更简单的方法,当它是第三个的时候,简单地阻止利润发生?我需要在其他两个空白,因为它创造了一个整洁的差距职位 <?php

我有一个循环,它基本上在页面上显示一个块,其中有一个图像,class
gallerypic
在右边有一个20px的边距,它还有规则
float:left,问题是每次创建第三个div时,它都会从新行开始,因为边距会将其推到那里。因此,理想情况下,每发第三篇文章我都不想留任何边距,而是使用div
gallerypicright
或其他什么

我想知道有没有人能解决这个问题?可能是一个更简单的方法,当它是第三个的时候,简单地阻止利润发生?我需要在其他两个空白,因为它创造了一个整洁的差距职位

<?php 
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $archive_query = new WP_Query('cat=14&showposts=14&paged=' . $paged);
            $id = get_the_ID();

while ($archive_query->have_posts()) : $archive_query->the_post(); ?>

                     <div class="events">
        <div class="gallerypic"><div class="limerickguideblockheader"><p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?>
        </div>
        <div class="gallerypiccontainer"><a href="<?php the_permalink(); ?>" >
         <?php echo get_the_post_thumbnail( $id, 'gallery-thumb', $attr ); ?> </a></div>
         </div> 
  </div>
            <?php endwhile; ?>


编辑:一张图片可以画1000字,这里是到目前为止的链接,有三个帖子。。。

如果可能的话,通过CSS的方法会更好。 干杯 Adrian尝试下面的代码

<style>
.gallerypicright {
  margin: 0;
}
</style>
...
<?php
$count = 0;
while ($archive_query->have_posts()) : $archive_query->the_post(); 
  $count++;
  $third_div = ($count%3 == 0) ? 'gallerypicright' : '';
?>
...
<div class="gallerypic <?php echo $third_div; ?>">

gallerypicright先生{
保证金:0;
}
...
...
请尝试下面的代码

<style>
.gallerypicright {
  margin: 0;
}
</style>
...
<?php
$count = 0;
while ($archive_query->have_posts()) : $archive_query->the_post(); 
  $count++;
  $third_div = ($count%3 == 0) ? 'gallerypicright' : '';
?>
...
<div class="gallerypic <?php echo $third_div; ?>">

gallerypicright先生{
保证金:0;
}
...
...

如果您想要一个纯CSS解决方案,可以尝试使用

.gallerypic:nth-child(3n + 1) {
    margin:0;
}
n
是为每个元素递增的计数器。计数器(
n
)从
0
开始,但页面上的元素从
1
开始,因此
3n+1
表示每个
3*n+1
元素,例如:

元素1(3*0+1)、4(3*1+1)、7(3*2+1)等

此解决方案仅在CSS3中可用,因此较旧的浏览器不会有此解决方案。(见:)

请注意,
:n子项
统计所有子项,因此应将事件分组到div中:

<div class="container">
    <div class="gallerypic">...</div>
    <div class="gallerypic">...</div>
    <div class="gallerypic">...</div>
</div>

...
...
...

如果您想要纯CSS解决方案,可以尝试使用

.gallerypic:nth-child(3n + 1) {
    margin:0;
}
n
是为每个元素递增的计数器。计数器(
n
)从
0
开始,但页面上的元素从
1
开始,因此
3n+1
表示每个
3*n+1
元素,例如:

元素1(3*0+1)、4(3*1+1)、7(3*2+1)等

此解决方案仅在CSS3中可用,因此较旧的浏览器不会有此解决方案。(见:)

请注意,
:n子项
统计所有子项,因此应将事件分组到div中:

<div class="container">
    <div class="gallerypic">...</div>
    <div class="gallerypic">...</div>
    <div class="gallerypic">...</div>
</div>

...
...
...

在循环中,你必须计算你的帖子,然后你必须将帖子总数按3进行模块化,并在给定的帖子中应用正确的类,例如


如果($cnt%3==0){$class='right'}

您必须在循环中计算您的帖子,然后您必须按3模块化帖子总数,并在给定帖子中应用正确的类,例如


如果($cnt%3==0){$class='right'}

您可以尝试在容器上添加一个平衡的负右边距,因此在您的情况下,可能

div.events { margin-right: -20px; }
或者,如果您可以处理稍微有点零碎的浏览器支持(仅IE9+支持,我认为在其他浏览器中更好),也许可以使用第n个child:

.gallerypic:nth-child(3n+3) { margin-right: 0px; }

您可以尝试在容器上添加一个平衡负右边距的技巧,因此在您的情况下,也许

div.events { margin-right: -20px; }
或者,如果您可以处理稍微有点零碎的浏览器支持(仅IE9+支持,我认为在其他浏览器中更好),也许可以使用第n个child:

.gallerypic:nth-child(3n+3) { margin-right: 0px; }

谢谢air4x,这是有道理的,但我得到了一个意想不到的错误';'在第98行的/var/www/dev_limerickfc/cms/wp content/themes/limerickfc/category-14.php中,创建
$third_div
的那一行语法是否正确?抱歉,因为我仍然只是在学习phpSorry,请将第二个
替换为
。我已经更新了代码。谢谢air4x,这是有道理的,但是我得到了一个意外的错误“;”在第98行的/var/www/dev_limerickfc/cms/wp content/themes/limerickfc/category-14.php中,创建
$third_div
的那一行语法是否正确?抱歉,因为我仍然只是在学习phpSorry,请将第二个
替换为
。我已经更新了代码。