-
AuthorPosts
-
May 26, 2022 at 9:58 pm #1353078
Hi,
We have a situation where the owner of the website needs to have an emergency message on each page. Some of the pages are using the WordPress default and others are using the Avia Page Builder. So what we did was added a message to child-theme/page.php which took care of the WordPress default pages. Then we used the heading element to manually add the message to the few pages that are using the Avia page builder. The home page has a different message.My question: is there a global message we can add to functions.php or header.php to add this message across all pages except the home page? We want to be able to easily edit the message in one place and remove it when the time comes.
May 26, 2022 at 11:54 pm #1353080Hey GWS,
Thanks for the link to your site, try adding this code to the end of your functions.php file in Appearance ▸ Editor:function custom_special_message() { ?> <script> window.addEventListener('DOMContentLoaded', function() { (function($){ if ($("body").hasClass("home")) { $( '<div class="av-special-heading">Our training center is currently closed for all in-person classes</div>' ).insertBefore( 'main.content' ); }else{ $( '<div class="av-special-heading">Sorry we are currently closed for all in-person classes</div>' ).insertBefore( 'main.content' ); } })(jQuery); }); </script> <?php } add_action('wp_footer', 'custom_special_message');
The first message is for your homepage, the second is for all other pages. You can add a custom class to the div to style to suit your needs.
In my test I had to inject jQuery, because your site was giving the “jQuery is not defined” error, it looks like “rocket-loader” is adding jQuery too late, at least in my test.
If you have any trouble with this script try disabling “rocket-loader” or allow jQuery to load in the header, I’m not sure what options the “rocket-loader” gives you.Best regards,
MikeMay 27, 2022 at 6:04 pm #1353188Thank you, Mike. I disabled Rocket Loader and added the script to functions.php and that does the trick. I appreciate your help. You can consider this topic closed.
May 27, 2022 at 6:45 pm #1353201Hi,
Great, I’m glad that Mike could help you out. I’ll close this thread for now then, please open a new thread if you should have any further questions or problems.
Best regards,
Rikard -
AuthorPosts
- The topic ‘Adding a message in the content on all pages using functions.php’ is closed to new replies.