wordpress将字段添加到post_class

时间:2014-05-29 10:08:37

标签: wordpress class post advanced-custom-fields

我需要在文章帖子中添加自定义字段,但不知道如何向其中添加其他类。

目前这些课程已经过了<?php post_class($classes); ?>

但是我还需要为此添加自定义字段。为了证明我添加了一个class =但是这不起作用,因为class =被添加了两次。

<?php post_class($classes); ?> class="<?php the_field( "size" ); ?>

所以我需要post_class和the_field一起工作。

3 个答案:

答案 0 :(得分:2)

感谢您的回答,但我找到了一种简单的方法

<?php post_class(get_field('field_name')); ?>

答案 1 :(得分:0)

你可以用两种不同的方式做到这一点,

首先: - theme's functions.php文件中添加以下代码:

这会将您的班级添加到调用post_class的位置。

function category_id_class($classes)
{
    global $post;
    if($post->post_type == 'post')
    {
        $classes[] = get_field( "size" );;
    }
    return $classes;
}
add_filter('post_class', 'category_id_class');

第二: - 将以下代码直接添加到您的页面中: -

$post_classes = get_post_class();
$post_classes= implode(' ', $post_classes);
echo 'class="'.$post_classes. the_field( "size" )'"';

希望这对你有所帮助。

答案 2 :(得分:0)

那么为什么你不能这样做:

<?php post_class(the_field( "size" )); ?>

因为它的工作原理如下:

<?php post_class('my_custom_class'); ?>