Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何为wordpress帖子添加不同的类/样式?_Php_Css_Wordpress_Styles - Fatal编程技术网

Php 如何为wordpress帖子添加不同的类/样式?

Php 如何为wordpress帖子添加不同的类/样式?,php,css,wordpress,styles,Php,Css,Wordpress,Styles,我试图添加一个不同的类,通过不同的风格调用特定的帖子。我已经玩了一点post_类函数,但我仍然几乎不知道如何实现这一点,有什么想法吗 <?php if (have_posts()) : ?> <?php $c = 0;while (have_posts()) : the_post(); $c++; if( $c == 3) { $style = 'third'; $c = 0; } else $style=''; ?> <div <?php p

我试图添加一个不同的类,通过不同的风格调用特定的帖子。我已经玩了一点post_类函数,但我仍然几乎不知道如何实现这一点,有什么想法吗

<?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(); ?>">


就像您正在使用的动态Id一样

<div class="post-class-<?php echo get_the_ID(); ?>" id="post-<?php the_ID(); ?>">

在你的循环之外:

$classes = array(
    0=>'first-column',
    1=>'second-column',
    2=>'third-column'
);
$i = 0;
在您的循环中:

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

发布一些代码或您尝试过的内容,或您面临的问题。您可以使用通常由wordpress添加到正文中的常规post类。每个帖子都有一个唯一的ID和一个类,如
postd-206
。现在可以在选择器中使用此类来设置所需的样式。
<article id="post-<?php the_ID(); ?>" <?php post_class($classes[$i++%3]); ?>>
<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(); ?>">
<div class="divclassname" id="post-206">
.divclassname {
background-color: #f3f3f3;
color: #ffffff;
}

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