1

I am building a site that is somewhat of a mix between a directory and a blog. Some posts are to be typical blog posts with text and images, without any particular structure. Other posts, however, are "restaurant" posts, which contain custom meta data fields for opening hours, review score, address, etc. I want these types of posts to have a very specific structure (i.e. image slider at top, followed by table, google map, etc.) that should be identical across every single "restaurant" post. I've thought of a couple of ways to achieve this:

  • Modify the "single.php" file to check whether the post is in the "restaurant" category, and then output content accordingly
  • Create a custom "restaurants" post type and then design the template file to my exact specifications

Are there any obvious issues that I will run into by taking any of these approaches? From a WP developer perspective, which would be the preferred option? Is there a better way to tackle this?

1
  • Both sound like fine solutions. Personally I would create a post type for it though because of just how custom the post is compared to a normal blog post ( i.e. slider, google map, table, post-meta, etc. ).
    – Howdy_McGee
    Jan 4, 2015 at 5:54

1 Answer 1

2

There is no situation ever that just have pro's, there will always be a con somewhere. Also, I don't think there is any wrong or right answer here and merely a fact of preference

I think the most important question to ask yourself is: "How is the rest of my structure going to look like"

If you just need to differentiate your posts in single view, your option one would be enough. Going with this option, you can do the following:

  • Create a category for the restaurant posts. Call it something like Restuarants

  • Instead of running a lot of conditional checks in single.php, create a custom single.php for that specific category, say single-restaurants.php

  • Use the single template filter to assign this single template to posts assigned to the Restaurant category

Example of that function

add_filter( 'single_template', function ($single_template) {

     global $post;

    if ( in_category( 'Restaurants' ) ) {
          $single_template = dirname( __FILE__ ) . '/single-restaurants.php';
     }
     return $single_template;

}, 10, 3 );

All posts which belongs to the Restaurants category will now use single-restaurants.php for single view

If you need more control and completely different structures, go with custom post types. Custom post types are excluded from the main query, except on taxonomy pages and custom post type archive pages. So you can have your restaurant posts completely separate from any other posts. Here you can make use of pre_get_posts to add you custom post type to the main query for specific templates

Here is an example to add your custom post type to the home page

add_action( 'pre_get_posts', function ( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {

        $query->set( 'post_type', array( 'post', 'my_post_type' ) );

    }
});

By default, if your post type is names restaurants, you can have a single template called single-restaurants.php, no extra code needed.

You should also have a look at the Template Hierarchy for assistance on how Wordpress chooses which templates to use where and how you can manipulate that

2
  • 1
    Thanks very much for the detailed response - very helpful!
    – Dan
    Jan 4, 2015 at 7:58
  • My pleasure, glad it was helpful. Enjoy :-) Jan 4, 2015 at 8:08

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.