Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

<ul>
    <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>
</ul>

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?

share|improve this question
    
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

1 Answer 1

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.

<?php
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">
    <?php
    switch ( $wp_query->query_vars['section'] ) {
        case 'gallery':
            wpse_74829_the_section( 'gallery' );
            break;

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

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

        default:
            the_content();
            break;
    }
    ?>
</div>

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' ); ?>

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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