wordpress custom page list categories exclude

238
23 февраля 2018, 19:28

Please help me with this.

For now I have custom page where my categories listed with thumbnails. But I want to display only 3 categories from 5. So I need to exclude categories ID 1 and 103. I tried to put the code

array('exclude' => array( 1, 103 ),'parent' => '103','hide_empty'    => true, )

in 3 places... but nothing happened.

here is the code of the page:

<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<h1 class="border-radius-5"><?php the_title(); ?></h1>
    <div id="page" class="post-content">
        <?php the_content(); ?>
        <?php
            $terms = get_terms("category", $args);
            $count = count($terms);
            $categories = array(
            );
            if ($count > 0) {
            echo '<ul class="listing-cat">';
            foreach ($terms as $term) {
            $args = array(

                'post_type'        => 'post',
                'posts_per_page'   => 1,
                'show_count'       => 1,
                'orderby'          => 'rand',
                'post_status'      => 'publish',
                'tax_query' => array(
                                    array(
                                        'taxonomy' => 'category',
                                        'field' => 'slug',
                                        'terms' => $term->slug
                                    )
                                )
            );
            $video_from_categorie = new WP_Query( $args );
            if( $video_from_categorie->have_posts() ){
                $video_from_categorie->the_post();
            }else{}
            $term->slug;
            $term->name;
        ?>
    <li class="border-radius-5 box-shadow">
        <?php echo novavideo_get_post_image();?>
        <a href="<?php echo get_category_link( get_cat_ID($term->name) ); ?>" title="<?php echo $term->name; ?>"><span><?php echo $term->name; ?></span></a> 
        <span class="nb_cat border-radius-5"><?php echo $term->count; ?> <?php if ( $term->count > 1 ){
            _e( 'videos', novavideo_get_theme_name() );
        }else{
            _e( 'video', novavideo_get_theme_name() );
        } ?></span>                   
    </li>
<?php
    }
    echo '</ul>';
    echo '<div class="clear"></div>';
    }
?>        
</div><!-- #page --> 

Maybe I need to put my code at the certain place or maybe I should use another code. Please advice me.

Answer 1

В массиве параметров WP_Query можно указать имена выводимых категорий в лупе с помощью category_name. Можно и по ID с помощью cat, но с именами категорий код более читаемый.

$args = array(
    'post_type' => 'post',
    'posts_per_page' => 1,
    'show_count' => 1,
    'orderby' => 'rand',
    'post_status' => 'publish',
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => $term->slug
        )
    ),
    'category_name' => 'staff,news,other'
);

Документация: https://wp-kama.ru/function/wp_query#parametry-kategorij-rubrik

READ ALSO
Сортировка русских дат

Сортировка русских дат

Добрый день, возникла проблема такого характераИспользуется SQLite, мне нужно сортировать дату на русской локализации(с английской все ок отрабатывает)...

248
Curl+php не получает данные

Curl+php не получает данные

Почему не получается получить данные?

266
Помогите пожлуста [требует правки]

Помогите пожлуста [требует правки]

Необходимо разработать универсальный класс поиска пользователейНа входе передаются фильтры поиска в определенном вами формате

291
Селект и ajax запрос

Селект и ajax запрос

Вообщем такая задача http://prntscrcom/ii6jme при выборе селекта вакансии происходит фильтр значений и перебрасывает в начало страницы

250