<?
/*
Template Name: News
*/
?>
<? get_header(); ?>
<?php
// Запрашиваем продукты
$query = new WP_Query( [
'post_type' => 'news',
'posts_per_page' => 9,
'paged' => get_query_var( 'page' ),
] );
// Обрабатываем полученные в запросе продукты, если они есть
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();?>
<div class="news-item">
<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<p><?php $content = get_the_content(); echo mb_strimwidth($content, 0, 120, '.');?></p>
</div>
<?php }
wp_reset_postdata();
}
// Выводим пагинацию, если продуктов больше запрошенного количество
echo paginate_links( [
'base' => user_trailingslashit( wp_normalize_path( get_permalink() .'/%#%/' ) ),
'current' => max( 1, get_query_var( 'page' ) ),
'total' => $query->max_num_pages,
] );
?>
<? get_footer(); ?>
WP_Query()
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
the_posts_pagination();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
'has_archive' => 'news',
в функцию register_post_type()
add_action( 'init', 'register_post_types' );
function register_post_types(){
register_post_type( 'news', [
'label' => null,
'labels' => [
'name' => 'Новости', // основное название для типа записи
'singular_name' => 'Новость', // название для одной записи этого типа
'add_new' => 'Добавить новость', // для добавления новой записи
'add_new_item' => 'Добавление новости', // заголовка у вновь создаваемой записи в админ-панели.
'edit_item' => 'Редактирование новости', // для редактирования типа записи
'new_item' => 'Новое новость', // текст новой записи
'view_item' => 'Смотреть новости', // для просмотра записи этого типа.
'search_items' => 'Искать новости', // для поиска по этим типам записи
'not_found' => 'Не найдено', // если в результате поиска ничего не было найдено
'not_found_in_trash' => 'Не найдено в корзине', // если не было найдено в корзине
'parent_item_colon' => '', // для родителей (у древовидных типов)
'menu_name' => 'Новости', // название меню
],
'items_list_navigation' => 'true',
'description' => '',
'public' => true,
// 'publicly_queryable' => null, // зависит от public
// 'exclude_from_search' => null, // зависит от public
// 'show_ui' => null, // зависит от public
// 'show_in_nav_menus' => null, // зависит от public
'show_in_menu' => true, // показывать ли в меню адмнки
// 'show_in_admin_bar' => null, // зависит от show_in_menu
'show_in_rest' => null, // добавить в REST API. C WP 4.7
'rest_base' => null, // $post_type. C WP 4.7
'menu_position' => 4,
'menu_icon' => 'dashicons-megaphone',
//'capability_type' => 'post',
//'capabilities' => 'post', // массив дополнительных прав для этого типа записи
//'map_meta_cap' => null, // Ставим true чтобы включить дефолтный обработчик специальных прав
'hierarchical' => false,
'supports' => [ 'title', 'editor','thumbnail','excerpt' ], // 'title','editor','author','thumbnail','excerpt','trackbacks','custom-fields','comments','revisions','page-attributes','post-formats'
'taxonomies' => [],
'has_archive' => false,
'rewrite' => true,
'query_var' => true,
] );
'has_archive' => false,
, а я говорю укажите 'has_archive' => 'news',
, пересохраните ссылки и проверьте страницу /news/ if ( get_post_type() === 'news' ) {
get_template_part( 'templates/archive', 'news' );
} else {
get_template_part( 'templates/archive', 'common' );
}
<?php
/**
* The template for displaying archive pages
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package vulan
*/
get_header();
?>
<main id="primary" class="site-main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<?php
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description( '<div class="archive-description">', '</div>' );
?>
</header><!-- .page-header -->
<?php
/* Start the Loop */
while ( have_posts() ) :
the_post();
/*
* Include the Post-Type-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Type name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_type() );
endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</main><!-- #main -->
<?php
get_sidebar();
get_footer();
if ( get_post_type() === 'news' ) {
get_template_part( 'templates/archive', 'news' );
} else {
get_template_part( 'templates/archive', 'common' );
}
if ( get_post_type() === 'news' ) {
get_template_part( 'templates/archive', 'news' );
} else {
get_template_part( 'templates/archive', 'common' );
}
<?php
/**
* The template for displaying archive pages
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package vulan
*/
get_header();
?>
<main id="primary" class="site-main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<?php
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description( '<div class="archive-description">', '</div>' );
?>
</header><!-- .page-header -->
<?php
/* Start the Loop */
while ( have_posts() ) :
the_post();
/*
* Include the Post-Type-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Type name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_type() );
endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</main><!-- #main -->
<?php
get_sidebar();
get_footer();