如何创建和链接类别的自定义模板

时间:2019-04-12 18:15:16

标签: php wordpress-theming custom-wordpress-pages

我正在尝试在类别页面上显示子类别,然后从子类别到他们的帖子。

我不知道该怎么做,在我的子主题中,存在一个php文件“ category.php”,该文件在侧边栏中显示子类别,并在页面内容中显示帖子,我想在单击类别然后显示其帖子后显示子类别点击子类别,简短的子类别和不同页面上的帖子

我在category.php文件中有此

<?php
/**
 * The template for displaying Category pages.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage classiera
 * @since classiera 1.0
 */

get_header();
?>
<?php
global $redux_demo;
global $allowed_html;
global $cat_id;
$caticoncolor="";
$category_icon_code ="";
$category_icon ="";
$category_icon_color="";
$classieraSearchStyle = $redux_demo['classiera_search_style'];
$classieraPremiumStyle = $redux_demo['classiera_premium_style'];
$classieraPartnersStyle = $redux_demo['classiera_partners_style'];
$classieraCategoriesStyle = $redux_demo['classiera_cat_style'];
$classieraPostCount = $redux_demo['classiera_cat_post_counter'];
$classieraAdvSearchCats = $redux_demo['classiera_adv_search_on_cats'];
$classiera_categories_desc = $redux_demo['classiera_categories_desc'];

    $cat_id = get_queried_object_id();
    $this_category = get_category($cat_id);
    $cat_parent_ID = isset( $cat_id->category_parent ) ? $cat_id->category_parent : '';
    if ($cat_parent_ID == 0) {
        $tag = $cat_id;
    }else{
        $tag = $cat_parent_ID;
    }
    $category = get_category($tag);
    $count = $category->category_count;
    $catName = get_cat_name( $tag );
function classiera_Cat_Ads_Count(){
        $cat_id = get_queried_object_id();
        $cat_parent_ID = isset( $cat_id->category_parent ) ? $cat_id->category_parent : '';
        if ($cat_parent_ID == 0) {
            $tag = $cat_id;
        }else{
            $tag = $cat_parent_ID;
        }
        $q = new WP_Query( array(
            'nopaging' => true,
            'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => $tag,
                    'include_children' => true,
                ),
            ),
            'fields' => 'ids',
        ) );
    $count = $q->post_count;
    echo esc_attr($count);
}
function classiera_single_catid(){
    $cat_id = get_queried_object_id();
    return $cat_id;
}
?>
<?php
    //Search Styles//
    if($classieraSearchStyle == 1){
        get_template_part( 'templates/searchbar/searchstyle1' );
    }elseif($classieraSearchStyle == 2){
        get_template_part( 'templates/searchbar/searchstyle2' );
    }elseif($classieraSearchStyle == 3){
        get_template_part( 'templates/searchbar/searchstyle3' );
    }elseif($classieraSearchStyle == 4){
        get_template_part( 'templates/searchbar/searchstyle4' );
    }elseif($classieraSearchStyle == 5){
        get_template_part( 'templates/searchbar/searchstyle5' );
    }elseif($classieraSearchStyle == 6){
        get_template_part( 'templates/searchbar/searchstyle6' );
    }elseif($classieraSearchStyle == 7){
        get_template_part( 'templates/searchbar/searchstyle7' );
    }
?>
<?php
    global $redux_demo;
    $premiumSECtitle = $redux_demo['premium-sec-title'];
    $premiumSECdesc = $redux_demo['premium-sec-desc'];
    $classieraCurrencyTag = $redux_demo['classierapostcurrency'];
    $featuredCatOn = $redux_demo['featured-caton'];
    $classieraFeaturedCategories = $redux_demo['featured-ads-cat'];
    $classieraPremiumAdsCount = $redux_demo['premium-ads-counter'];
    $category_icon_code = "";
    $category_icon_color = "";
    $catIcon = "";
    $classieraIconsStyle = $redux_demo['classiera_cat_icon_img'];
    $classiera_ads_typeOn = $redux_demo['classiera_ads_type'];
    if($classiera_ads_typeOn == 1){
        $adstypeQuery = array(
            'key' => 'classiera_ads_type',
            'value' => 'sold',
            'compare' => '!='
        );
    }else{
        $adstypeQuery = null;
    }
