Display full post if only one post in category
-
Is there a way to display a full post (instead of any excerpt) if there is only ONE post in a particular category?
For instance, if there is only ONE post in Category X, when people open up category X tab, the full post will display instead of having to click on the excerpt.
Thanks!
-
Open your category template file and replace
<?php the_excerpt(); ?>
with
<?php if ($wp_query->found_posts == 1) the_content(); else the_excerpt(); ?>
I don’t have a category template… the closest I have is in custom_functions
/*this function allows for the auto-creation of post excerpts*/ function truncate_post($amount,$echo=true) { global $post, $shortname; $postExcerpt = ''; $postExcerpt = $post->post_excerpt; if (get_option($shortname.'_use_excerpt') == 'on' && $postExcerpt <> '') { if ($echo) echo do_shortcode($postExcerpt); else return do_shortcode($postExcerpt); } else { $truncate = $post->post_content; $truncate = preg_replace('@\[caption[^\]]*?\].*?\[\/caption]@si', '', $truncate); if ( strlen($truncate) <= $amount ) $echo_out = ''; else $echo_out = '...'; $truncate = apply_filters('the_content', $truncate); $truncate = preg_replace('@<script[^>]*?>.*?</script>@si', '', $truncate); $truncate = preg_replace('@<style[^>]*?>.*?</style>@si', '', $truncate); $truncate = strip_tags($truncate); if ($echo_out == '...') $truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' ')); else $truncate = substr($truncate, 0, $amount); if ($echo) echo $truncate,$echo_out; else return ($truncate . $echo_out); };
/*this function allows for the auto-creation of post excerpts*/ function truncate_post($amount,$echo=true) { global $post, $shortname; $postExcerpt = ''; $postExcerpt = $post->post_excerpt; if (get_option($shortname.'_use_excerpt') == 'on' && $postExcerpt <> '') { if ($echo) echo do_shortcode($postExcerpt); else return do_shortcode($postExcerpt); } else { $truncate = $post->post_content; $truncate = preg_replace('@\[caption[^\]]*?\].*?\[\/caption]@si', '', $truncate); if ( strlen($truncate) <= $amount ) $echo_out = ''; else $echo_out = '...'; $truncate = apply_filters('the_content', $truncate); $truncate = preg_replace('@<script[^>]*?>.*?</script>@si', '', $truncate); $truncate = preg_replace('@<style[^>]*?>.*?</style>@si', '', $truncate); $truncate = strip_tags($truncate); if ($echo_out == '...') $truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' ')); else $truncate = substr($truncate, 0, $amount); if ($echo) echo $truncate,$echo_out; else return ($truncate . $echo_out); };
Then place the condition around the function call inside whichever template used to display category pages. Like this:
<?php if ( is_category() && $wp_query->found_posts == 1 ) the_content(); else truncate_post(); ?>
Ok, I just am not sure where to put it in that code specifically….
Can you paste your template file in https://wordpress.pastebin.com/ so I see where to put it?
/*this function allows for the auto-creation of post excerpts*/ function truncate_post($amount,$echo=true) { global $post, $shortname; $postExcerpt = ''; $postExcerpt = $post->post_excerpt; if (get_option($shortname.'_use_excerpt') == 'on' && $postExcerpt <> '') { if ($echo) echo do_shortcode($postExcerpt); else return do_shortcode($postExcerpt); } else { $truncate = $post->post_content; $truncate = preg_replace('@\[caption[^\]]*?\].*?\[\/caption]@si', '', $truncate); if ( strlen($truncate) <= $amount ) $echo_out = ''; else $echo_out = '...'; $truncate = apply_filters('the_content', $truncate); $truncate = preg_replace('@<script[^>]*?>.*?</script>@si', '', $truncate); $truncate = preg_replace('@<style[^>]*?>.*?</style>@si', '', $truncate); $truncate = strip_tags($truncate); if ($echo_out == '...') $truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' ')); else $truncate = substr($truncate, 0, $amount); if ($echo) echo $truncate,$echo_out; else return ($truncate . $echo_out); }; }
You need to show me the template that’s using the function to display category posts (maybe archive.php), not the function itself.
Edit: Which theme are you using?
It is there.. thanks for all your help!!
No luck, huh?
Try this:
function truncate_post($amount,$echo=true) { global $post, $shortname, $wp_query; if ( is_category() && $wp_query->found_posts == 1 ) { if ($echo) echo get_the_content(); else return get_the_content(); } else { $postExcerpt = ''; $postExcerpt = $post->post_excerpt; if (get_option($shortname.'_use_excerpt') == 'on' && $postExcerpt <> '') { if ($echo) echo do_shortcode($postExcerpt); else return do_shortcode($postExcerpt); } else { $truncate = $post->post_content; $truncate = preg_replace('@\[caption[^\]]*?\].*?\[\/caption]@si', '', $truncate); if ( strlen($truncate) <= $amount ) $echo_out = ''; else $echo_out = '...'; $truncate = apply_filters('the_content', $truncate); $truncate = preg_replace('@<script[^>]*?>.*?</script>@si', '', $truncate); $truncate = preg_replace('@<style[^>]*?>.*?</style>@si', '', $truncate); $truncate = strip_tags($truncate); if ($echo_out == '...') $truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' ')); else $truncate = substr($truncate, 0, $amount); if ($echo) echo $truncate,$echo_out; else return ($truncate . $echo_out); }; } }
I am sorry you are getting frustrated… I don’t know what file you are talking about – I am not a php expert. I am using Elegant Themes – Interphase, but I don’t know what file you would like…. sorry! ?? This may not be possible… ??
You’re using a premium theme so I can’t get it but you could try the modification I’ve just posted. Can’t guarentee it’ll work as I don’t know how the function is been used.
- The topic ‘Display full post if only one post in category’ is closed to new replies.