0

Overview

I have a Custom Post Type called Locations. When the user visits /locations, I would like them to see some general information about all the locations. When the user visits /location/{a-location}, I would like them to see specific information about "a-location". Sounds easy enough.

1st Solution Attempt

I created a Locations page in the Wordpress dashboard to hold the content for the /locations url. I then created an archive-locations.php file and a single-location.php. archive-locations.php pulls info out of the Locations page and it lists all of the custom post type Locations. When the user clicks on a link for a location, the user gets redirected to /locations/{a-location} and single-location.php gets called. This almost does the trick, but it just doesn't feel right and it's creating some other problems.

The Problem

When a user visits /locations I would prefer to be working with a page template for the Locations page (instead of the archive-locations.php page). I'm using the Yoast plugin to specify a custom meta title and description for each page. Since archive-locations.php is being used, all of this meta gets ignored. If I were working with a page template, my meta info would get recognized correctly. Also, logically, it feels like I should be working on the Locations PAGE, not an archive. The Locations PAGE is it's own entity with much more content then just an archive of custom post types. To try and force it into an archive concept seems like a stretch.

2nd Solution Attempt

So I created a page-locations.php template for the Locations page and I was hoping that it would get called instead of the archive-locations.php. Unfortunately, it does not. I then tried removing the archive-locations.php thinking that maybe it was taking precedence. That didn't help either; Wordpress simply renders index.php. If I disable the Custom Post type and visit /locations, then my page template is called correctly. It looks like naming a custom post type the same name as a page causes Wordpress some issues. However, I need to be somewhat similar for the URL structures to work out. /locations needs to pull from a page template; /locations/{a-location} needs to pull from another php file.

Question

Is there any way to get page templates and custom post types with similar permalinks to work together? If not, I guess my only option is to enhance my header.php and be smarter about determining the title and description when I'm on /locations.

Here's the code that registers my custom post type in case I'm doing something wrong:

$args = array(
    'labels'        => $labels,
    'description'   => 'Holds our location specific data',
    'public'        => true,
    'menu_position' => 20,
    'supports'      => array('title', 'editor', 'thumbnail', 'page-attributes' ),
    'hierarchical'  => true,
    'has_archive'   => true,
    'rewrite'       => array('with_front' => false, 'slug' => 'locations') //remove /news/ from the permalink when displaying a custom post type
);
register_post_type( 'locations', $args );

(I also tried changing has_archive to false, but it didn't help.)

1 Answer 1

1

If you want your single post pages to be hierarchical children of /locations, then the easiest way is simply to use archive-locations.php.

I'm not sure why you're involving a "Locations" static page here, when you can modify the archive-locations.php template file in any way to suit your needs - including outputting a custom query loop.

And because you have complete control of the template, you can add your own custom meta description and title. For example:

Custom Title

function wpse124052_locations_title( $title, $sep ) {
    if ( ! is_post_type_archive( 'locations' ) ) {
        return $title;
    } else {
        return 'Your Custom Title Here';
    }
}
add_filter( 'wp_title', 'wpse124052_locations_title', 10, 2 );

Custom Meta

function wpse124052_locations_meta() {
    if ( is_post_type_archive( 'locations' ) ) {
        echo '<meta....'; // your meta tag/discription here
    }
}
add_action( 'wp_head', 'wpse124052_locations_meta' );
2
  • This helps a little, thank you. I need to involve a "Locations" page to make editing easy for my client. I've already given him other static pages and Yoast has attached Title and Description meta boxes to all of the pages. It's not user friendly if I have to tell him, "all of your pages except Locations can be editing this way. For Locations, you need to do it this way." From the user point of view, he sees all his pages in the same place. He doesn't care if it's being driven by an archive page template or a static page template
    – CodeSmith
    Commented Nov 21, 2013 at 19:32
  • I'm trying to figure this part out now, but without a static page called Locations, there doesn't seem to be anywhere for me to store a Title and Description meta box for the user it use.
    – CodeSmith
    Commented Nov 21, 2013 at 19:35

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.