Limit post excerpt and post content in Wordpress - php

I have this following snipper :
<div class="content">
<h2><?php echo $article->post_title ?></h2>
<h3><?php echo get_field( 'subtitle', $article->ID ); ?></h3>
<p><?php echo display_post_excerpt( $article->post_excerpt, $article->post_content ); ?></p>
</div>
Here is display_post_excerpt function :
function display_post_excerpt( $excerpt, $content ){
if( $excerpt != '' ){
$text = $excerpt;
}else{
$text = $content;
}
$text = strip_tags( $text );
return $text;
}
I want to limit the post excerpt (and also content if posts dont have excerpt). I dont know how should I proceed and also where (in the function.php? the template? I guess I should use something like :
if (strlen($text) > 20) {
strlen($text) == 20;
}
But as I am a big noob, I am not sure.
Can you help guys?

function display_post_excerpt( $excerpt, $content ){
if( $excerpt != '' ){
$text = $excerpt;
if (strlen($text) > 20) {
$text = substr($text,0,strpos($text,' ',20)) . ' ... [ read more ]'; } ;
}
else{
$text = $content;
}
}
return $text;
}

I think that the better solution is to add to your functions.php file below code:
function wpt_excerpt_length($length) {
return 16;
}
add_filter('excerpt_length', 'wpt_excerpt_length', 999);
You can read more about excerpt here Function Reference/the excerpt

Related

How to split the_content() after some paragraph

I want to add Advertisement after end 7 th paragraph ,after end 22 paragraph and after end 30 th paragraph on my the_content(). How can i do it ?
I try to used below code but there have foreach section because of that i cant used another loop under foreach
<?php
$content = apply_filters( 'the_content', $post->post_content );
if( substr_count( $content, '<p>' ) > $insert_after )
{
$closing_p_tag = '</p>';
$contents = explode( '</p>', $content );
$p_tag_counter = 1;
foreach( $contents as $content )
{
echo $content;
echo $closing_p_tag;
if( $p_tag_counter == 7)
{
?>
<?
}
echo $closing_p_tag;
if( $p_tag_counter == 22)
{
?>
<?
}
echo $closing_p_tag;
if( $p_tag_counter == 30)
{
?>
<?
}
echo $closing_p_tag;
$p_tag_counter++;
}
}
?>

Wordpress functions.php - if condition kills other posts content

So I got this code, which I am trying to adapt to my needs. It should and does wrap a div around the necessary <img> tags. But when I add the "if (is_page...)" all other sites have no content anymore. I am very confused as to why this is happening since the code should not execute or do anything when the if statement is not met.
function breezer_addDivToImage( $content ) {
if ( is_page_template('single.php') ) {
// A regular expression of what to look for.
$pattern = '/(<img([^>]*)>)/i';
// What to replace it with. $1 refers to the content in the first 'capture group', in parentheses above
$replacement = '<div class="myphoto">$1</div>';
// run preg_replace() on the $content
$content = preg_replace( $pattern, $replacement, $content );
// return the processed content
return $content;
}
}
add_filter( 'the_content', 'breezer_addDivToImage' );
/* Place custom code above this line. */
?>
This is how i display the content on the page
<!--S3 show if dynamic page (blogposts)-->
<?php } else if (is_page() == false) {
if(have_posts()) :
while(have_posts()) : the_post();
if ( has_post_thumbnail() ) {
<img class="responsive-img" src="<?php the_post_thumbnail_url(); ?>">
<?php } ?>
<h2><?php the_title(); ?></h2>
<?php the_content();
endwhile;
else : ?>
<h3></h3>
<p></p>
<?php endif;
} ?>
<!--S3 end "show if dynamic page"-->
you filter your content of all pages and posts with this hook and only return the content if the condition is true.
Put simply a else {return $content} to your breezer_addDivToImage function.
function breezer_addDivToImage( $content ) {
if ( is_page_template('single.php') ) {
// A regular expression of what to look for.
$pattern = '/(<img([^>]*)>)/i';
// What to replace it with. $1 refers to the content in the first 'capture group', in parentheses above
$replacement = '<div class="myphoto">$1</div>';
// run preg_replace() on the $content
$content = preg_replace( $pattern, $replacement, $content );
// return the processed content
return $content;
}
else {
return $content;
}
}
add_filter( 'the_content', 'breezer_addDivToImage' );

WordPress excerpt to show 2 paragraphs

