Show Alternate Content When WordPress More Tag Is Missing


Quick tip. It's a wordpress tip day for bloggers or webmasters running a self-hosted wordpress installation.

One of my favourite wordpress quick-tags is the more tag. Its basic function is to spilt your articles or blog post entries in parts so that only a certain part of it is visible via the Home, Category or Archive pages of your site and thus any visitor interested in getting the whole gist, has to click through to the full article.

The more tag also helps SEO wise to evade any possible Google duplicate content penalties but then it all depends on how your wordpress theme is coded. Some themes don't need the more tag to stop them from showing full articles on the Home, Archive and Category pages.


Now aside this basic function, the more tag could also be used to show any text and image in the form of adverts etc in between long articles.

This is easily done using the code below which must be pasted into the function.php file of your active wordpress theme. Create a backup of function.php before doing this as if you get the pasting wrong, your site will stop showing content and you might even have a problem navigating the wordpress dashboard - backend too.

 

add_filter('the_content', 'adds_block');
function adds_block($content){
if (is_single())
global $post;
$thePostID = $post->ID; // getting the post ID
$new_insert = '' ;
return str_replace($new_insert, my_ad(), $content);
}
function my_ad(){
return 'INSERT AD CODE/CONTENT HERE'; // What you want to display in place of the more tag. can be a custom code, advertisement etc.
}

Now, in some cases such as short articles below 400 words, you would forego the use of the more tag but still want to show that extra code somewhere in the article.

 

How To Show Content When There's No More Tag In WordPress Page

 

Simply copy and paste the code below provided by WordPress.org member 3Stripe into the single.php file of your wordpress theme.

 

<?php if(strstr($post->post_content,'<!--more-->')) { echo 'show this when there's a more tag'; } else { echo 'show this when there's NO more tag'; } ?>

 

Save changes and test to see if it works right.

Unfortunately after much testing, I think it's safe to say that this doesn't work in the sidebar or anywhere else except within the article/content column which is basically run by single.php.

Questions and comments are welcome as always should you run into difficulties or whatever 😉













Share Whatsapp Twitter Facebook Google+





Article written by On September 28, 2016.

BBM Channel Enquiries (No Small Talk pls. Get straight to the point. Thanks): C000B896A