Сначала создадим кастомный тип записей (Custom Post Type), в functions.php добавляем:
// Creates activity Custom Post Type function activity_init() { $args = array( 'label' => 'Деятельность', 'public' => true, 'show_ui' => true, 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => array('slug' => 'activity'), 'query_var' => true, 'menu_icon' => 'dashicons-images-alt2', 'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes',) ); register_post_type( 'activity', $args ); flush_rewrite_rules(); }
Далее создадим внутри этого кастомного поста раздел Категории:
add_action( 'init', 'activity_init' ); register_taxonomy("activities", array("activity"), array("hierarchical" => true, "label" => "Категории деятельности", "singular_label" => "activity item", "rewrite" => true));
Обратите внимание, ОЧЕНЬ ВАЖНО! Если post_type равен «activity«, то taxonomy должен иметь ДРУГОЕ имя, например, во множественном числе «activities«. Иначе ничего работать не будет!
Теперь нам нужно создать шаблон для вывода категорий кастомных постов. Файл должен носить имя вида taxonomy-{taxonomy-name}, то есть если название моей таксономи (категории) — activities, то файл должен называться taxonomy-activities (не taxonomy-activity!)
Сделали файл, нужно запустить в луп (цикл) вывода постов наши кастомные аргументы:
<?php $term = $wp_query->queried_object; $getterm = $term->slug; // get current slug (E.g. activity_running) $posts = get_posts(array( "post_type" => "activity", 'tax_query' => array( array( 'taxonomy' => 'activities', 'field' => 'slug', 'terms' => $getterm, 'include_children' => true, 'operator' => 'IN' ), ), "numberposts" => "-1", 'post_status' => 'publish', 'order' => 'DESC', )); ?>
Ну а дальше обычный наш цикл вывода постов:
<div class="elements-list"> <?php foreach ($posts as $post){ setup_postdata($post); ?> <div class="el clearfix"> <a href="<?php the_permalink(); ?>"> <div class="pict" style="background-image:url(<?php echo(get_field("фотография")['url']); ?>);"></div> </a> <div class="txt"> <a href="<?php the_permalink(); ?>"> <h3><?php the_title(); ?></h3> </a> <p><?php the_excerpt(); ?></p> <a class="readmore semi" href="<?php the_permalink(); ?>">Читать далее</a> </div> </div> <?php } wp_reset_postdata(); ?> </div>
You must be logged in to post a comment.