How to place shortcode below header, above all content

Hey,

I need to place a shortcode below the header but above all content. The shortcode is for a banner ad.

Ideally I would like the banner ad to be placed below my header but I can’t achieve this. I tried placing the shortcode using a widget but that did not work out. I am at a loss!

Mickey

Hello Mickey,

Thanks for writing in!

To resolve your issue in adding a shortcode below the header but above all content, assuming that your child theme is set up, please add the following code in your child theme’s functions.php file

// Add custom banner with shortcode
// =============================================================================
function add_shortcode_banner(){
    echo do_shortcode('[your shortcode here]');
}
add_action('x_before_view_global__slider-below', 'add_shortcode_banner');
add_action('x_after_masthead_end', 'add_shortcode_banner');
// =============================================================================

We would love to know if this has worked for you. Thank you.

It worked perfectly - thank you so much!!! I have a follow-up question. How do I edit the code so that the banner ad appears perfectly centre with some padding around the ad? Is this possible?

Mickey

I lied. It didn’t work. I’m getting this message:

“Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.”

  • Mickey

Hello Mickey,

I have tested this code in my local testing server and there are no issues with it:

// Add custom banner with shortcode
// =============================================================================
function add_shortcode_banner(){
    echo do_shortcode('[your shortcode here]');
}
add_action('x_before_view_global__slider-below', 'add_shortcode_banner');
add_action('x_after_masthead_end', 'add_shortcode_banner');
// =============================================================================

You might be having a file permission or upload timeout in your server. You may need to login to your FTP, browse to wp-content/themes/pro-child/. Edit the functions.php file in that folder and insert the code above.

Please let us know if this works out for you.

It does work, thank you very very much!

I have another question: How do I seperate it so that I have a different banner ad for just the homepage, and another banner ad for pages?

Also how do I adjust the code so that the banner ad is completely centred. Thank you in advance for all your help, I am very bad at coding (obviously).

Mickey

Hello Mirjana,

Thanks for updating the thread. :slight_smile:

For this you will have to use Conditional tags. I am sharing few resources that you can take a look at to learn more.

https://developer.wordpress.org/reference/functions/is_front_page/

I tried looking at the website but don’t see banner image. Can you please share the page URL so we can share the necessary code?

Thanks.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.