wordpress - 如何在自定义帖子类型循环中将帖子标签添加为类?

标签 wordpress loops tags custom-post-type

我有一个自定义帖子类型的自定义循环,效果很好,但我想根据每个帖子的标记标签向 div 包装器 (.room_entry) 添加一个类。这是我到目前为止得到的:

我正在尝试添加“.get_the_tags()”。直接上课,不过好像不行。该类输出“数组”

//Display Rooms Feed
function rooms_function() {
 global $post;

 $html = "";

 $my_query = new WP_Query( array(
    'post_type' => 'room',
    'posts_per_page' => -1 ,
    'orderby'          => 'menu_order',
    'order'            => 'ASC',
));


 if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();

    $html .= "<div class='room_entry ". get_the_tags() ." '>";
    $html .= "<div class='room_image'>" .get_the_post_thumbnail() . "</div>";
    $html .= "<h4 class='room_title'>" . get_the_title() . " </h4>";
    $html .= "<p class='room_description'>" .get_field('description') . "</p>";
    $html .= "<a class='room_view' href=' ".get_permalink() ." '>View Details</a>";
    $html .= "<a class='room_book et_pb_button' target='_blank' href=' ".get_field('book_now') ." '>Book Now</a>";
    $html .= "</div>";
   endwhile;
 wp_reset_postdata();

endif;

return $html;
}

最佳答案

    if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();

//Add tags as class for single or multiple tags.
    $post_tags = get_the_tags(); 
    $class = array();
    for($i=0; $i < count($post_tags); $i++){
        $class[] = $post_tags[$i]->name; 
    } 
    $classfinal = implode(' ',$class);

        $html .= "<div class='room_entry ". $classfinal ." '>";
        $html .= "<div class='room_image'>" .get_the_post_thumbnail() . "</div>";
        $html .= "<h4 class='room_title'>" . get_the_title() . " </h4>";
        $html .= "<p class='room_description'>" .get_field('description') . "</p>";
        $html .= "<a class='room_view' href=' ".get_permalink() ." '>View Details</a>";
        $html .= "<a class='room_book et_pb_button' target='_blank' href=' ".get_field('book_now') ." '>Book Now</a>";
        $html .= "</div>";
       endwhile;
     wp_reset_postdata();

    endif;

关于wordpress - 如何在自定义帖子类型循环中将帖子标签添加为类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56119237/

相关文章:

docker - 如何删除 <none> 标签而不删除 docker 镜像本身?

git - 将二进制文件附加到 Git 标记

javascript - 在 Wordpress 图像 slider 中导致 JavaScript 冲突的插件

Python Selenium 处理带有长 URL 列表的超时异常

c - for循环不会停止

javascript - 来回循环的for循环

java - actionURL 标签在 Liferay 中有什么作用?

php - 尝试在 URL 中传递变量时出现 "sorry you are not allowed to access this page"

php - WordPress : Need to change text color from a PHP file

javascript - wordpress自定义模板引用外部js