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 created a custom post type named "Training" & a Custom Taxonomy for the same named "Table Of Content"

I need to create a Page template to display Table Of Content Something like this

<div id="TableOfContents">
  <ul class="sections">
    <li> <a>Custom Taxonomy 1</a>
      <ul class="sidenav">
        <li class="selected"> Child Taxonomy 1
          <ul class="items">
            <li> Post Title 1 </li>
            <li> Post Title 2 </li>
            <li> Post Title 2 </li>
          </ul>
        </li>
        <li class="selected"> Child Taxonomy 2
          <ul class="items">
            <li> Post Title 1 </li>
            <li> Post Title 2 </li>
            <li> Post Title 2 </li>
          </ul>
        </li>
      </ul>
    </li>
    <li> <a>Custom Taxonomy 2</a>
      <ul class="sidenav">
        <li class="selected"> Child Taxonomy 1
          <ul class="items">
            <li> Post Title 1 </li>
            <li> Post Title 2 </li>
            <li> Post Title 2 </li>
          </ul>
        </li>
        <li class="selected"> Child Taxonomy 2
          <ul class="items">
            <li> Post Title 1 </li>
            <li> Post Title 2 </li>
            <li> Post Title 2 </li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</div>

Here is the php code in functions.php for creating custom taxonomy

// Custom Taxonomy for Training
$labels = array(
    'name'                          => 'Table of Content',
    'singular_name'                 => 'Table of Content',
    'search_items'                  => 'Search Table of Content',
    'popular_items'                 => 'Popular Table of Content',
    'all_items'                     => 'All Table of Content',
    'parent_item'                   => 'Parent Table of Content',
    'edit_item'                     => 'Edit Table of Content',
    'update_item'                   => 'Update Table of Content',
    'add_new_item'                  => 'Add New Table of Content',
    'new_item_name'                 => 'New Table of Content',
    'separate_items_with_commas'    => 'Separate Table of Content with commas',
    'add_or_remove_items'           => 'Add or remove Table of Content',
    'choose_from_most_used'         => 'Choose from most used Table of Content'
    );

$args = array(
    'label'                         => 'Table of Content',
    'labels'                        => $labels,
    'public'                        => true,
    'hierarchical'                  => true,
    'show_ui'                       => true,
    'show_in_nav_menus'             => true,
    'args'                          => array( 'orderby' => 'term_order' ),
    'rewrite'                       => array( 'slug' => 'table-of-content', 'with_front' => false ),
    'query_var'                     => true
);
register_taxonomy( 'tableofcontent', 'training', $args );

Thanks & Regards

share|improve this question
add comment

1 Answer

Here is the final code I managed to working out:

<div class="content" role="main">
    <?php $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
        echo '<h3 class="tax-title">' . $term->name . '</h3>';
    ?>
    <div id="TableOfContents">
      <div class="ui-boxmenu">
        <ul class="sections">
          <?php
$taxonomyName = "tableofcontent";
$parent_terms = get_terms($taxonomyName, array(
    'parent' => $term->term_id,
    'orderby' => 'menu_order',
    'hide_empty' => false
));

foreach ($parent_terms as $pterm) {
    //Get the Child terms
    $terms = get_terms($taxonomyName, array(
        'parent' => $term->term_id,
        'orderby' => 'menu_order',
        'hide_empty' => false
    ));
    foreach ($terms as $term) {
        echo '<li><a>' . $term->name . '</a>';

        $childs = get_terms($taxonomyName, array(
            'parent' => $term->term_id,
            'orderby' => 'menu_order',
            'hide_empty' => false
        ));
        echo '<ul class="sidenav">';

        foreach ($childs as $child) {
            echo '<li>' . $child->name . '';
                $wpq = array ('taxonomy'=>'tableofcontent','orderby' => 'menu_order','order' => 'ASC','term'=>$child->slug);
                  $myquery = new WP_Query ($wpq);
                  $article_count = $myquery->post_count;
                  if ($article_count) {
                    echo "<ul class=\"items\">";
                    while ($myquery->have_posts()) : $myquery->the_post();
                      echo "<li><a href=\"".get_permalink()."\">".$post->post_title."</a></li>";
                    endwhile;
                    echo "</ul>";
                  } 

            echo '</li>';
        }

        echo '</ul>';


        echo '</li>';
    }
}

?>
        </ul>
      </div>
    </div>
    <script language="javascript">
        $('.ui-boxmenu').boxmenu();
    </script> 
    <script type="text/javascript">
        // Default add class selected to First Item in Box Menu
        jQuery(document).ready(function($) {
            $("ul.sections li:first").addClass("selected");
            $("ul.sections li:first .sidenav li:first").addClass("selected");
            $("ul.sections li:first .sidenav li ul.items").addClass("selected");
            $("ul.sections li:first .sidenav li ul.items li:first").addClass("selected");
        });
    </script>

<?php   
$singleterm = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
    $args = array(
        'post_type'         => 'training',
        'tableofcontent'  => $singleterm->slug,
        'orderby'           => 'menu_order',
        'showposts'         => 1,
        'order' => 'ASC',
    );

    $posts = get_posts( $args );
    foreach ($posts as $post) :  setup_postdata($post); 

?>

<h2 class="content-subhead"><?php the_title() ?></h2>
<?php the_content() ?>

<?php endforeach; wp_reset_postdata(); ?>

  </div>

Regards

share|improve this answer
add comment

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.