Fetch several rss feeds from other blogs in one page - wordpress

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>

Related

How to create author dropdown having list of authors in wordpress?

How to create authors list dropdown in wordpress ?
I need to filter posts by author.
Please help !
echo '<form method="get">';
$authors = get_users( 'orderby=nicename' );
$ret='';
foreach ( $authors as $aut ) {
$ret.= '<option value="'.$aut->ID.'">' . esc_html( $aut->user_nicename ) . '</option>';
}
echo '<select name="author">'.$ret.'</select><input type="submit"></form>';
Use this code
<ul>
<?php $allUsers = get_users('show_fullname=1&optioncount=1&orderby=post_count&order=DESC&role=author');
foreach($allUsers as $user){ ?>
<li><?php echo $user->display_name; echo " (".count_user_posts( $user->ID ).")";?></li>
<?php } ?>
</ul>

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.

Using an ACF image object for a custom taxonomy within an echo block loop

I can't seem to pull a ACF image object for a custom taxonomy when used in this loop, which is made of echo blocks:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'product_type' );
$image = get_field('product_type_tax_image');
$hero_image = $image['sizes']['medium'];
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
var_dump($hero_image);
// We successfully got a link. Print it out.
echo '<a href="' . esc_url( $term_link ) . '" class="product-grid- block" style="' . $hero_image[0] . '">';
echo '<div class="product-grid-inner-txt-wrap">';
echo '<h4>' . $term->name . '</h4>';
echo '<p>' . $term->description . '</p>';
echo '</div>';
echo '</a>';
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
The var dump just returns NULL. I've looked at this and this but don't seem to help. What am I doing wrong?
Take a look at this article:
http://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/
To pull a custom field from a Taxonomy Term, you need to add a second item to your get_field function call. Like this:
$image = get_field('product_type_tax_image', $term );
Also, it looks like you're trying to loop through $terms, but you're grabbing the custom field's value outside of your foreach loop, so that should get moved inside that loop, like so:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'product_type' );
foreach ( $terms as $term ) {
//get $term's image
$image = get_field('product_type_tax_image', $term );
$hero_image = $image['sizes']['medium'];
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
var_dump($hero_image);
// We successfully got a link. Print it out.
echo '<a href="' . esc_url( $term_link ) . '" class="product-grid- block" style="' . $hero_image[0] . '">';
echo '<div class="product-grid-inner-txt-wrap">';
echo '<h4>' . $term->name . '</h4>';
echo '<p>' . $term->description . '</p>';
echo '</div>';
echo '</a>';
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>

How do you get the mp3 file extension from an iTunes feed and display it in a WP loop?

I want to pull the 5 most recent podcasts from an iTunes feed and post them along with their audio to a WP page.
The code I have is below
its pulling the feed and displaying the name, details, etc fine but its using the same podcast audio for each item.
<div class="podcastfeed">
<h4>Recent Podcasts</h4>
<?php // Get $feed Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );
$hpFeed=get_post_meta($post->ID, "cmb_hp_feed", true);
$feed = fetch_feed( $hpFeed );
$feed->init();
$feed->handle_content_type();
foreach ($feed->get_items() as $item)
{
if ($enclosure = $item->get_enclosure())
{
$enclosure->get_link();
}
}
if ( ! is_wp_error( $feed ) ) : // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $feed->get_item_quantity( 5 );
// Build an array of all the items, starting with element 0 (first element).
$feed_items = $feed->get_items( 0, $maxitems );
endif;
$attr = array(
'src' => $enclosure->get_link(),
'loop' => '',
'autoplay' => '',
'preload' => 'none'
);
?>
<ol>
<?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 ( $feed_items as $item ) : ?>
<li>
<a href="<?php echo esc_url( $item->get_permalink() ); ?>"
title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('F j, Y') ); ?>">
<p>
<span>
<?php echo esc_html( $item->get_title() ); ?>
</span>
<span><?php printf( __( '%s', 'my-text-domain' ), $item->get_date('F j, Y') ); ?></span>
</p>
</a>
<?php echo wp_audio_shortcode( $attr );?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ol>
</div>
I got it working in the end though it took some fiddling with
The page is now displaying each of the podcasts as audio.
<div class="podcastfeed">
<?php // Get $feed Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );
$hpFeed=get_post_meta($post->ID, "cmb_hp_feed", true);
// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( $hpFeed );
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;
?>
<?php if ( $maxitems == 0 ) : ?>
<ol style="display:none;"><?php _e( 'No items', 'my-text-domain' ); ?></ol>
<?php else : ?>
<h4>Recent Podcasts</h4>
<ol>
<?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('F j, Y') ); ?>">
<p>
<span>
<?php echo esc_html( $item->get_title() ); ?>
</span>
<span>
<?php printf( __( '%s', 'my-text-domain' ), $item->get_date('F j, Y') ); ?>
</span>
</p>
</a>
<?php
if ($enclosure = $item->get_enclosure()){
$enclosure->get_link();
}
?>
<?php
$attr = array(
'src' => $enclosure->get_link(),
'loop' => '',
'autoplay' => '',
'preload' => 'none'
);
?>
<?php echo wp_audio_shortcode( $attr );?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ol>
</div>

Twitter API 1.1 to display latest tweets

I was using the following code to get latest tweets of my account to my wordpress website. As soon as twitter launched its new API 1.1, it all broke completely. How to proceed.
<?php // Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );
// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$cvt_twitter );
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 style="list-style:none;">
<?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.
foreach ( $rss_items as $item ) : ?>
<li>
<div class="row">
<div class="span2">
<a class="thumb" href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank">
<img width="100" height="auto" src="<?=$cvt_logo?>">
</a>
</div>
<div class="span9">
<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') ); ?>" target="_blank">
<?php $string = esc_html( $item->get_title() );
$word = substr($string, 0, strpos($string, ':')+1);
echo str_replace($word, "", $string); ?>
</a>
</div></div>
</li>
<?php endforeach;
endif; ?>
</ul>
Try using the Twitter API 1.1 Client for WordPress.
It's very easy to implement and all you need is your consumer_key and consumer_secret.
Example (from the README):
<?php
// Include Twitter API Client
require_once( 'class-wp-twitter-api.php' );
// Set your personal data retrieved at https://dev.twitter.com/apps
$credentials = array(
'consumer_key' => 'xxxxxxxxxxxxxxxx',
'consumer_secret' => 'xxxxxxxxxxxxxxxx'
);
// Let's instantiate Wp_Twitter_Api with your credentials
$twitter_api = new Wp_Twitter_Api( $credentials );
// Example a - Retrieve last 5 tweets from my timeline (default type statuses/user_timeline)
$query = 'count=5&include_entities=true&include_rts=true&screen_name=micc1983';
var_dump( $twitter_api->query( $query ) );

Resources