Site logo

Archived topic

Customizing Category Pages

5 replies · Started by Hernan on November 27, 2019

Viewing posts 1–6 of 6

I want to add some custom content to each category page. The custom content would be placed above the list of posts for that category.

In this thread:
https://generatepress.com/forums/topic/how-to-customize-category-archive-page-layouts/
I found this code:

add_action( 'generate_before_main_content', 'tu_add_content_above_posts', 0 );
function tu_add_content_above_posts() {
    if ( is_category( 'category-name' ) ) : ?>
        Your content
    <?php endif;
}

So, what I did is i modified the archive.php file by adding this:

do_action( 'generate_archive_title' );
/*my edit*/

if ( is_category( 'cat-1' ) ) : ?>
     <div>
	<p>
           Custom content for cat 1.
 	</p>
    </div>
	<?php endif;
if ( is_category( 'cat-2' ) ) : ?>
     <div>
          <p>
             Custom content for cat 2.
          </p>
     </div>
        <?php endif;

/*end my edit*/

This seems to work fine. However, I'd have to create a child theme in order to persist this across an theme update.

Is there a more efficient way to do this? One that would not require a child theme. And perhaps allow for a separate file for each category with it's own custom HTML content?

Thanks,

Hernan

You could do this:

add_action( 'generate_after_archive_title', function() {
    // Your code in here
} );

Let me know if you need more info :)

Hi Tom,

Thanks for the response. Would the add_action function go in the archive.php file as well?

Regards,
Hernan

Hi there,

that code should go in the Child themes functions.php

Thanks!

Glad we could be of help

This archived topic is closed to new replies.