@Genri_Rus

Как выводить у произвольного шаблона страницы — произвольный тип записей?

Есть папка page-templates, в ней - template-main.php, template-news.php

Я зарегистрировал с помощью post_type новый тип записей - "Новости"

Также в админке создал страницу Новости, подключил для нее шаблон: template-news.php

Проблема заключается в следующем, нужно в template-news.php вывести произвольный тип записей

В качестве темы, я взял шаблон у underscores, у них есть папка template-parts с файлами:

-content.php
-content-none.php
-content-page.php
-content-search.php

В archive.php есть такая строчка кода

<div id="primary" class="content-area">
		<main id="main" 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 -->
	</div><!-- #primary -->

Я вот не могу понять, почему-то archive.php перебивает в моей папке файл template-news.php, в котором я хочу вывести произвольные записи

Как вывести в template-news.php произвольный тип записей ?
  • Вопрос задан
  • 343 просмотра
Решения вопроса 1
dimovich85
@dimovich85
https://u-academy.net/
1. Получить записи в виде массива. Через get_posts, или WP_Query.
2. Организовать цикл. Либо через foreach, либо через while( have_posts() )...
3. Вывести все, что нужно либо через поля объекта-записи, либо настроив глобальные переменные (setup_post_data) и через функции шаблона (the_title, the_content...).
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы