WP read_more($limit);

WordPress read_more($limit); function with limitation. Code and explanation is given below.

//functions.php
function read_more($limit){
$content = explode(' ', get_the_content());
$less_content = array_slice($content, 0, $limit);
echo implode(" ", $less_content);
}
//page.php
read_more(10);

Function's explanation :

First we make a function with a parameter which through we will pass the limit of contents.
Then we take a variable named $content which containing explode (php) function.

What explode function do actually ??
explode(); convert contents in to array basis on which you define. It can be space or any character.
explode(); function has two parameter. First one is for the exact context which divide the content into two array. We have define here space ' '. So according to our statement where it will get space it will built array. And using space in first parameter we have made each word in an array.

Second parameter is for content. get_the_content(); is a (WP) function. We will get content through this function.

$less_content is another variable where we will use a (php) function array_slice();

array_slice(); function reduce content according to your declaration.
array_slice(); function has three parameter. First one is for explode(); content , second one is for where from you want to start slicing and third one is for where you want to end slicing your array.

Now finally another (php) function implode();
This function convert array into content. This function also have two parameter. First one is for what we want to use as divider between two array and the second one is for content where we have kept $less_content.

Comments

Popular posts from this blog

WP register_post_type() with custom CMB2 meta box

Git post receive setup at server for git push to the production from local machine