1

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.

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) 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
    Jul 26, 2021 at 15:40

2 Answers 2

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.

2
  • Thanks @mit2sumit. Partial success. I seem to get a page that is text only - how do i get it to appear with links on the articles etc - just as the default Avada page would look with the list of posts? Recall, that the Avada template allows for different grid/list styles. How can this be available on this new page i created?
    – kneidels
    Aug 2, 2021 at 15:51
  • @kneidels Updated above to show posts as links.
    – mit2sumit
    Aug 2, 2021 at 17:22
0

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' );
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.