如何自定义类别模板?

时间:2016-08-20 作者:jrcollins

我甚至不确定是否有可能做我想做的事。我想创建一个自定义类别模板,显示当前类别的子类别,而不是该类别中的帖子。

我还想保持布局尽可能接近原始模板的缩略图图像,标题和摘录。我将为缩略图使用自定义类别图像,标题是子类别术语,对于摘录,我将使用类别描述。

在原始类别模板中,帖子以网格格式排列,循环设置了一个计数器,用于在每第二篇帖子后打开和关闭“行”div(请参见下面的代码)。是否可以对类别执行相同的操作?

这是我用来获取子类别的代码:

<?php 
$cat = get_category( get_query_var( \'cat\' ) );
$cat_id = $cat->cat_ID;
$child_categories=get_categories(
array( \'parent\' => $cat_id )
);

foreach ( $child_categories as $child ) {
echo \'<li>\'.$child ->cat_name.\'</li>\';
}
?>
但我需要以下格式的列表:

<article>
 <div class="category thumbnail">
<a href="" title=""><img alt="" src="" />
</a>
</div>
<div class="category-title">
        <h2 class="entry-title">Category Title</h2>
    </header><!-- .entry-header -->
    <div class="category description">
    </div><!-- .entry-content -->
</div>
</article>
。。。这是原始类别模板:

    <?php
/**
* The template for displaying category archive pages.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package Codilight_Lite
*/

get_header(); ?>
<div id="content" class="site-content container <?php echo codilight_lite_sidebar_position(); ?>">
<div class="content-inside">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">

<?php
if ( have_posts() ) : $count = 0; ?>

<header class="page-header">
<?php
the_archive_title( \'<h1 class="page-title">\', \'</h1>\' );
the_archive_description( \'<div class="taxonomy-description">\', \'</div>\' );
?>
</header><!-- .page-header -->

<?php
$layout_archive_posts = get_theme_mod( \'layout_archive_posts\', \'grid\' );
if ( $layout_archive_posts == \'grid\' ) {
echo \'<div class="block1 block1_grid">\';
echo \'<div class="row">\';
while ( have_posts() ) : the_post();
$count++;
get_template_part( \'template-parts/content-grid\' );
if ( $count % 2 == 0 ) {
echo \'</div>\';
echo \'<div class="row">\';
}
endwhile;
echo \'</div>\';
echo \'</div>\';
codilight_lite_custom_paginate();

} else {
echo \'<div class="block1 block1_list">\';
while ( have_posts() ) : the_post();
get_template_part( \'template-parts/content-list\' );
endwhile;
codilight_lite_custom_paginate();
echo \'</div>\';
}
?>

<?php else : ?>

<?php get_template_part( \'template-parts/content\', \'none\' ); ?>

<?php endif; ?>

</main><!-- #main -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
生产线get_template_part( \'template-parts/content-grid\' ); 是链接到上面提到的“article”div的地方。

1 个回复
最合适的回答,由SO网友:CodeMascot 整理而成

这是您的解决方案-

<?php
/**
 * The template for displaying category archive pages.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Codilight_Lite
 */
$cat = get_category( get_query_var( \'cat\' ) );
$cat_id = $cat->cat_ID;
$child_categories=get_categories(
    array( 
        \'parent\' => $cat_id,
        // Uncomment the below line if you want empty category to appear on the list. 
        // \'hide_empty\'   => 0
    )
);
get_header(); ?>
<div id="content" class="site-content container">
    <div class="content-inside">
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
            <?php
            foreach ( $child_categories as $child ) { ?>
                <article>
                    <div class="category thumbnail">
                        <a href="" title=""><img alt="" src="" />
                        </a>
                    </div>
                    <div class="category-title">
                        <h2 class="entry-title"><?php echo $child ->cat_name;?></h2>
                        </header><!-- .entry-header -->
                        <div class="category description">
                            <?php echo $child ->description;?>
                        </div><!-- .entry-content -->
                    </div>
                </article>
            <?php
            }
            ?>
        </main><!-- #main -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
只需复制并粘贴到archive.php 你已经准备好了。希望这对你有帮助。