Okay
  Public Ticket #2634486
Adding a Banner to the Category Pages
Closed

Comments

  • JahMaican started the conversation

    Hi! I love your theme.

    I would like to add a banner to each of the shop category pages. How can I do this? Is there some CSS I can apply? Also what would be the ideal banner dimensions?

    Any help would be appreciated. Thank you!

  • [deleted] replied

    Hi JahMaican,

    Sure you can do this, by adding the following code in functions.php:

    add_action( 'woocommerce_before_main_content', function() {
        if ( is_product_category() ) {
            $page = get_page_by_title( 'Shop Categories Banner' );
            if ( $page instanceof WP_Post ) {
                echo '<div class="container">';
                echo apply_filters( 'the_content', $page->post_content );
                echo '</div>';
            }
        }
    }, 0 );
    

    So this code will look for a page with the title "Shop Categories Banner" and apply its content in the shop categories page, as can be seen here in this video demonstration:

    https://d.pr/v/3oUVdS

  • JahMaican replied

    Hi! That's excellent. Thank you for that. I am however in a situation where I want to add a banner to possible 50 or more category pages.

    Would there be a easier way to do this rather than having to  create multiple additional pages and then adding a code snippet to pull from each of those pages?

  • [deleted] replied

    Hi JahMaican,

    Oh sorry I thought you want a single banner for all categories. So here is the modified code (remove previous one):

    add_action( 'woocommerce_before_main_content', function() {
        if ( is_product_category() ) {
            $term = get_queried_object();
            if ( $term instanceof WP_Term ) {
                echo '<div class="container">';
                echo kalium_get_field( 'category_custom_content', $term );
                echo '</div>';
            }
        }
    }, 0 );
    if ( function_exists( 'acf_add_local_field_group' ) ):
        acf_add_local_field_group( [
            'key'                   => 'group_5fe06d505dd2d',
            'title'                 => 'Category Custom Content',
            'fields'                => [
                [
                    'key'               => 'field_5fe06d9b48ad0',
                    'label'             => 'Content',
                    'name'              => 'category_custom_content',
                    'type'              => 'wysiwyg',
                    'instructions'      => '',
                    'required'          => 0,
                    'conditional_logic' => 0,
                    'wrapper'           => [
                        'width' => '',
                        'class' => '',
                        'id'    => '',
                    ],
                    'default_value'     => '',
                    'tabs'              => 'all',
                    'toolbar'           => 'full',
                    'media_upload'      => 1,
                    'delay'             => 0,
                ],
            ],
            'location'              => [
                [
                    [
                        'param'    => 'taxonomy',
                        'operator' => '==',
                        'value'    => 'product_cat',
                    ],
                ],
            ],
            'menu_order'            => 0,
            'position'              => 'normal',
            'style'                 => 'default',
            'label_placement'       => 'top',
            'instruction_placement' => 'label',
            'hide_on_screen'        => '',
            'active'                => true,
            'description'           => '',
        ] );
    endif;
    

    It will display a field when you edit Product Category:

    medium
    (view large image)