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
  • Do you know what file is used to display category? category.php or archive.php or something else entirely. I'm asking because depending on the php file you would need to add some if checkes (if in archive.php) – Buttered_Toast Commented Jul 26, 2021 at 6:25
  • @Buttered_Toast- I assume its archive.php, but not 100% sure, as it probably depends on the Avada template. – kneidels Commented Jul 26, 2021 at 15:40
Add a comment  | 

2 Answers 2

Reset to default 2

In 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