I have created two categories within a WordPress site with one post in each category. I am pulling in and excerpt from each category post on different pages.
I am showing the post excerpt like so on each page and adding a read more link manually.
<?php query_posts('cat=4&showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
Read More
<?php endwhile; ?>
The following code will then end the excerpt after the first paragraph.
function post_single_paragrapgh($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
$text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
}
return $text;
}
add_filter( 'wp_trim_excerpt', 'post_single_paragrapgh', 10, 2 );
What I would like to do is tell it to cut off after a second paragraph or, in fact an image and a paragraph.
It will pull in an image if there is one at the top of post but then I also want a further paragraph after the image or just two paragraphs of text. Either or.
I used this site for reference but method C1 throws up errors.
https://www.bybe.net/wordpress-the_excerpt-show-first-paragraph/
Thanks in advance!
Are you open to a solution with regex? Look for <p>(Anything)</p> and merge the two first occurrence. The code is not tested but should work.
function post_single_paragrapgh($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
preg_match_all( '~<p>(.*?)</p>~', $content, $matches );
if( isset( $matches[0][0] ) && isset( $matches[0][1] ) ) {
$text = $matches[0][0] . $matches[0][1];
} else {
$text = $matches[0][0];
}
}
return $text;
}
I managed to figure out what I needed with this code, posted the question too hastily I guess.
function awesome_excerpt($awesomeness_excerpt) {
global $post;
$raw_excerpt = $awesomeness_excerpt;
if ( '' == $awesomeness_excerpt ) {
$awesomeness_excerpt = get_the_content('');
$awesomeness_excerpt = strip_shortcodes( $awesomeness_excerpt );
$awesomeness_excerpt = apply_filters('the_content', $awesomeness_excerpt);
$awesomeness_excerpt = "<p>$awesomeness_excerpt</p>";
$wanted_number_of_paragraph = 2;
$tmp = explode ('</p>', $awesomeness_excerpt);
for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
if (isset($tmp[$i]) && $tmp[$i] != '') {
$tmp_to_add[$i] = $tmp[$i];
}
}
$awesomeness_excerpt .= $excerpt_end;
return $awesomeness_excerpt;
}
return apply_filters('awesome_excerpt', $awesomeness_excerpt, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'awesome_excerpt');
Just took out the end if at the bottom - must have been left there in error. Hopefully it can help others.

Display WordPress full content instead post excerpt

I want to display full content instead the post excerpt. I didn't want to modify the theme. I just want to make a plugin where I will put the file.
I found something like this
function narga_excerpts($content = false) {
# If is the home page, an archive, or search results
if(is_front_page() || is_archive() || is_search()) :
global $post;
$content = $post->post_excerpt;
$content = strip_shortcodes($content);
$content = str_replace(']]>', ']]>', $content);
$content = strip_tags($content);
# If an excerpt is set in the Optional Excerpt box
if($content) :
$content = apply_filters('the_excerpt', $content);
# If no excerpt is set
else :
$content = $post->post_content;
$excerpt_length = 50;
$words = explode(' ', $content, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '...<p><a class="more-link" href="' . get_permalink() . '" title="' . the_title_attribute('echo=0') . '"> ' . __( 'Read more »', 'narga' ) . ' </a></p>');
$content = implode(' ', $words);
endif;
$content = '<p>' . $content . '</p>';
endif;
endif;
return $content;
}
add_filter('the_content', 'narga_excerpts')
But it's totally different. The code modify the_content to show the_excerpts. How can I change the code to display WordPress full content instead post excerpt.
Using the_content() will display the full content of a post.
Changing the function to this should work
function narga_excerpts($content = false)
{
# If is the home page, an archive, or search results
if(is_front_page() || is_archive() || is_search()) :
global $post;
$content = $post->post_content;
endif;
return $content;
}
add_filter('the_content', 'narga_excerpts')

Custom function to limit excerpt length is returning post title as part of excerpt (WP)

I added the following to functions.php:
function excerpt($limit) {
global $id;
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'... <p class="readmore">Read More »</p>';
} else {
$excerpt = implode(" ",$excerpt).'... <p class="readmore">Read More »</p>';
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
and for some reason it's returning the post title as part of the excerpt (Post title comes first, then excerpt without a space between title & excerpt -- sample here: http://autisticadvocacy.uniongraphics.org/category/news/). Can anybody tell me what I am doing wrong?
$excerpt = implode(" ",$excerpt)
. '... <p class="readmore"><a href="'
. get_permalink($id)
. '" title="'
. the_title_attribute(array(
'echo' => 0,
'before' => 'Permalink to: ',
'after' => ''))
### change is in this line. (the_title() removed)
. '">Read More »</a></p>'
;
The way you write the code it's very hard to read and therefore hard to debug when you run into problems. Try to improve that. A useful function for better formatted strings is sprintf():
$excerpt = sprintf('%s ... <p class="readmode">Read More »</p>'
, implode(" ",$excerpt), get_permalink($id)
, the_title_attribute(array('echo' => 0, 'before' => 'Permalink to: ', 'after' => ''))
);
Add the following code to your functions.php file for excerpt and content.
<?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;
}
?>
Now, instead of using the_content() or the_excerpt in your loop, use excerpt($limit) or content($limit).
If you want to limit your excerpt to 300 words the code would look like this:
<?php echo excerpt(50); ?>
<?php echo content(300); ?>
I got another way to display limited excerpt by character. Here is the functions.php file code.
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: http://www.e2soft.com/blog/custom-excerpt-and-content-limit-wordpress/

Categories