Setting the limit of the_content characters - php

I'm using this code to set the limit of 300 characters to the_content.
How can i set when i want to use it and when i dont ?
<?php
add_filter("the_content", "plugin_myContentFilter");
function plugin_myContentFilter($content)
{
// Take the existing content and return a subset of it
return substr($content, 0, 300);
}
?>

<?php
function plugin_myContentFilter($content) {
if (!is_single()) {
return substr($content, 0, 300);
} else {
return $content;
}
}
add_filter("the_content", "plugin_myContentFilter");
?>
Will shorten the content to 300 characters unless on a single post page.

Related

RSS feed excerpt doesn't display the excerpt

I am trying to display the excerpt in a custom feed page, using the_excerpt_rss() function, I already set the summary show mode in Settings->Reading but it returns me the first paragraph of content, is there any another function to call just the excerpt, I already try with the_excerpt() and get_the_excerpt()?
Here is my code:
<description><![CDATA[<?php html_entity_decode(preg_replace( "/\r|\n/", "",the_excerpt_rss())); ?>]]></description>
I had found a solution. So I customized the function add_feed_content($content):
function add_feed_content($content) {
global $post;
$excerpt = $post->post_excerpt;
if(is_feed()) {
if (strlen($excerpt) > 0){
$content = '';
$content .= $excerpt;
return $content;
} else {
return $content;
}
}
}
add_filter('the_excerpt_rss', 'add_feed_content');

Shortcode output shown at the top of the page

I know this question is similar to other questions that have been posted. I have followed exactly what was suggested in answers to those questions but still can't figure out why the output is shown at the the top of the page.
function foo_shortcode($atts, $content = null) {
$datashortcode = '<div>'.(function_exists('rtb_kk') ? rtb_kk() : '').'</div>';
return $datashortcode;
}
add_shortcode('showfoo', 'foo_shortcode');
Any idea?
Without knowing how the rtb_kk() function works, I can only assume it uses echo to display content rather than using return. This is what causes the output of that function to appear at the top of the page.
To work around this issue, you can capture the output of the function with ob_start() and ob_get_clean():
function foo_shortcode($atts, $content = null) {
if (function_exists('rtb_kk')) {
// Start output buffering
ob_start();
// Run the function
rtb_kk();
// Capture buffer as a string
$output = ob_get_clean();
} else {
// Function doesn't exist so we return an empty string
$output = '';
}
return '<div>' . $output . '</div>';
}
add_shortcode('showfoo', 'foo_shortcode');
Alternative method
If you're able to use the bcn_display() instead of the rtb_kk() method you're using, then there is no need to rely on ob_get_clean().
function foo_shortcode($atts, $content = null) {
if (function_exists('bcn_display')) {
// Return the output as a string so we can control when it's displayed
$output = bcn_display( true );
} else {
// Function doesn't exist so we return an empty string
$output = '';
}
return '<div>' . $output . '</div>';
}
add_shortcode('showfoo', 'foo_shortcode');
This will solve your problem, just try
<script type="text/javascript">
function foo_shortcode($atts, $content = null) {
if(function_exists('rtb_kk')){
$rtb_kk = rtb_kk();
}else{
$rtb_kk = '';
}
$datashortcode = "<div>$rtb_kk</div>";
return $datashortcode;
}
add_shortcode('showfoo', 'foo_shortcode');
</script>

PHP return size of Wordpress Articles

