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.
Related
My loop looks like this:
<!-- loop for the posts here -->
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => 'news'
);
$query = new WP_Query($args);
while($query->have_posts()) : $query->the_post();
?>
<div class="news_box_content">
<h5><?php the_title(); ?></h5>
<figure><?php the_post_thumbnail(); ?></figure>
<?php if($post->post_excerpt) { ?>
<p><?php echo get_the_excerpt(); ?></p>
Read more...
<?php } else {
the_content('Read More');
} ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
I used a function to count the excerpt length but it is not working.
function custom_excerpt_length(){
return 10;
}
add_filter('excerpt_length', 'custom_excerpt_length');
How can I limit the number of characters on any excerpt?
No need for an extra filter
echo substr(get_the_excerpt(), 0,10);
Try this one. I always use this one
<?php
$content = get_the_content();
$content = strip_tags($content);
$dots = strlen(substr($content,0,50)) < 50 ? " " : "...";
echo substr($content,0,50) . $dots;
?>
Add this to your functions.php file:
function trim_excerpt( $exc ) {
$exc_trimmed = substr( $exc, 0, 150 ) . '...';
return $exc_trimmed;
}
add_filter( 'the_excerpt', 'trim_excerpt', 999 );
In single.php I use <?php the_category(', '); ?>. This function lists categories attached to post, but title attribute (on hover) is missing. How can I add this? I've tried with adding a filter in functions.php and making a new function like <?php the_better_category('%cat% - my text'); ?>, but the result is miserable.
From the WordPress Codex:
<?php single_cat_title( '', true ); ?>
The first part (in single quotes) will output whatever custom text you put there before the category title and true means it will display (false is use in PHP).
Have you tried:
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
?>
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/
I am trying to shave off a few characters or pieces of text we have in our posts. We have the date and source from which the story in our posts cam but I would not like that included in our excerpt and the formatting people insist that it must remain at the top of the post.
How would I go about specifying exactly where I would like the excerpt to begin in the post? Could I have it begin at something like <p> tag or could I set the number of characters to skip before it begins?
Any help would be greatly appreciated. Here is my code thus far:
<phpcode>
<?php $my_query = new WP_Query('category_name=science&showposts=5'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div id="postlist_container">
<h4 class="und"></h4>
<?php get_the_image(array( 'image_scan' => true , 'image_class' => 'small_image_left','width' => 80 , 'height' => 80)); ?><div class="post_desc"><date><?php the_time('M j, Y') ?></date> · <a href="<?php the_permalink() ?>">
<?php the_title(); ?></a> <br /><br /><?php the_excerpt_max_charlength(250); ?>
</div>
</div>
<div class="clear"></div>
<?php endwhile; ?>
<?php
function the_excerpt_max_charlength($charlength) {
$excerpt = get_the_excerpt();
$charlength++;
if ( mb_strlen( $excerpt ) > $charlength ) {
$subex = mb_substr( $excerpt, 0, $charlength - 5 );
$exwords = explode( ' ', $subex );
$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
if ( $excut < 0 ) {
echo mb_substr( $subex, 0, $excut );
} else {
echo $subex;
}
echo '[...]';
} else {
echo $excerpt;
}
}
?>
</phpcode>
If the date/source are always the same length (which is probably unlikely), then you could use substr() on $excerpt to remove X number of characters:
// assume we want to remove the first 10 chars
$chars_to_skip = 10;
// get the full excerpt
$excerpt = get_the_excerpt();
// check the length
if ( strlen( $excerpt ) > $chars_to_skip ){
// remove chars from the beginning of the excerpt
$excerpt = substr( $excerpt, $chars_to_skip );
}
What's more likely is that you would need to do a regex search and replace to remove whatever the pattern matches even when the exact length of the source or date text differs post to post. You could use preg_replace() (api info) to accomplish this, but I can't help with the regular expression not knowing the format you're using.
I want to remove the excerpt function or render it functionless as i want all posts to be viewed in full of its content. I think the theme has some tracking to make sure the excerption is in that line, so it must exist.
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;
}
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;
}
I only have these 2 lines of codes which i know is related.
I dont know where the $limit come from, i tried to find on all theme related php,no findings.
Please help me. Thank you very muc.
Just find your template file (could be index.php) where you want to display the full content instead of excerpted content and replace the function the_excerpt() with the_content() inside the loop, i.e.
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- do other stuff ... -->
the_content();
<?php endwhile; ?>
<?php endif; ?>
About the_content() and the loop.