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')
Related
I want two differents excerpts on my homepage, one for the posts inside the loop, the other one for the latest post published, outside the loop.
I managed to add the excerpt on the latest post outside of the loop like this
<?php $postslist = get_posts('numberposts=1&order=DESC&orderby=post_date');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<?php wp_latest_excerpt('wp_latest', 'excerpt_more_latest'); ?>
<?php endforeach; ?>
While the excerpt for the posts inside the loop is
<?php wp_excerpt('wp_index'); ?>
For some reasons I am able to set the excerpt length differently for both of my excerpts, but the "more" stays the same. (both "more" have the class .view-article)
I thought I could create another excerpt with a different "more" function, but it does not work, here are my 2 different excerpts and more functions.
For the length
function wp_index($length)
{
return 21;
}
function wp_latest($length)
{
return 18;
}
For the "more"
function view_article($more)
{
return '... </br><a class="view-article" href="' . get_permalink($post->ID) . '">' . __('Continue Reading', '') . '</a>';
}
function latest_view_article($more)
{
return '... </br><a class="view-latest-article" href="' . get_permalink($post->ID) . '">' . __('Continue Reading', '') . '</a>';
}
add_filter('excerpt_more_latest', 'latest_view_article');
add_filter('excerpt_more', 'view_article');
And finally the excerpts
function wp_latest_excerpt($length_callback = '', $more_callback = '')
{
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more_latest', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
function wp_excerpt($length_callback = '', $more_callback = '')
{
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
Is this a wrong way to achieve this or did I make some mistakes in my code ?
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
I've changed my excerpt link in wordpress from "[...]" to "Read More" with the code below. I want to know if there is a way to use more than one link for the excerpt. Let's say for posts in one category have "Read More" for their excerpt, and then posts in another category have "See Photos" for their excerpt link. is this possible?
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_trim_excerpt');
function custom_trim_excerpt($text) { // Fakes an excerpt if needed
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = 24;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '...<br />Read More');
$text = implode(' ', $words);
}
}
return $text;
}
yes you can do it. use excerpt_more filter to change the "[...]" and in_category function to check the category for read more text.
try this code.
function custom_excerpt_more( $more ) {
$read_more_txt = 'Read More..';
if (in_category('cat_slug'))
$read_more_txt = 'See Photos..';
else if (in_category('cat_slug2'))
$read_more_txt = 'Something else..';
return ' <a title="'. $read_more_txt .'" href="'. get_permalink( get_the_ID() ) .'">'. $read_more_txt .'</a>';
}
add_filter( 'excerpt_more', 'custom_excerpt_more' );
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/
I want to extract images from the_content() and want to display first images from post on index.php and want to display all images on single.php. To do this I apply a filter on the_content():
<?php
/*****************************************************************************
Post Summary Filter - Grap Image Thumbnails, Strip Tags, etc
******************************************************************************/
add_filter('the_content', 'filterContentSummary', 0);
function filterContentSummary($content){
global $post;
if( is_home() || is_category() || is_tag() || is_author() || is_date() || is_search() || is_single ){
//if NOT single page or "page" page
$img=''; //default img code
$final_width = 300;
$content = $post->post_content;
//search for first image in article
preg_match('/<img[^>]*>/i',$content,$matches);
//image found, process it
if($matches){
preg_match('/src="[^"]*"/i',$matches[0],$src);
preg_match('/width="(\d*)[^"]*"/i',$matches[0],$width);
if($width[1] < $final_width){
$final_width = $width[1];
}
}
//prepare text
if(!empty($post->post_excerpt)){
$content = $post->post_excerpt;
}else{
//strip shortcodes, tags
$content = preg_replace('/\[\/*[^\]]*\]/i', '', $content); //cont is ready
$content = preg_replace('/<[^>]*>/i', '', $content); //cont is ready
$content = substr($content,0,500);
$content = explode(". ", $content);
array_pop($content);
$content = implode(". ", $content) . "";
}
if($content=="."){
$content = $post->post_content;
}
//prepare final content
$content = "<p>". $content ."</p>";
//Adding Read more link
$content .= "<p align='right' class='seemore'></p>";
}
// Make sure to return the content
return $content;
}
?>
On index.php using regular expressions I find the first image from the post and display it:
<?php while (have_posts()) : the_post();
$content = $post->post_content;
preg_match_all('/(<img.*src="(.*)"[^>]*>)/iU',$content,$matches);
?>
Now $matches have all the images:
<div class="content-img"><img src="<?php echo $matches[2][0];?>"/></div>
...used to display first image on a post.
But how I can display all images on single.php ?
after global $post just add if(is_single()) return $content; this way if it is single template you filter will return the original content with all the images... then display it normally.