admin管理员组

文章数量:1415072

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?

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?

Share Improve this question asked Aug 29, 2019 at 23:49 MattMatt 1033 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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]

本文标签: How can I add content to a post that only shows in singlepost mode