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/
Related
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.
I want to make the title of any post from post content. So, I made the following in functions.php
function my_title($title)
{
global $post;
// Do whatever here with your title...
$content = $post->post_content;
print $content;
$title =$content. $post->post_title . ' | ' .
get_bloginfo('name');
return $title;
}
It prints shortcode inside post content but if i apply $content = do_shortcode( $content ); it does not produce the actual post content. When i applied $content = do_shortcode( $content ); website hanged. Let me know how to use $content = do_shortcode( $content ); inside this function so that title can be changed.
Can you try this with wp_filter_nohtml_kses. I guess your html o/p is causing the issue
$content = $post->post_content;
$content = wp_filter_nohtml_kses( $content ); // this wp_filter_nohtml_kses indicates strip_tags
$content = do_shortcode( $content );
or
echo do_shortcode(get_post_field('post_content', $post->id));
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;
}
How create corect function with this code?
<?php $price = get_post_meta($id, 'price_input', true);
if ($price != ''){
echo $price . " Euro";
}
else {
echo "None";
}
?>
I want to put this code in function.php or another place and use <?php myfunction(); ?>
Thx for answer!
That's pretty straight forward PHP:
#in functions.php
function display_price( $post_id ){
$price = get_post_meta($post_id, 'price_input', true);
if ($price != ''){
echo $price . " Euro";
}
else {
echo "None";
}
}
in your template:
<?php display_price( $id ); ?>
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.