0

In WordPress I can specify which page is used as my home page and which page will list all my posts.

I have created a Custom Post Type called 'Property'. I know I can access all of the posts with the Custom Post Type of 'Property' via a particular URL (/properties) and add a custom link to that page on my nav menu.

What I would really like is an extra section in the settings to allocate a particular page to list all the posts with the 'Properties' custom post type. That way I can use wp_list_pages as a fallback for my nav menu.

Is this possible?

EDIT:

Sorry, maybe I should've been more specific. I'm developing my own theme. I would like to be able to specify which page is used to list all the posts of the custom post type I have registered called 'Properties'. The code I use to register the post type is below.

$args = array(
        'labels' => array(
          'name'               => _x( 'Properties', 'post type general name', 'themename' ),
          'singular_name'      => _x( 'Property', 'post type singular name', 'themename' ),
          'menu_name'          => _x( 'Properties', 'admin menu', 'themename' ),
          'name_admin_bar'     => _x( 'Property', 'add new on admin bar', 'themename' ),
          'add_new'            => _x( 'Add New', 'book', 'themename' ),
          'add_new_item'       => __( 'Add New Property', 'themename' ),
          'new_item'           => __( 'New Property', 'themename' ),
          'edit_item'          => __( 'Edit Property', 'themename' ),
          'view_item'          => __( 'View Property', 'themename' ),
          'all_items'          => __( 'All Properties', 'themename' ),
          'search_items'       => __( 'Search Properties', 'themename' ),
          'parent_item_colon'  => __( 'Parent Property:', 'themename' ),
          'not_found'          => __( 'No properties found.', 'themename' ),
          'not_found_in_trash' => __( 'No properties found in trash.', 'themename' )
        ),
        'public'        => true,
        'has_archive'   => true,
        'rewrite'       => array(
          'slug'          => 'properties'
        ),
        'menu_icon'     => 'dashicons-admin-home',
        'menu_position' => 5,
        'supports'      => array(
          'title', 
          'editor', 
          'thumbnail'
        ),
        'taxonomies'    => array(
          'nc_prop_cat'
        )
      );
      register_post_type('nc_prop', $args);

I would like to add a another dropdown box in the reading settings of my theme (which is shown in the screenshot below). Currently my blog page lists all my posts and I would like my properties page to list all my properties.

Reading settings

To clarify I want to avoid using a hardcoded theme file such as page-properties.php so if the user deletes the page it doesn't break the theme.

  • In what settings of what theme? – Brad Dalton Feb 10 '15 at 17:10
  • Thanks for taking the time to look at my question, I have updated it @brad-dalton – Connel Feb 10 '15 at 23:09
2

The posts page is a special case that's hardcoded into core- it lets a page's main query be converted to a posts query. In this situation, the posts page ceases to be a "page" in the conventional sense- is_page is false, you can't access the page's content, etc.. Doing this page-query-to-posts-query conversion with the main query for any other sort of page is tricky, it involves quite a bit of request manipulation. I've never been able to make it work 100% without issue.

What you can do a bit more easily, is a more dynamic version of your page-properties.php template file, using template filters.

The idea is to store the ID of which page you'd like as your properties page, and filter the template hierarchy to override the template for that page with your custom template. This also has the bonus benefit of letting you use both the page's content, while running a secondary custom loop for the post type posts.

function wpd_properties_page_template( $page_template = '' ){
    // globalize the queried page object, so we can access its ID
    global $post;
    // check if this page's ID is equal to the value stored in the option
    if( $post->ID == get_option( 'my_page_id_option' ) ){
        // locate the special template
        // this also lets a child theme override the parent theme version
        $page_template = locate_template( 'your-special-template.php', false );
    }
    return $page_template;
}
add_filter( 'page_template', 'wpd_properties_page_template' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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