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();
Related
I have a code that shows in-article ads after a specified amount of words, the thing is:
If I write a very long article, the ad will be lost due to text length, so there will be only text showing. What I need to do is to create 1 or 2 ads and make it/them repeat indefinitely every 4 paragraphs and 250 words (just an example), based on article length.
HERE'S AN EXAMPLE:
This blog has the very same thing that I'm trying to achieve. As you scroll the article, you'll see that more and more ads will be loaded between the article paragraphs.
THIS IS MY CURRENT CODE:
// Insert ads after a number of words and after the </p> closing tag.
// https://stackoverflow.com/questions/42801541/insert-text-in-content-after-300-words-but-after-closing-tag-of-a-paragraph
function anunciamentosegundo($content) {
$ad_code = '<script type="application/javascript">Adsense code goes here</script>';
// only inject google ads if post is longer than 800 characters
$enable_length1 = 1800;
// Insert at the end of the paragraph every 200 words
$after_word1 = 400;
// Maximum of 2 ads
$max_ads = 2;
if (strlen($content) > $enable_length1) {
$len = strlen($content);
$i=0;
// Keep adding untill end of content or $max_ads number of ads has ben inserted
while($i<$len && $max_ads-->0) {
// Work our way untill the apropriate length
$word_cout = 0;
$in_tag = false;
while(++$i < $len && $word_cout < $after_word1) {
if(!$in_tag && ctype_space($content[$i])) {
// Whitespace
$word_cout++;
}
else if(!$in_tag && $content[$i] == '<') {
// Begin tag
$in_tag = true;
$word_cout++;
}
else if($in_tag && $content[$i] == '>') {
// End tag
$in_tag = false;
}
}
// Find the next '</p>'
$i = strpos($content, "</p>", $i);
if($i === false) {
// No more paragraph endings
break;
}
else {
// Add the length of </p>
$i += 4;
// Get ad as string
ob_start();
echo $ad_code ; //would normally get printed to the screen/output to browser
$ad = ob_get_contents();
ob_end_clean();
$content = substr($content, 0, $i) . $ad . substr($content, $i);
// Set the correct i
$i+= strlen($ad);
}
}
}
return $content;
}
add_filter( 'the_content', 'anunciamentosegundo' );
Currently I can show ads after an amount of words and paragraphs, but not every x amount words and paragraphs. What should I do?
i am trying to display a shoppingcart image instead of the "My Cart"-Phrase. Above the shoppingcart image has just the number of items in the cart to appear.
To change the text one has to edit the mage/checkout/block/links.php and there this part:
public function addCartLink()
{
$parentBlock = $this->getParentBlock();
if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout')) {
$count = $this->getSummaryQty() ? $this->getSummaryQty()
: $this->helper('checkout/cart')->getSummaryCount();
if ($count == 1) {
$text = $this->__('My Cart (%s item)', $count);
} elseif ($count > 0) {
$text = $this->__('My Cart (%s items)', $count);
} else {
$text = $this->__('My Cart (0 items)');
}
to
public function addCartLink()
{
$parentBlock = $this->getParentBlock();
if ($parentBlock && Mage::helper('core')->isModuleOutputEnabled('Mage_Checkout')) {
$count = $this->getSummaryQty() ? $this->getSummaryQty()
: $this->helper('checkout/cart')->getSummaryCount();
if ($count == 1) {
$text = $this->__('</span></span>%s</span></span>', $count);
} elseif ($count > 0) {
$text = $this->__('<span><span>%s</span></span>', $count);
} else {
$text = $this->__('<span><span>0'</span></span>);
}
So, now the items number is showing inside/above the shopping cart image. Just as i wanted it to be. Problem is: By hoovering over the link it shows now the span span-tags before and after the item number.
Any idea how to change that link title there? Or maybe is there a even better way to display a shoppingcart image in the toplinks?
A little below the block you quoted in the Magento source, I see this line:
$parentBlock->addLink($text, 'checkout/cart', $text, true, array(), 50, null, 'class="top-link-cart"');
Try providing different values for the two values that $text is given for - I bet one of them is the HTML of the link, and the other is the tooltip text. (You should probably use $this->__() for the new text as well, for consistency - though it's unlikely to have any effect, since what it does is allow Magento to translate that text to another language.) You could then have spans in the HTML version, while leaving them out of the tooltip.
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';
}
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.
Basically I want to echo only summary of my blog post on a certain page by making a function() that must limit the number of counts of words as specified there.
function sumarize($your_string){
$count++;
$maximum = 10;
foreach(explode("\n", $your_string) as $line){
$count++;
echo $line."\n";
if ($count == $maximum) break;
}
}
Lets say your table (named main) looks like that.
id | blogpost
1 sample1
2 sample2
...
At first you need to connect to db
$db=NEW MYSQLI('localhost', 'username', 'pass', 'dbname') or die ($db->error);
Then write following piece of code
function sumarize($your_string){
$count++;
$maximum = 10;
foreach(explode("\n", $your_string) as $line){
$count++;
echo $line."\n";
if ($count == $maximum) break;
}
}
$result=$db->query("SELECT `id`, `blogpost` FROM `main`");
while($row->fetch_object()){
echo sumarize($row->blogpost);
}
This is how to get work genesis φ's solution
this one takes into account numbers of character whilst ending at the last word without cutting out a character
use
select .... SUBSTR(body,1,300) .....
later you can use this function in php to cut the string at the last space or period so you wont get a half cut word in the end. The second parameter is the number of characters you want.
function shorten_string($string, $characters)
{
$shortened_string = "";
$smaller_string = substr($string, 0, $characters);
$pos_of_last_space = strrpos($smaller_string, " ");
$pos_of_last_break = strrpos($smaller_string, " ");
if (strlen($string) <= $characters) {
$shortened_string = $string;
} elseif (!$pos_of_last_space && !$pos_of_last_break) {
$shortened_string = $smaller_string;
} else {
$break_at = 0;
if ($pos_of_last_space > $pos_of_last_break) {
$break_at = $pos_of_last_space;
} else {
$break_at = $pos_of_last_break;
}
$shortened_string = substr($smaller_string, 0, $break_at);
}
}
NOTE: takes care of spaces put in html with 'nbsp'
Save the summary and body of your blog posts in different columns.