0

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
1
  • Your question is not clear to me
    – IXN
    Commented Sep 30, 2019 at 10:58

1 Answer 1

0

Your question does not seem very clear to me but I believe this is what you are trying to do.

You've created a custom post-type: people. You want to have a template which displays the the archive list as well as the full content of a post which is also assigned to this template. If this sounds right, I think you could continue with what you were trying before, making use of the archive-people.php template.

You'd use archive-people.php to display the archive list of posts for this custom post-type as normal. To get and display content from a specific post/page, either before or after the archive listing, you can use the post ID of the post from which you want to display the content like the below:

// This is the ID of the post from which we want to get the post_content.
$my_post_id = 12; 

// Get the content for our specific post ID.
$my_post_content = apply_filters('the_content', get_post_field('post_content', $my_post_id));

// To print or display the content
echo $my_post_content;
1
  • Not quite, I don't want to hard code the value, I want to ignore the archive entirely as I need to use more than just content from the page itself including the slug etc. What I want to is to define my template by the page state I've defined in my code, similar to how WooCommerce does it's checkout/account/shop pages. But thank you for the input! This is essentially the 'unclean' code I've already got in place.
    – Aravona
    Commented Sep 30, 2019 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.