简体   繁体   English

如何添加不同的类/样式的WordPress的职位?

[英]how to add a different class / style to wordpress posts?

I am trying to add a different class to specific posts which would call through a different style. 我试图将不同的类添加到特定的职位,这将通过不同的样式进行调用。 I have played with the post_class function a little but i still have next to no clue how i could accomplish this, any ideas? 我已经使用了post_class函数,但是我几乎不知道如何完成此工作,有什么想法吗?

<?php if (have_posts()) : ?>
<?php $c = 0;while (have_posts()) : the_post(); $c++;
if( $c == 3) {
    $style = 'third';
    $c = 0;
}
else $style='';
?>
<div <?php post_class($style) ?> id="post-<?php the_ID(); ?>">

i tried adding this, the creating a custom style for it that just changed the color of the background and text 我尝试添加它,为其创建自定义样式,只是更改了背景和文本的颜色

Would I be able to edit how the posts look on the home page with these though, that's what i am after if so i could of fixed this quickly instead of finding that obscure code :D 我是否可以使用这些内容编辑帖子在主页上的外观,如果可以的话,这就是我想要的,所以我可以快速修复此问题,而不用找到晦涩的代码:D

就像您使用的动态ID。

<div class="post-class-<?php echo get_the_ID(); ?>" id="post-<?php the_ID(); ?>">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2 id="post-<?php the_ID(); ?>">

Outside your loop: 在循环之外:

$classes = array(
    0=>'first-column',
    1=>'second-column',
    2=>'third-column'
);
$i = 0;

In your loop : 在您的循环中:

<article id="post-<?php the_ID(); ?>" <?php post_class($classes[$i++%3]); ?>>

output like : 输出像:

<article class="first-column format-standard hentry category-uncategorized">
<article class="second-column format-standard hentry category-uncategorized">
<article class="third-column format-standard hentry category-uncategorized">
<article class="first-column format-standard hentry category-uncategorized">
<article class="second-column format-standard hentry category-uncategorized">
<article class="third-column format-standard hentry category-uncategorized">
<div <?php post_class($style) ?> id="post-<?php the_ID(); ?>">

check what class names this code is creating by using firebug or view source you'll probably see something like this 通过使用萤火虫或查看源代码来检查此代码正在创建的类名称,您可能会看到类似以下的内容

<div class="divclassname" id="post-206">

Now, in the css stylesheet loaded with the page you need to make up new rules based on either the value of class (divclassname) or id (post-205) as follows. 现在,在加载了该页面的css样式表中,您需要根据class(divclassname)或id(post-205)的值制定新规则,如下所示。

.divclassname {
background-color: #f3f3f3;
color: #ffffff;
}

#post-205 {
background-color: #cccccc;
color: #a19402;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM