我试图从Kirki函数中获取类别ID,然后将该ID传递到一个循环中,以查看该特定类别ID中的所有帖子。我在后端编写以下代码:
Kirki::add_field( \'cactus_option\', array(
\'type\' => \'select\',
\'settings\' => \'cat_choose\',
\'label\' => __( \'This is the label\', \'kirki\' ),
\'description\' => __( \'This is the control description\', \'kirki\' ),
\'help\' => __( \'This is some extra help text.\', \'kirki\' ),
\'section\' => \'cat_arrange\',
\'default\' => \'option-1\',
\'priority\' => 10,
\'choices\' => Kirki_Helper::get_terms( \'category\' ),
));
And I get this result:
现在,无论我选择了什么类别,我都想把它的ID放在头版。借助该ID的漏洞,我想查看该Pacific ID的所有帖子。我编写了这段代码,因为它只显示ID号,其他什么都没有显示。
<div class="container">
<h1> <?php echo get_theme_mod(\'cat_choose\', $defaults);?></h1>
</div>
But I want to like this:
最合适的回答,由SO网友:Tanmay Patel 整理而成
请尝试此代码。
<?php $cat_id = get_theme_mod(\'cat_choose\', $defaults); ?>
<p><?php echo get_cat_name( $cat_id ); ?></p>
<hr>
<?php
$args = array(
\'post_type\' => \'post\' ,
\'orderby\' => \'date\' ,
\'order\' => \'DESC\' ,
\'cat\' => $cat_id,
\'posts_per_page\' => -1,
);
$cat_posts = new WP_query($args);
if ($cat_posts->have_posts()) : while ($cat_posts->have_posts()) : $cat_posts->the_post(); ?>
<div>
<div>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<p><?php the_time(\'F j, Y\'); ?></p>
</div>
<div>
<?php the_post_thumbnail(); ?>
</div>
</div>
<?php endwhile; endif; ?>
使用类别ID显示类别名称
<?php
$cat_id = get_theme_mod(\'cat_choose\', $defaults);
echo get_cat_name( $cat_id );
?>