Php 显示父类别中的所有帖子,并按子类别分组,标题为“木材/树枝”
我正在使用以下代码为我的客户网站上的类别登录页选择和设置上下文 代码Php 显示父类别中的所有帖子,并按子类别分组,标题为“木材/树枝”,php,html,wordpress,timber,Php,Html,Wordpress,Timber,我正在使用以下代码为我的客户网站上的类别登录页选择和设置上下文 代码 $args = array( 'cat' => '5,3,4,6', 'numberposts' => 5, 'orderby' => 'date', 'order' => 'DESC', ); $context['stories'] = Timber::get_posts( $args ); if( $category->parent == 0 ) { // Stories
$args = array(
'cat' => '5,3,4,6',
'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
);
$context['stories'] = Timber::get_posts( $args );
if( $category->parent == 0 ) {
// Stories parent category
$templates = array( 'category.twig' );
$context['categories'] = $categories;
}
我使用这段代码来设置模板和分配上下文
代码
$args = array(
'cat' => '5,3,4,6',
'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
);
$context['stories'] = Timber::get_posts( $args );
if( $category->parent == 0 ) {
// Stories parent category
$templates = array( 'category.twig' );
$context['categories'] = $categories;
}
这一切都是工作,但我需要复制的功能,并作出以下调整
- 将
设置为Allnumberofposts
- 为每个子类别添加标题
- 在标题下方显示分配给每个类别的所有帖子
不需要编写大量额外的代码就可以完成这项工作吗?我正在学习木材和细枝,所以请随意分享一些改进方法的技巧。将numberofposts设置为all
'numberposts' => -1,
更新上下文以添加标题
$context['title'] = single_cat_title( '', false );
$context['description'] = category_description();
显示分配给每个类别的所有帖子
$cat_id = 1;
$context['category_posts'] = Timber::get_posts(
array(
'cat' => $cat_id,
'posts_per_page' => -1
)
);
更好的方法是使用,
posts\u per\u page=>-1
而不是numberofposts