Limit excerpt by characters in Wordpress - php

Right so this seems to have been asked a few times but I cannot seem to find definitive working answer.
Currently I am trying to limit my post title and post excerpt on my WP website.
I have managed to trim the titles by character using
function custom_trim_my_title( $title ) {
if ( strlen( $title ) >= 72 && ! is_singular() ) {
$title = substr( $title, 0, 72 ) . '...';
return $title;
}
return $title;
}
add_filter( 'the_title', 'custom_trim_my_title' );
And that seems to be working okay but I can't seem to get it to work for the excerpt.
I was using this word count:
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
But the longer words still made it look messy so I need a character count and have tried this:
add_filter('the_excerpt','excerpt_char_limit');
function excerpt_char_limit($e){
return substr($e,0,50);
}
But it doesn't seem to work.
Any help would be great.

Try using
add_filter('the_excerpt','excerpt_char_limit',999);
instead. It is just in case if some other filter prevents your filter. (999 means executing later than other filters)

Related

Is there a way to fix this "Trying to get property 'taxonomy' of non-object" error on my Wordpress/Woocommerce site?

I'm setting up a new Wordpress/Woocommerce website using the Avada theme. The site is not live yet.
The store has custom product taxonomies, and as some of the taxonomy names contain a comma I've had to find a code snippet to do a workaround in PHP so that the comma is displayed without splitting the taxonomy name into two separate names (eg. Bouncy Balls, Putty and Slime). The PHP I found has replaced a double dash (--) with a comma in the front end, which appears to work as it should.
My problem is, that since I have added this code I'm getting an error at the top of the page on individual product pages:
Notice: Trying to get property 'taxonomy' of non-object in /var/sites/a/mywebsite.com/public_html/wpnew/wp-content/themes/Avada-Child-Theme/functions.php on line 32
Line 32 code:
if( $tag_arr->taxonomy == 'post_tag' && strpos( $tag_arr->name, '--' ) ) {
All of the code I inserted into functions.php is below.
I'm not sure what to try to fix this as this is all new to me and I'm learning as I go along. I have very little knowledge of PHP, besides the snippet I have found online and placed here.
if( !is_admin() ){
function comma_tag_filter( $tag_arr ){
$tag_arr_new = $tag_arr;
if( $tag_arr->taxonomy == 'post_tag' && strpos( $tag_arr->name, '--' ) ) {
$tag_arr_new->name = str_replace( '--', ', ', $tag_arr->name );
}
return $tag_arr_new;
}
add_filter( 'get_post_tag', 'comma_tag_filter' );
function comma_tags_filter( $tags_arr ) {
$tags_arr_new = array();
foreach( $tags_arr as $tag_arr ) {
$tags_arr_new[] = comma_tag_filter( $tag_arr );
}
return $tags_arr_new;
}
add_filter( 'get_terms', 'comma_tags_filter' );
add_filter( 'get_the_terms', 'comma_tags_filter' );
}
I expected this code to work without errors, but I have the error message at the top of the product pages.
Thanks for any help

Trim user entered excerpt in content.php wordpress file

I have a custom function to trim the excerpt:
function custom_excerpt_length( $length ) {
return 10; }
and the filter to apply it:
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
and then I get the excerpt in my content.php as:
<?php the_excerpt(); ?>
The function works if not custom user entered excerpt is found. so basically grabs the first 10 words from the blog post content. But it doesn't trim when I custom enter the excerpt. Any ideas to display the trimmed version of the user entered excerpt in the blog post page?
I finally got what I needed. So I just wanted to reduce the trimmed excerpt to a lower character count. I just added to my functions page:
function custom_excerpt_more( $more ) {
$more = '…';
return $more;
}
add_filter( 'excerpt_more', 'custom_excerpt_more' ); //displays ... after the excerpt
function custom_excerpt_length( $length ) {
return 30; //number of words to display in the excerpt
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); //999 si it filters at the end
And then in my content.php page:
<p><?php echo substr(get_the_excerpt(),0,80);?></p>
That way I get no more than 80 characters in my blog post list page and 30 words on my individual blog post page (content-single.php).

Load more without refreshing page [Wordpress]

I am using a WordPress plugin to load single post without refreshing the page, you can find it here: https://wordpress.org/plugins/read-more-without-refresh/
The plugin works perfectly fine when i use it's shortcode, it does exactly what i need it to do but here is the problem, I like to use it on all my post and this means i have to edit over 1000 post. Because i have some rules that i need to apply and there are some contents between the opening and closing shortcode elements , it gets tricky to use "do_shortcode"
Here is the shortcode: [read more="Click here to Read More" less="Read Less"] My Hidden Paragraphs Here [/read]
Here is what i want it to do: I need the plugin to apply the shortcode on all posts, after first 150 words and if the post has less than 150 words, it should do nothing.
Is there anyway i can do this? with php code or the already existing shortcode
You could probably use the_content filter but I haven't tested to see if it renders the shortcode or not.
function my_the_content_filter( $content ) {
if ( is_single() ) {
$word_count = str_word_count( strip_tags( $content ), 2 );
if( count( $word_count ) >= 150 ) {
$word = array_slice( $word_count, 150, 1, true );
$word_pos = key( $word );
$content = substr_replace( $content, '[read more="Click here to Read More" less="Read Less"]', $word_pos, 0 );
$content .= '[/read]';
}
}
return $content;
}
add_filter( 'the_content', 'my_the_content_filter' );

Trim Related Products Title Length - Woocommerce

I currently have an issue with trimming the length of the product title in the "Related Products" section of my single product page.
I have set up a custom hook pointing to a custom template for this. The only problem is that it isn't trimming any of it - it is still printing the whole title.
I have tried to use get_the_title() and it doesn't print anything to screen, so in the code below I have tried to pass the title as a string and not an array.
What am I doing wrong?
<?php $title = the_title('<h3 class="product_title entry-title">', '</h3>');
$text = wp_trim_words($title, 2, '...')
?>
<?php echo $text; ?>
Thanks in advance
Regards
Michael
the_title() will be auto print as you don't need to echo it.
You need to use filter for title as the following in functions.php,
function trim_title( $title ) {
$title = wp_trim_words( $title , 40, '...' );
return $title;
}
add_filter( 'the_title', 'trim_title', 10, 1 );
This solution doesn't work in up-sells.php even if it's almost the same as related.php , instead of this whole function trim_title( $title ) in both files after $post_object = get_post( $upsell->get_id() ); (or $related_product) I added $post_object->post_title = substr( $post_object->post_title, 0, 50) . '…'; and it works. It limits to 50 characters, you can change.

How to replace native comment_count with Comments Evolved aggregate count

What I Am Trying To DO
I am using a plugin called Comments Evolved which aggregates comments that are tabbed from Faceook, G+, and WordPress native.
I am trying to replace the native comments_number number with the number from the plugin's aggregate count.
The Problem
I want to do this via functions.php but I am having trouble as it seems to only be pulling the Facebook comments count. I suspect my filter has not worked and hence it's only pulling what WordPress native would pull.
What I've Tried
Currently I am using this filter:
// Replace native comment count with Comments Evolved comment in native comments_number function
function comments_evolved_number() {
$number = comments_evolved_get_total_count();
}
apply_filters('comments_number', 'comments_evolved_number');
but it doesn't seem to be doing the trick as it only shows the number of comments in the Facebook tab.
In my index.php, I am using this to pull the comments:
<?php comments_number( 'Say somethin\'!', '1 comment', '% comments' ) ?>
I've also tried add_filter but that seems to do nothing as the comments aren't output at all. I've searched everywhere, forums, WordPress codex, plugin GitHub, and even looked through similar threads dealing with Disqus comments, but I can't find the reason my filter is failing.
What am I doing wrong?
UPDATE 1
This seems to work:
function wpse_comments_evolved_number( $count )
{
// Override the comment count
if( function_exists( 'comments_evolved_get_total_count' ) )
$count = comments_evolved_get_total_count();
// We must then return the value:
return $count;
}
add_filter( 'get_comments_number', 'wpse_comments_evolved_number');
...but on testing, it seems that it does Not pull the Facebook comment count, though it pulls and aggregates all the rest:
Comments Evolved constructs comments_evolved_get_total_count() like this:
function comments_evolved_get_total_count() {
$total_count = 0;
$wordpress_count = comments_evolved_get_wordpress_count();
//$wordpress_count = get_comments_number();
$gplus_count = comments_evolved_get_gplus_count();
$trackback_count = comments_evolved_get_trackback_count();
$facebook_count = comments_evolved_get_facebook_count();
$disqus_count = comments_evolved_get_disqus_count();
$total_count = $total_count + $wordpress_count + $gplus_count + $trackback_count + $facebook_count + $disqus_count;
return $total_count;
}
//add_filter('get_comments_number', 'comments_evolved_get_total_count', 4269);
The Facebook comments_evolved_get_facebook_count() is constructed like this:
function comments_evolved_get_facebook_count($url = "") {
if(empty($url)){ $url = get_permalink(); }
$link = 'https://graph.facebook.com/?ids=' . urlencode($url);
$link_body = wp_remote_retrieve_body(wp_remote_get($link));
$json = json_decode($link_body);
return $json->$url->comments;
}
I don't see any errors in that and in other places it pulls the correct Facebook count (I think -- not sure).
What did work but doesn't seem efficient/satisfactory
function comment_count_agg() {
$total_count = 0;
//$wordpress_count = comments_evolved_get_wordpress_count();
$wordpress_count = get_comments_number();
$gplus_count = comments_evolved_get_gplus_count();
$trackback_count = comments_evolved_get_trackback_count();
$facebook_count = comments_evolved_get_facebook_count();
$disqus_count = comments_evolved_get_disqus_count();
$total_count = $total_count + $wordpress_count + $gplus_count + $trackback_count + $facebook_count + $disqus_count;
return $total_count;
}
add_filter('comments_evolved_get_total_count', 'comment_count_agg', 4270);
add_filter('get_comments_number', 'comments_evolved_get_total_count', 4271);
...though I am not exactly sure why.
I tried it this way because (a) I figured something in the plugin is messing with the aggregate count before the filter is applied and (b) because I thought maybe the priorities were somehow an issue.
UPDATE 2
I've actually tried both above methods on 2 different sites. On 1 site, everything works perfectly with either method.
On site 2, it fails to pull in the Facebook count towards the whole aggregate count. Any ideas?
This seems to work:
function wpse_comments_evolved_number( $count )
{
// Override the comment count
if( function_exists( 'comments_evolved_get_total_count' ) )
$count = comments_evolved_get_total_count();
// We must then return the value:
return $count;
}
add_filter( 'get_comments_number', 'wpse_comments_evolved_number');
However, on one site that I've tried it, the Facebook count is not being pulled in. On another, everything works perfectly.
Thanks guys, your discussion helps me to hide the total number of comments header by using get_comments_number hook from the frontend as well as I hide all comments by comments_array hook.
// Hide comments counter
add_filter( 'get_comments_number', 0);
// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
// Remove the comment form
add_filter( 'comments_open', '__return_false' );

Categories