提问者:小点点

在WordPress帖子中添加php ifpost_class语句


我在Wordpress博客帖子上使用post_类在css类中显示帖子信息,如下所示:

<?php post_class('blog-post clearfix'); ?>

我还使用高级自定义字段来回响css类到每个博客文章中使用以下内容:

<?php if (get_field('blog-post-style') == 'big-post') {
    echo('big-post');
} else if (get_field('blog-post-style') == 'small-post') {
    echo('small-post');
} ?>

所以我可以点击一个单选按钮选项时编辑后和回声无论是大后或小后的css类。但我也想保持post_class。所以我需要以某种方式把这两个结合在一起。

我可以将帖子包装到另一个div中,并将acf代码回显到包含的div中,但我不希望这样做。

这就是我想要的样子:

<div id="post-429" class="small-post blog-post clearfix post-429 post type-post status-publish format-standard has-post-thumbnail hentry category-latest-news">

或者

<div id="post-429" class="big-post blog-post clearfix post-429 post type-post status-publish format-standard has-post-thumbnail hentry category-latest-news">

希望这有意义。谢啦


共3个答案

匿名用户

我用这个方法计算出来:

<div id="post-<?php the_ID(); ?>" class="<?php $allClasses = get_post_class(); foreach ($allClasses as $class) { echo $class . " "; } ?>blog-post clearfix <?php if (get_field('blog-post-style') == 'big-post') { echo('big-post'); } else if (get_field('blog-post-style') == 'small-post') { echo('small-post'); } ?>">

匿名用户

也可以将附加类作为post\u class()函数的第一个参数传递。它将自动将其添加到列表中。

<div <?php post_class( get_field('blog-post-style') ); ?>>

匿名用户

一种解决方案是将条件语句包装在函数中,然后将结果传递给post_类()。

post_类(get_my_class())

 function get_my_class(){
    $my_class = "";
    if (get_field('blog-post-style') == 'big-post') {
        $my_class = 'big-post';
    } else if (get_field('blog-post-style') == 'small-post') {
        $my_class = 'small-post';
    }else{ 
        $my_class = 'default-class';
    }
    echo $my_class;
 }

使用示例: