How to Create a Restaurant Menu in Wordpress 3 using Custom Post Types and Taxonomies

Published Wednesday 18th of January 2012

I recently created a restaurant website in Wordpress and for the first time got to use Wordpress 3's new "Custom Post Types" feature. Here I thought I'd explain how you too can create a menu using the same.

We'll create a menu where each dish has a title, picture, description and price. Each dish will also be grouped under a category like "Meat" or "Fish".

First, we'll create our new post type for our dishes. Put this in your theme's functions.php file:

<?php
register_post_type
('dishes', array(
    
'label'                => __('Dishes'),
    
'singular_label'    => __('Dish'),
    
'public'            => true,
    
'show_ui'            => true,
    
'capability_type'    => 'post',
    
'hierarchical'        => false
    
'query_var'            => false
    
'supports'            => array(
        
'title',
        
'editor',
        
'excerpt',
        
'thumbnail',
        
'author',
        
'page-attributes'
    
)
));

Refer to the Wordpress Codex if you're curious about all the options.

That's all that's needed for a new menu item to show up in the admin; "Dishes". You could use "custom-fields" for the price but I think it's easier for the admin if we use the excerpt instead.

Now let's create a custom taxonomy for grouping our dishes:

<?php
register_taxonomy
('dish_types''dishes', array(
    
'label'            => __('Dish Type'), 
    
'hierarchical'    => true
));

That's it. Now when you add a new "Dish" you'll get to choose from your "Dish Types". Go ahead and add some new "Dish Types" and then add a couple of "Dishes" to each type.

Now we have everything we need in the admin / backend. All that's left is to display the menu on our website. We'll create a custom template for our menu called "menu.php". You can make it a copy of "page.php" so that the admin can still add text to the menu page. You need to set the "Template Name"-comment in the top of the file for Wordpress to recognize our new template:

<?php /* Template Name: Menu */ ?>

No let's write the code we need to fetch all our dish types as well as dishes:

<section id="menu">

    <?php $types get_terms('dish_types', array('orderby' => 'id')) ?>

    <?php foreach ($types as $type) : ?>
        <h3><?php echo htmlspecialchars($type->name?></h3>

        <?php if ($type->description) : ?>
            <p><?php echo htmlspecialchars($type->description?></p>
        <?php endif ?>

        <?php $dishes get_posts(array('post_type' => 'dishes''dish_types' => $type->name)) ?>

        <ul>
            <?php foreach ($dishes as $post) : setup_postdata($post?>
                <li>
                    <h4><?php the_title() ?></h4>

                    <?php the_post_thumbnail() ?>

                    <p class="price"><strong><?php the_excerpt() ?></strong></p>

                    <?php the_content() ?>
                </li>
            <?php endforeach; wp_reset_postdata() ?>
        </ul>
    <?php endforeach ?>

</section>

The code should hopefully be pretty self explanatory. The meat of it is first the call to get_terms() which retrieves all the terms for a taxonomy and get_posts() which is used to retrieve any post type based on params.

As you can see I use the_excerpt() to store the price of the dish. If you'd rather use a custom fields called "Price" you would display it like this: echo get_post_meta($post-&gt;ID, &apos;Price&apos;, true).

Happy Wordpressing!

Continue Reading »

Tags
Comments
No comments

About Me

Andreas Lagerkvist

I'm a Swedish web developer, spare-time php-developer, wanna-be designer and ex-3D-hobbyist. This is my website where I write about stuff that interests me.

Check out the latest article and post a comment, dig through the archives for more to read or check out the jQuery section for plenty of JS-fun.

Read more about me and the site

Slightly Older Articles

  1. HTML5 Base Wordpress Theme

    Published Tuesday 3rd of January 2012

    Like so many others I've now also written my own HTML5 starter theme for Wordpress.

    It's based on the Bones Wordpress Theme which is based on the HTML5 Boilerplate.

    A few things I've changed from Bones:

    • Removed all the class nam...

    Continue reading

  2. Sony and Google and the PS4

    Published Saturday 24th of December 2011

    I'd love it if Google and Sony could join forces on the PS4 (PSLife? :) and deliver super hardware (something like Nvidia's next) with Android / Google TV on top.

    All your saves and user profiles etc...

    Continue reading

  3. jQuery 3D Circle

    Published Monday 12th of December 2011

    I wanted to learn more about CSS 3D transforms so I started experimenting with the login page on SleekPHP.

    It all turned into a jQuery Plugin called "3D Circle". I haven't uploaded it to t...

    Continue reading

Random jQuery Plug-ins

  • Center

    This little pluggy centers an element on the screen using either fixed or absolute positioning. Can be used to display messages, pop up images etc.

    Details

  • Max Length Form Controls

    Gives form-controls with a 'maxlength-XXX'-class a max-length and prohibits user from entering more than set amount. It also displays number of charac...

    Details

  • Checked checkbox-parent

    This tiny plug-in adds and removes a 'checked'-class to input[type=checkbox]'s parents. Very useful if you want to style the entire wrapping <label>-e...

    Details

More Plug-ins

Recent Comments

  1. tramadol on "E3 Was Shite... As Usual"

    Aloha! kbi

  2. Gundosesd on "E3 Was Shite... As Usual"

    Aloha! aan

  3. Gundostkj on "E3 Was Shite... As Usual"

    Aloha!khbl! http://cokmla.com vcoej vbxyd

Page cached. Loaded in: 0.0087 second(s).
Last DB change: 2012-01-27 23:20:52
Last file change: 2011-10-03 07:42:35
Cache created: 2012-01-28 01:05:13