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 badges1 Answer
Reset to default 3Looking 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
版权声明:本文标题:How can I add content to a post that only shows in single-post mode? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745200773a2647357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论