admin管理员组文章数量:1289891
I would like to override the default post archives, via category.
In some cases (not all), I have 2 level-categories, such as:
Parent Category: "Video"
- Subcategory "Comedy"
- Subcategory "Action"
But some other categories, are just single-level.
Currently, no matter which category you choose, you get a list of posts, no matter whether its a main- or sub- category.
What I would like to do, is when entering the category listing, there is a test of sub-categories exist, and if so, the posts should be lists as such:
Heading: Video
Comedy
- post 1
- post 2
- post 3
Action
- post 4
- post 5
- post 6
If however, there are no subcategories, than the default listing is displayed:
Heading: Books
- post 7
- post 8
- post 9
How can this be done?
I have very basic PHP knowledge, and have a PHPCode script installed, but I'm not sure how to access the archive/category page.
If its relevant, I am using Avada (child) theme.
I would like to override the default post archives, via category.
In some cases (not all), I have 2 level-categories, such as:
Parent Category: "Video"
- Subcategory "Comedy"
- Subcategory "Action"
But some other categories, are just single-level.
Currently, no matter which category you choose, you get a list of posts, no matter whether its a main- or sub- category.
What I would like to do, is when entering the category listing, there is a test of sub-categories exist, and if so, the posts should be lists as such:
Heading: Video
Comedy
- post 1
- post 2
- post 3
Action
- post 4
- post 5
- post 6
If however, there are no subcategories, than the default listing is displayed:
Heading: Books
- post 7
- post 8
- post 9
How can this be done?
I have very basic PHP knowledge, and have a PHPCode script installed, but I'm not sure how to access the archive/category page.
If its relevant, I am using Avada (child) theme.
Share Improve this question edited Jul 18, 2021 at 16:13 kneidels asked Jul 18, 2021 at 15:24 kneidelskneidels 2751 gold badge6 silver badges14 bronze badges 2 |2 Answers
Reset to default 2In your Avada's child theme update your category.php, if its there or else create new file with that name and add the code as below:
<?php
$current_cat = get_queried_object();
echo 'Heading: '.$current_cat->name;
$subcategories = get_categories(
array( 'parent' => $current_cat->term_id )
);
if(!empty($subcategories)){
foreach($subcategories as $cat){
echo '<br>'.$cat->name.'<br>';
$cat_posts = get_posts( array(
'posts_per_page' => -1,
'category' => $cat->term_id
) );
if ( $cat_posts ) {
foreach ( $cat_posts as $post ) :
echo $post->post_title.'<br>';
endforeach;
}
}
}else{
$cat_posts = get_posts( array(
'posts_per_page' => -1,
'category' => $current_cat->term_id
) );
if ( $cat_posts ) {
foreach ( $cat_posts as $post ) :
echo $post->post_title.'<br>';
endforeach;
}
}
It'll print exactly as you asked for.
Update: To get the posts as links, we can add tags like this:
Change everywhere:
echo $post->post_title.'<br>';
To:
echo '<a href="'.get_permalink().'">'.$post->post_title.'</a><br>';
It'll make posts as links.
You can try something like this:
It checks, if category has parent
<?php if ( have_posts() ) {
$this_category = get_category($cat);
?>
<!-- If category is parent, list stuff in here -->
<?php if ($this_category->category_parent == 0){ ?>
<p>i am parent category</p>
<!-- If category is child, list stuff in here -->
<?php } elseif ($this_category->category_parent != 0) { ?>
<p>i am child category</p>
<?php } else { ?>
<p>i am post in category</p>
<?php } else {
get_template_part( 'loop-templates/content', 'none' );
}
?>
本文标签: categoriesList archived posts by subcategory
版权声明:本文标题:categories - List archived posts by subcategory 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741408938a2377114.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
category.php
orarchive.php
or something else entirely. I'm asking because depending on the php file you would need to add someif
checkes (if inarchive.php
) – Buttered_Toast Commented Jul 26, 2021 at 6:25archive.php
, but not 100% sure, as it probably depends on the Avada template. – kneidels Commented Jul 26, 2021 at 15:40