[Support request] Customizing Category Pages

Home Forums Support Customizing Category Pages

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1080265
    Hernan

    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

    #1080501
    Tom
    Lead Developer
    Lead Developer

    You could do this:

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

    Let me know if you need more info 🙂

    #1080592
    Hernan

    Hi Tom,

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

    Regards,
    Hernan

    #1081127
    David
    Staff
    Customer Support

    Hi there,

    that code should go in the Child themes functions.php

    #1099350
    Hernan

    Thanks!

    #1099635
    David
    Staff
    Customer Support

    Glad we could be of help

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.