在标签

时间:2017-10-28 06:39:33

标签: php wordpress

您好我正在使用Wordpress页面模板进行自定义帖子类型。帖子类型已注册并通过插件工作,并且还具有与之关联的自定义分类。

我在Bootstrap选项卡中显示自定义分类的每个父项。

我需要做的是在选项卡窗格中显示子项,然后在其下显示帖子标题。

像这样的层次结构:

TAB TITLE (parent) - done
-- SECTION TITLE (child)
-- -- POST TITLE
-- -- POST TITLE
-- SECTION TITLE (child)
-- -- POST TITLE
-- -- POST TITLE


到目前为止的代码(我已经标记了我需要显示子项目的位置):

<?php 

/**
 * Template Name: Agreement Index Page
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */

get_header();

// Get list of 'categories' for tabs -->
$args = array(
'hide_empty' => false,
'parent' => 0
);

$sections = get_terms( 'agreement-section', $args );
?>

<div class="row">
<div class="col-md-12">
<h1 class="display-2 text-center"><?php the_title(); ?></h1>
  <?php
    while ( have_posts() ) : the_post();
      the_content(); 
    endwhile; ?>

    <ul class="nav nav-tabs nav-fill" id="myTab" role="tablist">

        <!-- Create the tabs -->
        <?php
        // Use counter so that 'active' class is only applied to first tab
        $counter = 0;
        foreach ($sections as $section) { ?>

            <li class="nav-item">
                <a class="nav-link <?= ($counter == 0) ? 'active' : '' ?>" id="<?php echo $section->slug;?>-tab" data-toggle="tab" href="#<?php echo $section->slug; ?>" role="tab" aria-controls="<?php echo $section->slug;?>" aria-selected="<?= ($counter == 0) ? 'true' : 'false' ?>"><?php echo $section->name; ?></a>
            </li>

        <?php $counter++; } ?>

    </ul>

    <div class="tab-content" id="nav-tabContent">
    <!-- Get the content for each tab -->
    <?php
    $counter2 = 0;
    foreach ($sections as $section) { ?>

        <div class="tab-pane container-fluid fade <?= ($counter2 == 0) ? 'show active' : '' ?>" id="<?php echo $section->slug; ?>" role="tabpanel" aria-labelledby="<?php echo $section->slug; ?>-tab">
            <div class="row">
                <?php
                    $args = array(
                        'post_type' => 'agreement',
                        'tax_query' => array(
                            array(
                                'taxonomy' => $section->taxonomy,
                                'field'    => $section->slug,
                                'terms'    => $section->term_id
                            )
                        )
                    );

                    $loop = new WP_Query( $args );

                    ?>

                    <div class="col-md-6" id="<?php echo $section->slug . '-clauses' ?>">

                            <?php           
                                if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

                                    <?php
                                    //Do something if a specific array value exists within a post
                                    $term_list = wp_get_post_terms($post->ID, $section->taxonomy, array("fields" => "all"));

                                    foreach($term_list as $term_single) { ?>
                                            <h4><?php echo $term_single->name; ?></h4>
                                            <a class="col-md-12" href="     <?php echo the_permalink();?>"><?php echo the_title(); ?></a>                                           
                                    <?php } ?>

                            <?php endwhile; endif; wp_reset_query(); ?>



</div>
            </div> <!-- end row -->

        </div> <!-- end tab-pane -->
        <?php $counter2++; } ?>
    </div> <!-- end tab-content -->

</div> <!-- end col -->
</div> <!-- end row --> 

<!-- end content -->

任何帮助都非常感激。

CHRIS

1 个答案:

答案 0 :(得分:0)

显示父类别的子类别

<?php 

/**
 * Template Name: Agreement Index Page
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */

get_header();

// Get list of 'categories' for tabs -->
$args = array(
'hide_empty' => false,
'parent' => 0
);

$sections = get_terms( 'agreement-section', $args );
?>

<div class="row">
<div class="col-md-12">
<h1 class="display-2 text-center"><?php the_title(); ?></h1>
  <?php
    while ( have_posts() ) : the_post();
      the_content(); 
    endwhile; ?>

    <ul class="nav nav-tabs nav-fill" id="myTab" role="tablist">

        <!-- Create the tabs -->
        <?php
        // Use counter so that 'active' class is only applied to first tab
        $counter = 0;
        foreach ($sections as $section) { ?>

            <li class="nav-item">
                <a class="nav-link <?= ($counter == 0) ? 'active' : '' ?>" id="<?php echo $section->slug;?>-tab" data-toggle="tab" href="#<?php echo $section->slug; ?>" role="tab" aria-controls="<?php echo $section->slug;?>" aria-selected="<?= ($counter == 0) ? 'true' : 'false' ?>"><?php echo $section->name; ?></a>
            </li>

        <?php $counter++; } ?>

    </ul>

    <div class="tab-content" id="nav-tabContent">
    <!-- Get the content for each tab -->
    <?php
    $counter2 = 0;
    foreach ($sections as $section) { ?>

        <div class="tab-pane container-fluid fade <?= ($counter2 == 0) ? 'show active' : '' ?>" id="<?php echo $section->slug; ?>" role="tabpanel" aria-labelledby="<?php echo $section->slug; ?>-tab">
            <div class="row">
                <?php
                    $args = array(
                        'post_type' => 'agreement',
                        'tax_query' => array(
                            array(
                                'taxonomy' => $section->taxonomy,
                                'field'    => $section->slug,
                                'terms'    => $section->term_id
                            )
                        )
                    );

                    $loop = new WP_Query( $args );

                    ?>

                    <div class="col-md-6" id="<?php echo $section->slug . '-clauses' ?>">
                            <?php           
                                if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
                                    <?php
                                    //Do something if a specific array value exists within a post
                                    $term_list = wp_get_post_terms($post->ID, $section->taxonomy, array("fields" => "all"));
                                    ?>
                                    <h4>
                                    <?php
                                    foreach($term_list as $term_single) { 
                                        $category_children = get_terms( $destination_category_taxonomy, array(
                                            'parent'    => $term_single->term_id,
                                            'hide_empty' => false
                                            ) );
                                        $category_children_count = count($category_children);

                                        if($category_children_count>0)
                                        {
                                            $category_children_name_array = array();
                                            foreach($category_children as $category_children_single) { 
                                                $category_children_name_array[]=$category_children_single->name;
                                            }

                                            //display child category of parent category
                                            echo implode(",",$category_children_name_array);
                                        }
                                        else
                                        {
                                            //display parent category
                                            echo $term_single->name;
                                        }
                                    ?>
                                     </h4>
                                     <a class="col-md-12" href="<?php echo the_permalink();?>"><?php echo the_title(); ?></a>                                           
                                    <?php } ?>
                            <?php endwhile; endif; wp_reset_query(); ?>
                </div>
            </div> <!-- end row -->

        </div> <!-- end tab-pane -->
        <?php $counter2++; } ?>
    </div> <!-- end tab-content -->

</div> <!-- end col -->
</div> <!-- end row --> 

<!-- end content -->