Вывод постов из кастомного типа записей
Вот так объявляю кастомный тип записи
add_action('init', 'my_custom_init');
function my_custom_init(){
register_post_type('flats', array(
'labels' => array(
'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' => 'Недвижимость'
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','excerpt')
) );
}
Вот так пытаюсь вывести посты
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ){ setup_postdata($post);
the_title();
echo get_field('object_name');
wp_reset_postdata();
}
?>
Но посты не выводятся. Как это исправить?
Ответы (2 шт):
Автор решения: KAGG Design
→ Ссылка
Надо указать тип записей, иначе по умолчанию выборка делается из стандартного типа post.
<?php
global $post;
$args = [
'post_type' => 'flats',
'posts_per_page' => 5,
'offset' => 1,
'category' => 1,
];
$myposts = get_posts( $args );
foreach ( $myposts as $post ) {
setup_postdata( $post );
the_title();
echo get_field( 'object_name' );
}
wp_reset_postdata();
Автор решения: Denis
→ Ссылка
Вот так выводится
<?php
global $post;
$args = [
'post_type' => 'flats',
'posts_per_page' => 5
];
$myposts = get_posts( $args );
foreach ( $myposts as $post ) {
setup_postdata( $post );
the_title();
echo get_field( 'object_name' );
}
wp_reset_postdata();