MH Magazine Lite主题插件广告插入问题:广告显示在特色图下方而非标题下方
Hey there! Let's get that ad positioned right below your post title where you want it. The issue here is that you're probably hooking into a spot that fires after the featured image loads—let's adjust that with the theme's built-in hooks, or a quick child theme tweak if needed.
Option 1: Use the Theme's Built-in Hook (Recommended)
MH Magazine Lite has a handy action hook that fires right before the featured image loads, which is exactly after the post title and meta info. Here's how to use it in your plugin:
- Add this code to your plugin file:
function display_ad_below_title() { // Only show the ad on single post pages if (is_single()) { ?> <div class="ad-below-post-title" style="margin: 1.5rem 0; padding: 1rem; border: 1px solid #eee;"> <!-- Replace this with your actual ad code (e.g., Google AdSense, custom banner) --> <p>Your Ad Content Goes Here</p> </div> <?php } } // Hook into the spot right before the featured image add_action('mh_before_featured_image', 'display_ad_below_title');
This hook is part of the theme's mh_featured_image() function, so it will render your ad immediately after the post title/meta section, and before the featured image—perfect for your needs.
Option 2: Add a Custom Hook (If the Built-in One Doesn't Exist)
If for some reason your version of MH Magazine Lite doesn't have the mh_before_featured_image hook, use a child theme to add your own custom hook (never modify the parent theme directly—updates will erase your changes):
- Create and activate a child theme for MH Magazine Lite.
- Copy the
single.phpfile from the parent theme to your child theme folder. - Open the child theme's
single.phpand find this section:
<header class="entry-header"> <?php mh_post_title(); ?> <?php mh_post_meta(); ?> </header> <?php mh_featured_image(); ?>
- Insert a custom action hook right before closing the header:
<header class="entry-header"> <?php mh_post_title(); ?> <?php mh_post_meta(); ?> <!-- Custom hook for ad placement --> <?php do_action('mh_after_post_header'); ?> </header> <?php mh_featured_image(); ?>
- Now update your plugin code to use this new hook:
function display_ad_below_title() { if (is_single()) { ?> <div class="ad-below-post-title"> <!-- Your ad code here --> </div> <?php } } add_action('mh_after_post_header', 'display_ad_below_title');
Quick Tips
- Adjust the CSS class
ad-below-post-titleto style your ad spacing, borders, etc., to match your theme. - If you notice other elements loading before/after your ad, tweak the priority in
add_action(e.g.,add_action('mh_before_featured_image', 'display_ad_below_title', 5);—lower numbers run earlier).
内容的提问来源于stack exchange,提问作者Kenyis

