Show post excerpt - wordpress

I want to show several post excerpt, so I create function
function my_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;
}
and I use it:
<?php echo bereza_excerpt(35); ?>
but I have problem - I cant save post formatting.
Is it possible to recreate this function and save text formatting in post excerpt?

If you're trying to limit the number of words of your excerpt to echo, I'd recommend you to use wp_trim_words.
This function trims text to a certain number of words and returns the trimmed text.
Try:
<?php
echo wp_trim_words( get_the_excerpt(), 35, '…' );
Update:
Ops! I missed the keeping the format part.

Related

wp_trim_words not working on anything but ordinary strings

I´m a bit out of my field here and I´m confused about this. Using wp_trim_field doesn't work for me except for regular strings.
This does not work, it returns the whole text:
<?php
$field = the_field('project_description');
$trimmedfield = wp_trim_words( $field, $num_words = 1, $more = '… ' );
echo '<p>' . $trimmedfield . '</p>';
?>
This however does work:
<?php
$field = 'this text does get trimmed';
$trimmedfield = wp_trim_words( $field, $num_words = 1, $more = '… ' );
echo '<p>' . $trimmedfield . '</p>';
?>
Echoing out the $field instead does echo out the text that I am trying to trim, but the trimming aint working. Any ideas as to why?
edit - I also tried this, same thing happens:
<?php
$length = 1;
$text = the_field('project_description');
$words = explode(' ', $text);
array_splice($words, $length);
$text = implode(' ', $words);
echo $text;
?>
use var_dump($field); wp_trim_words( $field,....) $field must be string type ...check if this is or not to test the datatype, if its not im sure you know what to do then.
Use typecast if its not.
You'll need to change the $field variable to this: $field = get_field('project_description');
the_field(); outputs the content, while get_field(); retrieves it. In order to pass it through a function, you'll need to retrieve it.
ACF documentation page that answers this question: https://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/

How to display WordPress RSS feed your website?

