How to set get_the_excerpt() limit word in shordcode - wordpress

I want limit words in shortcode. I'm trying to made a WP plugin here i need limited words from get_the_excerpt{(); functions.

you should use official WordPress function:
$trimmed_content = wp_trim_words( $text, $num_words = 55, $more = null );

PHP has so many ways :
Place this in functions.php:
function excerpt($num) {
$limit = $num+1;
$excerpt = explode(' ', get_the_excerpt(), $limit);
array_pop($excerpt);
$excerpt = implode(" ",$excerpt)."... (<a href='" .get_permalink($post->ID) ." '>Read more</a>)";
echo $excerpt;
}
Then, in your theme, use the code <?php excerpt('22'); ?> to limit the excerpt to 22 characters.
Other way : <?php echo substr(get_the_excerpt(), 0,30); ?>
enjoy!!

The same question has been answered on wordpress.stackexchange by Nicolai
Answer:
function kzmagazine_get_excerpt( $count ){
$permalink = get_permalink( $post->ID );
$excerpt = get_the_content(); // or get_the_excerpt();
$excerpt = strip_tags( $excerpt );
$excerpt = mb_substr( $excerpt, 0, $count );
$excerpt = mb_substr( $excerpt, 0, strripos( $excerpt, " " ) );
$excerpt = rtrim( $excerpt, ",.;:- _!$&#" );
$excerpt = $excerpt . ' (...)';
return $excerpt;
}

Related

Want to customize [the_excerpt] length form wp

I'm trying to customize the excerpt length on posts. I'm using this function on function.php:
function get_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" ([.*?])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 25);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/s+/', ' ', $excerpt));
$excerpt = $excerpt.'... [...]';
return $excerpt;
}
and using it on this tag
<article class="secundary">
<div class="mini">
<?php the_post_thumbnail('large', array('class' => 'img-responsive')); ?>
</div>
<h1><?php the_title(); ?></h1>
<p>por <span><?php the_author_posts_link(); ?></span> em <span><?php the_category(' '); ?></span> <?php the_tags('Tags: ', ', '); ?></p>
<p><?php echo get_the_date(); ?></p>
<p><?php get_excerpt(); ?></p>
</article>
Anyone could help me? It didn't work… why?
Thank you! :)
I would avoid limiting by characters as that delivers a performance hit. Instead, limit by words. Put the following in your functions.php:
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`[[^]]*]`','',$excerpt);
return $excerpt;
}
Wherever you use the excerpt in your template files, you can now add the amount of words you would like displayed (for example, 30) as follows:
echo excerpt(30)
You don't have to write a custom function for changing the excerpt length.
You can use the excerpt_length filter. You can use following code in your functions.php file.
function mytheme_custom_excerpt_length( $length ) {
return 25;
}
add_filter( 'excerpt_length', 'mytheme_custom_excerpt_length', 999 );
And then just use the default the_excerpt() tag in your post template.
This will display post excerpt of 25 characters. For more option to customize excerpt check the following link.
https://developer.wordpress.org/reference/functions/the_excerpt/
Hope this helps.

How to add tags to code snippets?

I tried very hard during the day to add and tags but could not do as expected.
Can someone help me to add the tag and the tag in this code snippet?
echo wc_get_product_category_list( $product->get_id(), '<span class="meta-sep">,</span> ', '<span class="posted_in">' . _n( 'Category:', 'Categories:' , count( $product->get_category_ids() ), 'woocommerce' ) . ' ', '</span>' );
For example:
<th>Category:</th> <td>Kit baby</td>
echo '<th>SKU:</th>' . '<td>' . $product->get_sku() . '</td>';
Please help me thanks.
Display Product Tags:
<?php
$current_tags = get_the_terms( get_the_ID(), 'product_tag' );
if ( $current_tags && ! is_wp_error( $current_tags ) ) {
echo '<tr>';
echo '<th>Tags: </th>';
foreach ($current_tags as $tag) {
$tag_title = $tag->name;
$tag_link = get_term_link( $tag );
echo '<td>'.$tag_title.'</td>';
}
echo '</tr>';
} ?>
Display Product Category:
<?php
$current_tags = get_the_terms( get_the_ID(), 'product_cat' );
if ( $current_tags && ! is_wp_error( $current_tags ) ) {
echo '<tr>';
echo '<th>Category: </th>';
foreach ($current_tags as $tag) {
$tag_title = $tag->name;
$tag_link = get_term_link( $tag );
echo '<td>'.$tag_title.'</td>';
}
echo '</tr>';
} ?>
Hope this works for you.

WordPress excerpt height limit

I want to make in my style for WP excerpt like
HERE
When post title is higher, excerpt is reduced to smaller.
Its a plugin or something else?
I just found a solution to limiting the number of words in the excerpt without plugins. Add the following code to your functions.php file.
<?php
// Custom Excerpt
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
// Content Limit
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
?>
If you want to limit your excerpt to 25 words the code would look like this:
<?php echo excerpt(25); ?>
<?php echo content(25); ?>
Another way to display limited excerpt by character. Here is the functions.php file code.
<?php
function get_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 100);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'... Read More';
return $excerpt;
}
?>
After this you need to add where you want to display your customized character by character.
<?php echo get_excerpt(); ?>
Source: Web Design Company Bangladesh
Do it with CSS:
.truncated_exercept{
overflow:hidden;
text-overflow:ellipsis;
}
but in more then one line in current CSS is not possible with nice … (...) ending, have to use one of js or js+jQuery solutions look there:
http://dotdotdot.frebsite.nl/

Plugin's shortcode not working

Hi I have tried as the following
$my_postid = 12;//This is page id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
I have written shortcode in the page with id 12. It doesn't works. Please help!! !
You can use the get_shortcode_regex function to check the post content and isolate the shortcode. Replace my_shortcode with the actual identifier of the shortcode you're trying to get:
$content_post = get_post( 12 );
$content = apply_filters( 'the_content', $content_post->post_content );
$pattern = get_shortcode_regex();
preg_match( '/' . $pattern . '/s', $content, $matches );
if ( is_array( $matches ) && $matches[2] == 'my_shortcode' ) {
$shortcode = $matches[0];
echo do_shortcode( $shortcode );
}

Trim title at side bare

My wordpress theme has a "save post as a favortite" function. And it marks the post as a favorite at side bar. but it wont shorten the title. the long titles at sidebar looks messy. in function i use:
function short_title( $after = '', $length ) {
$mytitle = get_the_title();
if( mb_strlen( $mytitle ) > $length ) {
$mytitle = mb_substr( $mytitle, 0, $length );
echo $mytitle . $after;
} else echo $mytitle;
}
and i call it with:
<?php short_title( '...', 99 ); ?>
how can i put short_title in to here:
echo '</p>';
echo '<h4>' . stripslashes( strip_tags( $post_obj_fave->post_title ) ) . '</h4>';
echo '<p class="info">';
You need to apply the shorten title function to $post_obj_fave->post_title
So change your code from:
echo '<h4>' . stripslashes( strip_tags( $post_obj_fave->post_title ) ) . '</h4>';
To:
echo '<h4>' . stripslashes( strip_tags( shorten_title($post_obj_fave->post_title, $length=99) ) ) . '</h4>';
Now create a function for shorten_title()
Here it is:
function shorten_title($var, $length ) {
if( mb_strlen( $var ) > $length ) {
$var= mb_substr( $var, 0, $length );
return $var;
} else return $var;
}
This should shorten the passed variable ($post_obj_fave->post_title) in this case according to associated length.

Resources