I'm trying to make Wordpress return the length of my articles in 3 categories: Small, Medium and Large.
I'm currently using this function to test it but I doesn't seem to work (I'm a total PHP noob!)
function wcount(){
ob_start();
the_content();
$content = ob_get_clean();
if ($content > 4000) {
return 'large';
} else if ($content > 2500) {
return 'medium';
} else {
return 'small';
}
}
Could anyone please help me? It would be even better if the function automatically added the post to the right category in Wordpress, but for now this was all I could.
Thanks in advance!
You can use the strlen function to get the length of a string:
function wcount()
{
ob_start();
the_content();
$content = ob_get_clean();
$length = strlen($content);
if ($length > 4000) return 'large';
if ($length > 2500) return 'medium';
return 'small';
}

Wordpress the_content <!-- more --> issue?

I'm using a function to decide what the length of my articles are (below). The issue is that on my index page, the_content seems to be 'split up' by my <!-- more --> tags.
For example, my article is 3000 characters long, but my index page only shows 500 characters before the 'Read More' link appears. My function uses the 500 characters as the_content instead of the actual 3000 characters. The length is shown as Short, even though the full articles is considered Long.
However on the full article page, where all 3000 characters are shown, the function works fine.
Now my question is: is there an alternative for the_content that ALWAYS uses the full article instead of what's currently shown on the page?
function wcount(){
ob_start();
the_content();
$content = ob_get_clean();
$length = strlen($content);
if ($length > 2000) {
return 'Long';
} else if ($length > 1000) {
return 'Medium';
} else if ($length < 1000) {
return 'Short';
}
}
Try this:
function wcount($post) {
$length = strlen($post->post_content);
if ($length > 2000) {
return 'Long';
} else if ($length > 1000) {
return 'Medium';
} else if ($length < 1000) {
return 'Short';
}
}
Then call wcount() from within the post loop with something like echo wcount($post);
Try
global $more;
$more = 0;
the_content();

Limiting the output of PHP's echo to 200 characters

I'm trying to limit my PHP echo to only 200 characters and then if there are any more replace them with "...".
How could I modify the following statement to allow this?
<?php echo $row['style-info'] ?>
Well, you could make a custom function:
function custom_echo($x, $length)
{
if(strlen($x)<=$length)
{
echo $x;
}
else
{
$y=substr($x,0,$length) . '...';
echo $y;
}
}
You use it like this:
<?php custom_echo($row['style-info'], 200); ?>
Not sure why no one mentioned this before -
echo mb_strimwidth("Hello World", 0, 10, "...");
// output: "Hello W..."
More info check - http://php.net/manual/en/function.mb-strimwidth.php
Like this:
echo substr($row['style-info'], 0, 200);
Or wrapped in a function:
function echo_200($str){
echo substr($row['style-info'], 0, 200);
}
echo_200($str);
<?php echo substr($row['style_info'], 0, 200) .((strlen($row['style_info']) > 200) ? '...' : ''); ?>
It gives out a string of max 200 characters OR 200 normal characters OR 200 characters followed by '...'
$ur_str= (strlen($ur_str) > 200) ? substr($ur_str,0,200).'...' :$ur_str;
This one worked for me and it's also very easy
<?php
$position=14; // Define how many character you want to display.
$message="You are now joining over 2000 current";
$post = substr($message, 0, $position);
echo $post;
echo "...";
?>
<?php
if(strlen($var) > 200){
echo substr($var,0,200) . " ...";
}
else{
echo $var;
}
?>
this is most easy way for doing that
//substr(string,start,length)
substr("Hello Word", 0, 5);
substr($text, 0, 5);
substr($row['style-info'], 0, 5);
for more detail
https://www.w3schools.com/php/func_string_substr.asp
http://php.net/manual/en/function.substr.php
string substr ( string $string , int $start [, int $length ] )
http://php.net/manual/en/function.substr.php
more flexible way is a function with two parameters:
function lchar($str,$val){return strlen($str)<=$val?$str:substr($str,0,$val).'...';}
usage:
echo lchar($str,200);
function TitleTextLimit($text,$limit=200){
if(strlen($text)<=$limit){
echo $text;
}else{
$text = substr($text,0,$limit) . '...';
echo $text;
}
echo strlen($row['style-info']) > 200) ? substr($row['style-info'], 0, 200)."..." : $row['style-info'];
echo strlen($row['style-info'])<=200 ? $row['style-info'] : substr($row['style-info'],0,200).'...';
Try This:
echo ((strlen($row['style-info']) > 200) ? substr($row['style-info'],0,200).'...' : $row['style-info']);
In this code we define a method and then we can simply call it. we give it two parameters. first one is text and the second one should be count of characters that you wanna display.
function the_excerpt(string $text,int $length){
if(strlen($text) > $length){$text = substr($text,0,$length);}
return $text;
}

Categories