How can I add content to a post that only shows in single-post mode?

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 po

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]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745200773a4616310.html

相关推荐

  • How can I add content to a post that only shows in single-post mode?

    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 po

    5小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信