Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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_Wordpress_Colors_Title - Fatal编程技术网

Php 在wordpress中动态更改帖子标题颜色

Php 在wordpress中动态更改帖子标题颜色,php,wordpress,colors,title,Php,Wordpress,Colors,Title,我想像这样动态地改变文章标题的颜色 <h3 class="green">Post title 1</h3> <p>post Text</p> <h3 class="blue">Post title 1</h3> <p>post Text</p> <h3 class="orange">Post title 1</h3> <p>post Text</p>

我想像这样动态地改变文章标题的颜色

<h3 class="green">Post title 1</h3>
<p>post Text</p>

<h3 class="blue">Post title 1</h3>
<p>post Text</p>

<h3 class="orange">Post title 1</h3>
<p>post Text</p>

<h3 class="red">Post title 1</h3>
<p>post Text</p>

<h3 class="yello">Post title 1</h3>
<p>post Text</p>
文章标题1
后文

职位名称1 后文

职位名称1 后文

职位名称1 后文

职位名称1 后文

我限制帖子数量为5,所以当一个新帖子广告的标题颜色变为绿色时


任何想法我该如何做任何…….想法你可以通过以下方式来做: 首先为每篇文章添加一个自定义字段,并决定该文章标题的颜色

现在只需在查询中获取此元字段并更改标题样式

<h2 style="color:<?php echo $meta_color; ?>"> <?php the_title(); ?> </h2>

您只需执行以下操作即可:
首先为每篇文章添加一个自定义字段,并决定该文章标题的颜色

现在只需在查询中获取此元字段并更改标题样式

<h2 style="color:<?php echo $meta_color; ?>"> <?php the_title(); ?> </h2>

使用一个变量对post进行计数,当其5返回到零时。然后用它来响应不同的类。例如:

$post_count=0;
$class = "";
if (have_posts() ){....
$post_count .= 1;

if($post_count == 1) $class = "green";
// same with 2, 3, 4 and so on

<h3 class="<? echo $class; ?>">title</h3>      
<p>post Text</p> 

if($post_count == 4) $post_count = 0;

}// end loop
$post\u count=0;
$class=“”;
如果(have_posts()){。。。。
$post_count.=1;
如果($post_count==1)$class=“绿色”;
//与2、3、4等相同

用一个变量计算post,当它的5返回到0时。然后用它来回送不同的类。例如:

$post_count=0;
$class = "";
if (have_posts() ){....
$post_count .= 1;

if($post_count == 1) $class = "green";
// same with 2, 3, 4 and so on

<h3 class="<? echo $class; ?>">title</h3>      
<p>post Text</p> 

if($post_count == 4) $post_count = 0;

}// end loop
$post\u count=0;
$class=“”;
如果(have_posts()){。。。。
$post_count.=1;
如果($post_count==1)$class=“绿色”;
//与2、3、4等相同

虽然Dk Macadamias解决方案更加灵活(允许您在每篇文章的基础上更改颜色),但更自动的替代方案是让文章顺序决定并从预定数组输出颜色

在模板文件中:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3 class="<?php echo get_post_color_class(); ?>"><?php the_title(); ?></h3>
<?php endwhile; endif; ?>

虽然Dk Macadamias解决方案更加灵活(允许您在每篇文章的基础上更改颜色),但更自动的替代方案是让文章顺序决定并从预定数组输出颜色

在模板文件中:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3 class="<?php echo get_post_color_class(); ?>"><?php the_title(); ?></h3>
<?php endwhile; endif; ?>

如果你想在两种颜色之间切换,你可以这样做。这个函数实际上会计算页面上所有帖子的数量,而不是查看全局帖子ID

function get_post_color_class() { //Declare our custom function
  global $wp_query; //Import the global $wp_query object
  $colors = array('#D8D32B','#D12F4E'); //Declare our array of colors
  return $colors[($wp_query->current_post % 2 != 0) ? 0 : 1];
  //Return the item in our array with the same index as the current post in the loop
}
在我的例子中,我想在wordpess中更改帖子的边框颜色。我在content.php中这样做:

<article id="post-<?php the_ID(); ?>" <?php post_class( ); ?>
style="border-left-color:<?php echo get_post_color_class(); ?>" >
或者,您可以使用CSS解决此问题:


这应该更优雅、更快。

如果你想在两种颜色之间切换,你可以这样做。此功能实际上会计算页面上所有帖子的数量,而不会查看全局帖子ID

function get_post_color_class() { //Declare our custom function
  global $wp_query; //Import the global $wp_query object
  $colors = array('#D8D32B','#D12F4E'); //Declare our array of colors
  return $colors[($wp_query->current_post % 2 != 0) ? 0 : 1];
  //Return the item in our array with the same index as the current post in the loop
}
在我的例子中,我想在wordpess中更改帖子的边框颜色。我在content.php中这样做:

<article id="post-<?php the_ID(); ?>" <?php post_class( ); ?>
style="border-left-color:<?php echo get_post_color_class(); ?>" >
或者,您可以使用CSS解决此问题:


这应该更优雅、更快。

你的意思是,你想为一行中的每5篇文章的标题附加一个不同的类?你的意思是,你想为一行中的每5篇文章的标题附加一个不同的类?thanx alot@Valerius这对我很有帮助……两个答案非常有用,你的和rgdesignthanx alot@Valerius对我很有帮助……两个答案对我很有帮助,你的和rgdesignthanx@rgdesign它对我很有帮助……两个答案对你和Valeriusthanx@rgdesign它对我很有帮助……两个答案对你和Valerius都很有帮助