?>
<section class="classiera-premium-ads-v6 border-bottom section-pad">
    <div class="section-heading-v6">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-md-8 center-block">
                    <h3 class="text-capitalize"><?php echo esc_html($premiumSECtitle); ?></h3>
                    <p><?php echo esc_html($premiumSECdesc); ?></p>
                </div>
            </div>
        </div>
    </div>
    <div style="overflow: hidden;">
        <div style="margin-bottom: 40px;">
            <div id="owl-demo" class="owl-carousel premium-carousel-v6" data-car-length="4" data-items="4" data-loop="true" data-nav="false" data-autoplay="true" data-autoplay-timeout="3000" data-dots="false" data-auto-width="false" data-auto-height="true" data-right="<?php if(is_rtl()){echo "true";}else{ echo "false";}?>" data-responsive-small="1" data-autoplay-hover="true" data-responsive-medium="2" data-responsive-large="4" data-responsive-xlarge="6" data-margin="15">
                <?php
                $cat_id = get_cat_ID(single_cat_title('', false));
                global $paged, $wp_query, $wp;
                $args = wp_parse_args($wp->matched_query);
                $temp = $wp_query;
                $wp_query= null;
                if($featuredCatOn == 1){
                    $arags = array(
                        'post_type' => 'post',
                        'posts_per_page' => $classieraPremiumAdsCount,
                        'cat' => $classieraFeaturedCategories,
                        'orderby' => 'rand',
                    );
                }else{
                    $arags = array(
                        'post_type' => 'post',
                        'posts_per_page' => $classieraPremiumAdsCount,
                        'cat' => $cat_id,
                        'orderby' => 'rand',
                        'meta_query' => array(
                            array(
                                'key' => 'featured_post',
                                'value' => '1',
                                'compare' => '=='
                            ),
                            $adstypeQuery,
                        ),
                    );
                }
                $wp_query = new WP_Query($arags);
                $current = -1;
                while ($wp_query->have_posts()) : $wp_query->the_post();
                    $featured_post = get_post_meta($post->ID, 'featured_post', true);
                    if($featuredCatOn == 1){
                        $featured_post = "1";
                    }
                    if($featured_post == "1") {
                        $current++;
                ?>
                <?php
                    $category = get_the_category();
                    $catID = $category[0]->cat_ID;
                    if ($category[0]->category_parent == 0) {
                        $tag = $category[0]->cat_ID;
                        $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
                        if (isset($tag_extra_fields[$tag])) {
                            $category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
                            $category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
                            $classieraCatIcoIMG = $tag_extra_fields[$tag]['your_image_url'];
                        }
                    }elseif(isset($category[1]->category_parent) && $category[1]->category_parent == 0){
                        $tag = $category[0]->category_parent;
                        $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
                        if (isset($tag_extra_fields[$tag])) {
                            $category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
                            $category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
                            $classieraCatIcoIMG = $tag_extra_fields[$tag]['your_image_url'];
                        }
                    }else{
                        $tag = $category[0]->category_parent;
                        $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
                        if (isset($tag_extra_fields[$tag])) {
                            $category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
                            $category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
                            $classieraCatIcoIMG = $tag_extra_fields[$tag]['your_image_url'];
                        }
                    }
                    if(!empty($category_icon_code)) {
                        $catIcon = stripslashes($category_icon_code);
                    }
                    $postCatgory = get_the_category( $post->ID );
                    $categoryLink = get_category_link($catID);
                    $classiera_ads_type = get_post_meta($post->ID, 'classiera_ads_type', true);
                    $post_currency_tag = get_post_meta($post->ID, 'post_currency_tag', true);
                ?>
                <div class="classiera-box-div-v7 item match-height">
                    <figure>
                        <div class="premium-img">
                            <div class="featured">
                                <p><?php esc_html_e( 'Featured', 'classiera' ); ?></p>
                            </div><!--featured-tag-->
                            <?php
                                if(has_post_thumbnail()){
                                    $classieraIMGURL = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'classiera-370');
                                    $thumb_id = get_post_thumbnail_id($post->ID);
                                    $classieraALT = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
                                    ?>
                                <img class="img-responsive" src="<?php echo esc_url($classieraIMGURL[0]); ?>" alt="<?php echo esc_attr($classieraALT);  ?>">
                            <?php
                                }else{
                                    $classieraDafult = get_template_directory_uri() . '/images/nothumb.png';
                                    ?>
                                    <img class="img-responsive" src="<?php echo esc_url($classieraDafult); ?>" alt="<?php echo get_the_title(); ?>">
                                    <?php
                                }
                            ?>
                        </div><!--premium-img-->
                        <figcaption>
                            <div class="caption-tags">
                                <?php $post_price = get_post_meta($post->ID, 'post_price', true);?>
                                <?php if(!empty($post_price)){?>
                                <span class="price btn btn-primary round btn-style-six active">
                                    <?php
                                    if(is_numeric($post_price)){
                                        echo classiera_post_price_display($post_currency_tag, $post_price);
                                    }else{
                                        echo esc_attr($post_price);
                                    }
                                    ?>
                                </span>
                                <?php } ?>
                                <?php $classiera_ads_type = get_post_meta($post->ID, 'classiera_ads_type', true); ?>
                                <?php if(!empty($classiera_ads_type)){?>
                                <span class="buy-sale-tag btn btn-primary round btn-style-six active">
                                    <?php classiera_buy_sell($classiera_ads_type); ?>
                                </span>
                                <?php } ?>
                            </div>
                            <div class="content">
                                <h5><a href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a></h5>
                                <div class="category">
                                    <span><?php esc_html_e( 'Category', 'classiera' ); ?> :
                                        <a href="<?php echo esc_url($categoryLink); ?>">
                                            <?php echo esc_html($postCatgory[0]->name); ?>
                                        </a>
                                    </span>
                                </div>
                                <div class="description">
                                    <p><?php echo substr(get_the_excerpt(), 0,260); ?></p>
                                </div>
                                <a href="<?php the_permalink(); ?>"><?php esc_html_e( 'view ad', 'classiera' ); ?> <i class="fas fa-long-arrow-alt-right"></i></a>
                            </div>
                        </figcaption>
                    </figure>
                </div>
                <?php } ?>
                <?php endwhile; ?>
                <?php wp_reset_query(); ?>
            </div>
        </div>
        <div class="navText">
            <a class="prev btn btn-primary radius outline btn-style-six">
                <i class="icon-left fas fa-long-arrow-alt-left"></i>
                <?php esc_html_e( 'Previous', 'classiera' ); ?>
            </a>
            <a class="next btn btn-primary radius outline btn-style-six">
                <?php esc_html_e( 'Next', 'classiera' ); ?>
                <i class="icon-right fas fa-long-arrow-alt-right"></i>
            </a>
        </div>
    </div>