Hello i have a website and a blog, i want to display my self hosted wordpress blog on my website.
I want to show only 3 post on my website.
I want to automatically check for any new post everytime when i reload my website, so that the recent three gets displayed only.
I want to show the complete title of my wordpress blogpost but specific letters of description.
Also the description should end up with a word not some piece of non-dictionary word ending with "..."
How this can be done, i have heard that it can be done through RSS.
Can somebody help me?
To accomplish this you need to read the RSS of the blog, from RSS you need to read the Title and the description, after reading the whole description and title you need to trim the description to your desired number of letters. After that you need to check weather the description last word has been completed or not and then you need to remove a the last word if not completed and put the "...".
First we will make a script to trim the description and to put "..." in last:-
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
}
else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
}
else {
$output = $text;
}
return $output;
}
Now we will define the variables in which we store the values:-
$xml=("http://your-blog-path/rss/");
global $item_title, $item_link, $item_description;
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$x=$xmlDoc->getElementsByTagName('item');
Now, we will make an array and store values in it. I am only taking 3 because you have asked it the way. You can change it to anything (The number of post you want to show, put that in the loop)
for ($i=0; $i<3; $i++)
{
$item_title[$i] = $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$item_link[$i] = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$item_description[$i] = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
}
?>
Now echo all these values, Link is the value where your user will click and he will be taken to your blog:-
FIRST RECENT POST:
<?php echo $item_title[0]; ?>
<?php echo substrwords($item_description[0],70); ?>
SECOND RECENT POST:
<?php echo $item_title[1]; ?>
<?php echo substrwords($item_description[1],70); ?>
THIRD RECENT POST:
<?php echo $item_title[2]; ?>
<?php echo substrwords($item_description[2],70); ?>
Hope this can solve your problem. By the way Nice question.
Click here for the original documentation on displaying RSS feeds with PHP.
Django Anonymous's substrwords function is being used to trim the description and to insert the ... at the end of the description if the it passes the $maxchar value.
Full Code:
blog.php
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
} else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
} else {
$output = $text;
}
return $output;
}
$rss = new DOMDocument();
$rss->load('http://wordpress.org/news/feed/'); // <-- Change feed to your site
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3; // <-- Change the number of posts shown
for ($x=0; $x<$limit; $x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substrwords($description, 100);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
You can easily put this in a separate PHP file (blog.php) and call it inside your actual page.
Example:
social.php
<h3>Latest blog post:</h3>
<?php require 'blog.php' ?>
Also, this code is plug-n-play friendly.
Why not use the Wordpress REST API to retrieve posts -
API URL is : https://public-api.wordpress.com/rest/v1/sites/$site/posts/
where $site is the site id of your wordpress blog
or else simply use this plugin -
http://www.codehandling.com/2013/07/wordpress-feeds-on-your-website-with.html

how to remove read more when full text is on in wordpress?

the code looks like this
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
there is an option in wp-admin>settings>reading>For each article in a feed, show
if this is set to full text
the excerpt() must return full length article instead of specified length.
how to do this?
Good question! The answer is simple: write your own function!
Open up functions.php in your favorite editor and mash random buttons on your keyboard until you get something like this:
function my_awesome_excerpt ($post_id = false, $full = false, $length = 22) {
if (!$post_id) global $post;
else $post = get_post($post_id);
$text = $post->post_content;
if ($full) return $text;
else {
$text_array = explode(' ', $text);
$return_string = array();
for ($i = 0; $i <= $length; $i++)
array_push($return_string, $text_array[$i]);
$new_awesome_string = '<p>';
$new_awesome_string .= implode(' ', $return_string);
$new_awesome_string .= '</p><p class="readmore">';
$new_awesome_string .= '<a href="' . get_permalink($post_id) . '">';
$new_awesome_string .= 'Read More' . '</a></p>';
return $new_awesome_string;
}
}
Now, you're ready for the cool part. Once you're in your loop, you can write out some magic like this:
echo my_awesome_excerpt();
and it will automagically spit out an excerpt. It's using the global post variable and everything! You can even use it outside of the loop:
echo my_awesome_excerpt($cpt->ID, 22);
and set your own special length!
Or maybe you just know in your heart that it's not worth it, you just want to show the whole thing. How's that look?
Inside the loop, you're going to have to give it a post ID, sorry about that.
echo my_awesome_script($post->ID, false);
I hope this helps. Have a great day!

Highlighting Word(s) Searched on in WordPress Search

I am using built-in WordPress search.php routines, is it possible to highlight the word searched on, in context with the search results retrieved?
For example, if I typed in "products", any page that returned this matching word would be highlighted to the user.
Thanks.
Here is a function you can add to functions.php that will highlight the searched term in the results.
/* Search Highlighting ********************************************/
// This highlights search terms in both titles, excerpts and content
function search_excerpt_highlight() {
$excerpt = get_the_excerpt();
$keys = implode('|', explode(' ', get_search_query()));
$excerpt = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $excerpt);
echo '<p>' . $excerpt . '</p>';
}
function search_title_highlight() {
$title = get_the_title();
$keys = implode('|', explode(' ', get_search_query()));
$title = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $title);
echo $title;
}
To use this function it has to be added to your archive loop:
<?php if (is_search() ) {
search_excerpt_highlight(); } ?>
Here is a few WP Plug-in that highlights search words using jQuery:
http://wordpress.org/extend/plugins/highlight-search-terms/
http://urbangiraffe.com/plugins/search-unleashed/
or do it yourself:
http://www.livexp.net/wordpress/highlight-the-search-terms-in-wordpress-search-results.html

Wordpress excerpt - image and text

This is the mockup I have:
http://img697.imageshack.us/img697/3172/featuresb.jpg
Each section will be an excerpt of a post that has a certain tag.
Is there any way of doing this so a client doesnt have to touch and tags or code like that?
You could filter the content with your own function.
Instead of using <?php the_content() ?> its better to use this: <?php your_function(get_the_content()) ?>
The function can be put on functions.php and you are free to code.
You can use the_content filter like so,
add_filter('the_content', 'my_content_filter'); //
function my_content_filter($content) {
global $post;
if($post->post_excerpt == ''){ // check if the post has excerpt
$content = strip_tags($content); //strip tags
$cont_array = explode(' ',$content);
if(count($cont_array) > 55) //number of words wanted in excerpt default is 55
$content = implode(' ',array_slice($cont_array, 0, 55)).'...';
$content = '<p>'.$content.'</p>';
}else{
$content = $post->post_excerpt; //copy excerpt to content
}
return $content; //return content
}
The above code checks if the post has excerpt, if it has excerpt it returns excerpt else returns first 55 words (default length for excerpt).
use this code for limit the post content.
<?php substr($post->post_content, 0, 12); ?> ...
Put this in your themes functions.php file:
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;
}
then add this this in your loop:
<?php print '<p>'.excerpt(40).'</p>'; ?>

Resources