Skip to content
Public Ticket #31228

Banner Above Post

Conversation thread

PenciDesign Supporter replied

Staff Reply

Hi,

At this time, there is no available hook to insert content into a specific sidebar widget. You should manually add it via Appearance → Widgets.

The previous code I sent will apply the ads code at the top of posts for specific category IDs.

Regards,
PenciDesign.

LillaGreen replied

Customer Reply

hi, thank yoi for your reply.The banner is not Google banner, it is from my customer and I want to place it only on certain categories on the top of a blog post as well as a sidebar. Ho to do it?

PenciDesign Supporter replied

Staff Reply

Hi,

You should paste this code into the function.php of the child theme or via the CodeSnippet plugin here: https://wordpress.org/plugins/code-snippets/

add_action( 'penci_action_before_the_content', function() {
    $post_id = get_the_ID();
    $post_categories = get_the_category( $post_id );
    $show_ads = false;
    $category_ids = [1, 2, 3]; // add category id here
    foreach ( $post_categories as $category ) {
        if ( in_array( $category->term_id, $category_ids ) ) {
            $show_ads = true;
            break;
        }
    }
    if ( $show_ads ) {
        echo '<div class="penci-ads">insert ad code here</div>';
    }
});

Regards,
PenciDesign.

LillaGreen started the conversation

Conversation Start

Hi there,

Is there an easy way to implement a banner (not using JavaScript) that appears at the top of blog posts, but only within a specific category? I’d like to avoid showing it above all posts sitewide.  Or is it possible only with a snippet?

Additionally, is there a way to display a sidebar banner only for posts in a certain category?

I’d really appreciate any guidance on how to set this up.

Thank you in advance

Want to join this conversation or access attachments? Sign in first.