您想向 WordPress 主题中的帖子添加奇数和偶数类吗?添加奇数和偶数类可以让您以不同的方式设置其他帖子的样式。在本文中,我们将向您展示如何在 WordPress 主题中的帖子中添加奇数/偶数类。
您想为 WordPress 主题中的帖子添加奇数和偶数类吗?添加奇数和偶数类可以让您以不同的方式设置所有其他帖子的样式。在本文中,我们将向您展示如何向 WordPress 主题中的帖子添加奇数/偶数类别。

为什么要在WordPress主题的帖子中添加奇/偶类? (Why Add Odd/Even Class to Your Posts in WordPress Themes?)
许多 WordPress 主题使用旧的甚至类来进行 WordPress 评论。它可以帮助用户直观地看到一条评论的结束位置和下一条评论的开始位置。
许多 WordPress 主题使用旧的甚至 WordPress 注释类。它可以帮助用户直观地看到一条评论的结束位置和下一条评论的开始位置。
同样,您可以在 WordPress 帖子中使用此技术。它看起来美观,可以帮助用户快速浏览包含大量内容的页面。它对于杂志或新闻网站的主页特别有帮助。
同样,您可以在 WordPress 帖子中使用此技术。它看起来美观,可以帮助用户快速浏览包含大量内容的页面。对于杂志或新闻网站的主页特别有用。
话虽如此,让我们看看如何在 WordPress 主题中的帖子中添加奇数和偶数类。
话虽如此,让我们看看如何向 WordPress 主题中的帖子添加奇数和偶数类别。
在WordPress主题的帖子中添加奇/偶类 (Adding Odd/Even Class to Posts in WordPress Theme)
WordPress generates 默认 CSS 类 and adds them to different items on your website on the fly. These CSS classes help plugin and theme developers add their own styles for different items.
WordPress会生成默认 CSS 类 ,并将它们动态添加到您网站上的不同项目中。 这些CSS类可帮助插件和主题开发人员为不同的项目添加自己的样式。
WordPress also comes with a function called post_class
, which is used by theme developers to add classes to post item. See our guide on how to 每个 WordPress 帖子的样式都不同.
WordPress还附带了一个名为post_class
的函数,主题开发人员可使用该函数为发布项目添加类。 请参阅我们的指南,了解如何为为每个 WordPress 帖子设置不同的样式 。
The post_class
is also a 筛选, which means you can hook your own functions to it. This is exactly what we will be doing here.
post_class
也是一个筛选 ,这意味着您可以将自己的函数连接到它。 这正是我们在这里要做的。
Simply add this code to your theme’s 函数.php file or a 特定于站点的插件.
只需将此代码添加到主题的函数.php文件或具体的于只需插入网站即可 。
function oddeven_post_class ( $classes ) {
global $current_class;
$classes[] = $current_class;
$current_class = ($current_class == 'odd') ? 'even' : 'odd';
return $classes;
}
add_filter ( 'post_class' , 'oddeven_post_class' );
global $current_class;
$current_class = 'odd';
该函数只是将奇数添加到第一个帖子中,然后添加偶数,依此类推。
此函数仅在第一个帖子中添加奇数,然后添加偶数,依此类推。
您可以在站点的源代码中找到奇数类和偶数类。只需将鼠标移至帖子标题,然后右键单击以选择“检查”或“检查元素”。
您可以在网站的源代码中找到奇数类和偶数类。只需将鼠标移到帖子标题上,然后右键单击以选择“检查”或“检查元素”。

Now that you have added even and odd classes to your posts. The next step is to style them using CSS. You can add your custom CSS to your child theme’s stylesheet, or by using 简单的自定义 CSS plugin.
现在,您已经在帖子中添加了偶数和奇数类。 下一步是使用CSS 样式 。 您可以将自定义CSS添加到子主题的样式表中,或使用简单的自定义CSS插件。
以下是一个示例 CSS,您可以将其用作起点:
下面是一个示例 CSS,您可以将其用作起点:
.even {
background:#f0f8ff;
}
.odd {
background:#f4f4fb;
}
这是它在我们的测试站点上的样子:
这是它在我们的测试站点上的样子:

If you don’t know how to use CSS, then you may want to check out CSS 英雄. It allows you to add CSS to any part of your WordPress site without writing any code.
如果您不知道如何使用CSS,则可以查看CSS 英雄 。 它允许您将CSS添加到WordPress网站的任何部分,而无需编写任何代码。
We hope this article helped you learn how to add odd/even class to your posts in WordPress themes. You may also want to see our guide on 如何设置 WordPress 评论布局的样式.
我们希望本文能帮助您学习如何在WordPress主题的帖子中添加奇/偶类。 您可能还想查看有关如何设计 WordPress 评论布局指南。
If you liked this article, then please subscribe to our YouTube 频道 for WordPress video tutorials. You can also find us on 推特 and Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube 频道 WordPress视频教程。 您也可以在推特和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-themes/how-to-add-oddeven-class-to-your-post-in-wordpress-themes/