I have created a custom page type (using post state) for my project, so that I can select the page in the Reading settings like you do for page for posts and also for the home page. This is all working fine, and I have assigned ACF fields to turn up only for this page type.
However, I want to use this page so that any amount of content can be added, but also so that the archive list/loop for the custom post type which this page will represent will display (think of this page as page for posts but for a custom post type) but how do I assign a template to this page in the same way that WordPress picks up the index.php
or home.php
or front-page.php
templates? I first started with archive-people.php
but I have a disconnect between the content saved against the page and the content in the archive.
The below is how I have created and assigned the state:
add_filter( 'display_post_states', function ( $post_states, $post ) {
if ( intval( get_option( 'people' ) ) === $post->ID) {
$post_states['people'] = __('People', 'people');
}
return $post_states;
}, 10, 2 );
add_action( 'admin_init', function () {
$id = 'people';
add_settings_field(
$id,
'People Page:',
function( $args ) {
$id = 'people';
wp_dropdown_pages( array(
'name' => 'People',
'show_option_none' => '— Select —',
'option_none_value' => '0',
'selected' => get_option( $id ),
) );
},
'reading',
'default',
array(
'label_for' => 'field-' . $id,
'class' => 'row-' . $id,
)
);
} );
add_filter( 'whitelist_options', function ( $options ) {
$options['reading'][] = 'people';
return $options;
} );
So I want to end up with a page similar to how wordpress knows that page_for_posts is X page in the admin, or woocommerce does the shop/account pages, for example - whilst I've the settings for this, how do I assign the new state to the php page in my theme? To clarify, I'm not using page attribute driven templates.
Currently this is a solution, which works, but feels unclean - in my page.php:
@php $people_page = (int) get_option('people'); @endphp
@if( $people_page === $post->ID )
@section('content')
@while(have_posts()) @php the_post() @endphp
@include('partials.custom-archive')
@endwhile
@endsection
@else //do the regular page