Как добавить теги записей в качестве класса в пользовательский цикл типов записей? - PullRequest
3 голосов
/ 13 мая 2019

У меня есть пользовательский цикл для пользовательского типа записи, который отлично работает, но я хотел бы добавить класс к оберткам div (.room_entry) на основе теговых тегов для каждого сообщения. Вот что я получил до сих пор:

Я пытаюсь добавить ". Get_the_tags ()." непосредственно в классе, но это не похоже на работу. Класс выводит 'Array'

//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;
}

1 Ответ

1 голос
/ 13 мая 2019
    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;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...