</section>
<!-- page content -->
<section class="inner-page-content border-bottom top-pad-50">
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-lg-9">
                <!--category description section-->
                <?php if($classiera_categories_desc == 1){?>
                <section id="classiera_cat_description">
                    <div class="container">
                        <div class="row">
                            <div class="col-lg-12 col-md-12 col-sm-12 center-block">
                                <?php echo category_description($cat_id); ?>
                            </div>
                        </div>
                    </div>
                </section>
                <?php } ?>
                <!--category description section-->
                <!--Google Section-->
                <?php
                $homeAd1 = '';
                global $redux_demo;
                $homeAdImg1 = $redux_demo['post_ad']['url'];
                $homeAdImglink1 = $redux_demo['post_ad_url'];
                $homeHTMLAds = $redux_demo['post_ad_code_html'];
                if(!empty($homeHTMLAds) || !empty($homeAdImg1)){
                ?>
                <section id="classieraDv">
                    <div class="container">
                        <div class="row">
                            <div class="col-lg-12 col-md-12 col-sm-12 center-block text-center">
                                <?php
                                if(!empty($homeHTMLAds)){
                                    echo classiera_display_html_code($homeHTMLAds);
                                }else{
                                    echo '<a href="'.$homeAdImglink1.'" target="_blank"><img class="img-responsive" alt="image" src="'.$homeAdImg1.'" /></a>';
                                }
                                ?>
                            </div>
                        </div>
                    </div>
                </section>
                <?php } ?>
                <!--Google Section-->
                <!-- advertisement -->
                <?php
                    if($classieraCategoriesStyle == 1){
                        get_template_part( 'templates/catinner/style1' );
                    }elseif($classieraCategoriesStyle == 2){
                        get_template_part( 'templates/catinner/style2' );
                    }elseif($classieraCategoriesStyle == 3){
                        get_template_part( 'templates/catinner/style3' );
                    }elseif($classieraCategoriesStyle == 4){
                        get_template_part( 'templates/catinner/style4' );
                    }elseif($classieraCategoriesStyle == 5){
                        get_template_part( 'templates/catinner/style5' );
                    }elseif($classieraCategoriesStyle == 6){
                        get_template_part( 'templates/catinner/style6' );
                    }elseif($classieraCategoriesStyle == 7){
                        get_template_part( 'templates/catinner/style7' );
                    }elseif($classieraCategoriesStyle == 8){
                        get_template_part( 'templates/catinner/style5' );
                    }elseif($classieraCategoriesStyle == 9){
                        get_template_part( 'templates/catinner/style5' );
                    }
                ?>
                <!-- advertisement -->
            </div><!--col-md-8-->
            <div class="col-md-4 col-lg-3">
                <aside class="sidebar">
                    <div class="row">
                        <!--subcategory-->
                        <?php
                        $cat_term_ID = $this_category->term_id;
                        $cat_child = get_term_children( $cat_term_ID, 'category' );
                        if (!empty($cat_child)) {
                            $args = array(
                                'type' => 'post',
                                'parent' => $cat_id,
                                'orderby' => 'name',
                                'order' => 'ASC',
                                'hide_empty' => 0,
                                'depth' => 1,
                                'hierarchical' => 1,
                                'taxonomy' => 'category',
                                'pad_counts' => true
                            );
                            $category = get_categories($args);
                            if($category[0]->category_parent == 0){
                                    $tag = $category[0]->cat_ID;
                                    $category_icon_code = "";
                                    $category_icon_color = "";
                                    $your_image_url = "";
                                    $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
                                    if (isset($tag_extra_fields[$tag])) {
                                        $category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
                                        $category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
                                    }
                                }else{
                                    $tag = $category[0]->category_parent;
                                    $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
                                    if (isset($tag_extra_fields[$tag])) {
                                        $category_icon_code = $tag_extra_fields[$tag]['category_icon_code'];
                                        $category_icon_color = $tag_extra_fields[$tag]['category_icon_color'];
                                    }
                                }
                                $category_icon = stripslashes($category_icon_code);
                        ?>
                        <div class="col-lg-12 col-md-12 col-sm-6 match-height">
                            <div class="widget-box">
                                <div class="widget-title">
                                    <h4>
                                        <i class="<?php echo esc_html($category_icon); ?>" style="color:<?php echo esc_html($category_icon_color); ?>;"></i>
                                        <?php echo esc_html($catName); ?>
                                    </h4>
                                </div>
                                <div class="widget-content">
                                    <ul class="category">
                                    <?php
                                        foreach($category as $category) {
                                    ?>
                                        <li>
                                            <a href="<?php echo esc_url(get_category_link( $category->term_id ));?>">
                                                <i class="fas fa-angle-right"></i>
                                                <?php echo esc_html($category->name); ?>
                                                <span class="pull-right flip">
                                                <?php if($classieraPostCount == 1){?>
                                                    (<?php echo esc_attr($category->count); ?>)
                                                <?php }else{ ?>
                                                    &nbsp;
                                                <?php } ?>
                                                </span>
                                            </a>
                                        </li>
                                    <?php } ?>
                                    </ul>
                                </div>
                            </div>
                        </div>
                        <?php } ?>
                        <!--subcategory-->
                        <?php if($classieraAdvSearchCats == 1){?>
                            <div class="col-lg-12 col-md-12 col-sm-6 match-height">
                                <div class="widget-box">
                                <?php get_template_part( 'templates/classiera-adv-search' );?>
                                </div>
                            </div>
                        <?php } ?>
                        <?php get_sidebar('pages'); ?>
                    </div><!--row-->
                </aside>
            </div><!--row-->
        </div><!--row-->
    </div><!--container-->
</section>
<!-- page content -->
<?php get_footer(); ?>

预期结果应为不同页面上的子类别,以及不同页面上的帖子。 我被困住了,急切需要帮助,我会很高兴得到一些帮助。

0 个答案:

没有答案