创建页面模板以显示自定义分类的内容表

时间:2012-12-03 07:01:43

标签: wordpress custom-post-type custom-taxonomy

我创建了一个名为“Training”的自定义帖子类型&自定义分类法,用于相同名称的“目录”

我需要创建一个页面模板来显示Table Of Content像这样的东西

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

这是functions.php中用于创建自定义分类

的php代码
// 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 );

谢谢&amp;此致

1 个答案:

答案 0 :(得分:0)

以下是我设法解决的最终代码:

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

此致