I am currently developing a site for a car dealership to list their inventory. I have setup the car up as a custom post type. Each post will have children (gallery, features, accessories, etc).

I want to have a nav on all of the pages like so:

    <li><a href="/ford-escape-lt/gallery/">Gallery</a>
    <li><a href="/ford-escape-lt/features/">Features</a>
    <li><a href="/ford-escape-lt/accessories/">Accessories</a>

It would be nice to just create a single template like single-car.php and then have the menu in there. The question is, how do I replace the first part of the menu (/ford-escape-lt) with the current page we are on? Is there any way to add classes to the current page item?

  • Hi and welcome :) Please, add the PHP code that will render that Html output. Create your single-car.php based on single.php, locate the relevant code, do your tests and update the Question. – brasofilo Dec 3 '12 at 22:04

I've done something similar, and here's how I handled it.

First off, instead of using child pages, I put all the data on one page, and I stored the would-be "children" as postmeta. My custom post type editing page had 7 or 8 editors in it. This kept the WordPress admin cleaner and it made it easier to edit my posts, since I didn't start the post in one place then have to create 7 more pages and edit them. I won't include that code, as it's covered well elsewhere.

I navigated the page through rewrite tags. Here is what was in my functions.php file for this (all sample code modified for your data):

add_action('init', 'wpse_74829_rewrite_additions');
function wpse_74829_rewrite_additions() {
    add_rewrite_tag( '%section%', '(gallery|features|accessories)' );
    add_rewrite_rule('^([^/]+)/(gallery|features|accessories)/?', 'index.php?car=$matches[1]&section=$matches[2]', 'top');

Next, I had a helper to display the content depending on the section.

function wpse_74829_the_section( $section ) {
    $content = get_post_meta( get_the_ID(), $section, true );
    if ( $content ) {
        $content = str_replace( ']]>', ']]&gt;', $content );
        echo apply_filters( 'the_content', $content );

And here's what the inside of my loop looked like, with regards to the content:

<div class="entry-content">
    switch ( $wp_query->query_vars['section'] ) {
        case 'gallery':
            wpse_74829_the_section( 'gallery' );

        case 'features':
            wpse_74829_the_section( 'features' );

        case 'accessories':
            wpse_74829_the_section( 'accessories' );


Lastly, I had a helper for my links:

function wpse_74829_the_section_url( $section ) {
    $url = get_permalink();
    if ( strpos( $url, '?') ) echo add_query_arg( 'section', $section );
    else echo $url . $section . '/';

And then I could link to the sections within the page like, <?php wpse_74829_the_section_url( 'gallery' ); ?>

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.