0

I have a blog where each blog post has a map displayed. However, I only want the map to show when I'm in single post mode (like how comments do). When I'm on the homepage, and viewing latest posts, I want all the content to show except for the map (because I don't want the main page to be overrun with maps).

I realise that I can do this by editing my theme's php files... but I want to do it using some kind of plugin or markup within the post itself.

Ideally something like this:

<p>Blog post text text more text very interesting text</p>
<!-- EVERYTHING INSIDE HERE SOMEHOW MAGICALLY ONLY SHOWS WITHIN SINGLE POST MODE -->
<p>THIS TEXT ONLY SHOWS IN SINGLE POST MODE SOMEHOW</p>
<!-- BACK TO NORMAL MODE AGAIN -->
<p>Blog post text text more text even more very interesting text</p>

I also don't want to have to switch to excerpts only on the front page, but I'll do this as a last resort.

Can anyone suggest anything?

1 Answer 1

3

Looking at your specific example ... You probably want to use a shortcode. Something like:

add_shortcode('hide_from_summary','wpse_hide_stuff_here');
function wpse_hide_stuff_here($atts,$content){
     global $post;  // gives access to the post, so you can determine mode

     // don't know if you'll want/need $atts, so ... generic stuff here
     $atts = shortcode_atts( array(
        'foo' => 'no foo'
     ), $atts, 'bartag' );

    // default output of this shortcode is blank (hidden)
    $out = '';

    //Only show your content if ... you are viewing the post by itself
    if (is_a($post, 'WP_Post') && is_single($post)) $out = $content;

    return $out;
}

You can add this shortcode to your functions.php or a plugin. Then, apply it in the following way.

[hide_from_summary]
  <p>THIS TEXT ONLY SHOWS IN SINGLE POST MODE SOMEHOW</p>
[/hide_from_summary]
1
  • 1
    Worked perfectly, thanks!
    – Matt
    Commented Aug 30, 2019 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.