Define the Beginning of a Wordpress Excerpt - wordpress

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.

Related

Want to customize [the_excerpt] length form wp

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.

Reversing sort order of ingested RSS feed in PHP

I have a site where I am 'pulling' local events from a secondary website RSS feed. I have this working however the feed is displaying in reverse order with the local events dated later (i.e. at the end of October versus events dated for today) showing up at the top instead of the bottom.
Here is the code I am using for the feed ingest:
<?php if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed('http://sample.com.au/events/feed/'); // specify the source feed
$limit = $feed->get_item_quantity(25); // specify number of items
$semti = array_flip($limit);
$items = $feed->get_items(0, $limit); // create an array of items
}
if ($limit == 0) echo '<div>The feed is unavailable.</div>';
else foreach ($items as $item) : ?>
<p><b><a href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank">
<?php echo esc_html( $item->get_title() ); ?></a></b>
<?php echo esc_html( $item->get_date('| j F | g:i a') ); ?><br>
<?php echo sanitize_text_field( $item->get_content() ); ?>
</p>
<?php endforeach; ?>
This works perfectly to get my remote RSS feed and display the title, date of the event and the excerpt, however the order is reverse sorted.
I tried adding filters like "sort and ksort" in the "foreach ($items $items) :" area but this did not work for me. I've racked my brains on this one and am hoping someone can help me out. I appreciate any guidance/help in advance.
Try the appropriately named array_reverse function!
<?php if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed('http://sample.com.au/events/feed/'); // specify the source feed
$limit = $feed->get_item_quantity(25); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
$semti = array_reverse($items); // & flip it
}
if ($limit == 0) echo '<div>The feed is unavailable.</div>';
else foreach ($semti as $item) : ?>
<p><b><a href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank">
<?php echo esc_html( $item->get_title() ); ?></a></b>
<?php echo esc_html( $item->get_date('| j F | g:i a') ); ?><br>
<?php echo sanitize_text_field( $item->get_content() ); ?>
</p>
<?php endforeach; ?>
From PHP.net:
array_reverse
Return an array with elements in reverse order
array array_reverse ( array $array [, bool $preserve_keys = false ] )
Takes an input array and returns a new array with the order of the elements reversed.

Fetch several rss feeds from other blogs in one page

I am trying to make a function which take an rss fedd URL and fetches the most recent 2 posts. I have tried to remake the snippet from here to a full function in funtions.php as following. I don't want to use a plugin for this since the plugins I have looked at have been close to impossible to style with my own html...
function fetch_feed_from_blogg($path) {
$rss = fetch_feed($path);
if (!is_wp_error( $rss ) ) :
$maxitems = $rss->get_item_quantity(2);
$rss_items = $rss->get_items(0, $maxitems);
endif;
function get_first_image_url($html)
{
if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) {
return $matches[1];
}
}
function shorten($string, $length)
{
$suffix = '…';
$short_desc = trim(str_replace(array("/r", "/n", "/t"), ' ', strip_tags($string)));
$desc = trim(substr($short_desc, 0, $length));
$lastchar = substr($desc, -1, 1);
if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?') $suffix='';
$desc .= $suffix;
return $desc;
}
if ($maxitems == 0) echo '<li>No items.</li>';
else
foreach ( $rss_items as $item ) :
$html = '<ul class="rss-items" id="wow-feed"> <li class="item"> <span class="rss-image"><img src="' .get_first_image_url($item->get_content()). '"/></span>
<span class="data"><h5><a href="' . esc_url( $item->get_permalink() ) . '" title="' . esc_html( $item->get_title() ) . '"' . esc_html( $item->get_title() ) . '</a></h5></li></ul>';
return $html;
}
I am also trying to make it so that it can be used several times on a single page.
Much easier to use WordPress's built-in RSS function. See https://codex.wordpress.org/Function_Reference/fetch_feed
Use it as many times as you want in a php template, or make it generate a shortcode. Style the <ul> and <li> and add a containing <div> if needed.
Example:
<?php // Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );
// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'http://example.com/rss/feed/goes/here' );
$maxitems = 0;
if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity( 5 );
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items( 0, $maxitems );
endif;
?>
<ul>
<?php if ( $maxitems == 0 ) : ?>
<li><?php _e( 'No items', 'my-text-domain' ); ?></li>
<?php else : ?>
<?php // Loop through each feed item and display each item as a hyperlink. ?>
<?php foreach ( $rss_items as $item ) : ?>
<li>
<a href="<?php echo esc_url( $item->get_permalink() ); ?>"
title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
<?php echo esc_html( $item->get_title() ); ?>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>

How to make multiple page IDs simpler (array?)

The simple code below:
<?php
$callout_1 = '1';
$callout_2 = '3';
$callout_3 = '5';
?>
<ul>
<li><h3 class="title"><?php echo get_the_title($callout_1); ?></h3></li>
<li><h3 class="title"><?php echo get_the_title($callout_2); ?></h3></li>
<li><h3 class="title"><?php echo get_the_title($callout_3); ?></h3></li>
</ul>
I have lots of more stuff inside the <li> tags, but all has the same structure, the above sample is just an example. Can anyone help me to make it easier please?
Edit: Thanks for the answer Tamil, Can you help me on another question based on this please? If 1 then echo text 1, if 2 then echo text 2 ...
Use array to store the page ids
Try
<?php $callout = array('1', '3', '5'); ?>
<ul>
<?php
foreach($callout as $call) {
?>
<li><h3 class="title"><?php echo get_the_title($call); ?></h3></li>
<?php } ?>
You can create a associative array. Its a multiple array having key=>value pair. Value is accessed by key.
`$callouts = array(array('id'=>1, 'text'=>'text1'), array('id'=>2, 'text'=>'text2'),array('id'=>3, 'text'=>'text3'));`
So now while looping you can print both id and text.
foreach( $callouts as $call ) {
echo '<li><h3>'.get_the_title($call['id']).'</h3></li>';
echo '<p>'.$call['text'].'</p>';
}
If you have to get the value and store in array:
$ids = array('1','2','3');
$callouts = array();
foreach($ids as $id ){
$title = get_the_title($id);
if( $id == 1)
$text = 'Text 1';
elseif($id == 2)
$text = 'Text 2';
else
$text = '';
$callouts[] = array('id'=>$id, 'title'=>$title, 'text'=>$text);
}
To print, you can use single array to print everything.
foreach( $callouts as $call ){
echo '<li><h3>'.$call['id'].'<h3></li>';
echo '<li><h3>'.$call['title'].'<h3></li>';
echo '<li><h3>'.$call['text'].'<h3></li>';
}
Hope this helps

how to show limited content of page in wordpress

I have code the displaying content and `title' of page.I want to show only 150 words of that particular page content.
Here is my code
<?php
$args = array(
'include' => 1319,
'post_type' => 'page',
'post_status' => 'publish'
);
$mypages = get_pages($args);
foreach($mypages as $page)
{
$content = $page->post_content;
$content = apply_filters('the_content', $content);
?>
<div class="page_botheadingtop">
<?php echo $page->post_title ?>
</div>
<div class="page_botheadingmiddle">
<?php echo $page->post_content; ?>
</div>
<div class="page_botheadingbottom">
Read More..
</div>
<?php
}
?>
This code showing all content of page that have id 1319.I want to disply only 150 word please provide me the suggestion.
I shall be very thankful to you
I am waiting for your reply
thanks
Use
<?php
the_excerpt_max_charlength(140);
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;
}
}
?>
Or
<?php
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>

Resources