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 have a custom post type, for examle, products. They can be multilevel, like pages, f.e.,

Products (custom post type)

    Books
    |- LOTR
    |- Sherlock Holmes
    |- Cooking help
    |- etc

    CD
    |- Iron maiden
    |- AC/DC
    |- Brainstorm
    |- etc

    Manuals
    |- MS Word manual
    |- MS Excel manual
    |- PHP manual

I need a seperate pages for first level of hierachy (Books, CD, manuals), what i can achieve by creating a file archive-products.php in theme directory and then making a new query with parameter to get only first level.

The problem is with the second level. I cannot figure out, how to create a template file for displaying a child elements of f.e. Books ?

Of course, i can manage it by using jquery. Get all level of hierarchy, then hide second level and on the click of first level Name, open sublevel and hide first level, but this is not a solution what i need to achieve, because of the lack of the correct url. I want to get correct url for 1 level www.ddd.com/products/ and also for the second www.ddd.com/products/books/

How would you accomplish this?

UPDATED

This is what I have in my archive-POSTTYPE.php file.

<?php get_header();?>
            <div id="maincol">
            <?

    $query = new WP_Query( 'post_type=produkts&post_parent=0' );
    while( $query->have_posts() ) : $query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
    endwhile;
    wp_reset_postdata();    

            ?>

                </div>

<?php get_footer();?>

and the single-posttype.php

<?php get_header();?>
        <div id="maincol">
            <?php

            if (is_child_post()){
                //this is the true single product
                the_title();

            }else{
                //this is the product list of parent 

                $query = new WP_Query( 'post_type=produkts&post_parent='.$post->ID.'' );
                while( $query->have_posts() ) : $query->the_post();
                echo '<li>';
                the_title();
                echo '</li>';
                endwhile;
                wp_reset_postdata();
            }       

            ?>
            </div>
<?php get_footer();?>
share|improve this question
    
Please add your solution as an answer (and accept if appropriate). That would be more in line with site's mechanics. –  Rarst Jun 2 '11 at 21:51

1 Answer 1

i needed something similar and i've come up with this simple little function that make things easier :

function is_child_post($parent_ID = null) {
    global $post;
    if (isset($parent_ID) && $parent_ID != null){
        return $post->post_parent == $parent_ID;
    }
    return $post->post_parent > 0;
}

Usage:

check if a post/page/custom has a parent:

if (is_child_post()){
  //yes we have a parent
}else{
  //not its a top level parent
}

check if a post/page/custom is a child of a specific parent with the ID of 32:

if (is_child_post(32)){
  //yes its a child of a parent with the ID of 32
}else{
  //not its a child of a parent with the ID of 32
}

So once you have this handy function you can create your a child-products.php template file and then use 'template_redirect' hook to tell WordPress when to use it for example this will tell WordPress to use it even time its a child product:

function custom_child_template_redirect($single_template) {
    global $post;
    if ($post->post_type == 'YOUR_TYPE' && is_child_post()) { //change YOUR_TYPE to your custom Post type name 
        return TEMPLATEPATH . '/child-products.php'; // change child-products.php to the name of your child template file
    }
    return $single_template;

}
add_action( 'template_redirect', 'custom_child_template_redirect' );
share|improve this answer
    
Bainternet, yes that was actualy also my thougt, but using Page template panel. Manualy. Thanks for the code! –  jam Jun 2 '11 at 13:52
    
Glad I could Help. –  Bainternet Jun 2 '11 at 13:54
    
but still ned to get a value '32' dynamicaly - the page parent –  jam Jun 2 '11 at 13:59
    
The last example I've added doesn't need the parent id, but if you want you can get the parent id in the same way $post->post_parent –  Bainternet Jun 2 '11 at 14:23
    
Somehow I cannot figure out about the conditional if else statement. Where to put it? in the archive-products.php file? –  jam Jun 2 '11 at 19:12

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.