How to fetch rss feed month wise from other website - wordpress

I have a question regarding RSS feeds. I am using wordpress ver 3.2.1 and using other website rss feeds in my website. My requirement is to fetch the feeds month wise and to show in my website side bar.
Currently i am using rss widget and showing the feeds in sidebar but they are coming in below format:
Thu 9-29 4:30pm Elementary Quest - Parent Information Night
Thu 10-6 -All Day- Half-Day: Elementary Conferences
I will be very thankful for your help. Thanks in advance.
Update: Mar 13 2012
This code is present in index.php
<h2><?php _e('Recent news from Some-Other Blog:'); ?></h2>
<?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');
$rss = fetch_feed('http://lakewashington.intand.com/index.php?type=export&action=rss&schools=48&groups=884,876,874,996');
print_r($rss);
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) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a href='<?php echo esc_url( $item->get_permalink() ); ?>'
title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
<?php echo esc_html( $item->get_title() ); ?></a>
</li>
<?php endforeach; ?>
</ul>

You can do it manually.
Just use wordpress native function fetch_feed.
You even have an example in there:
<h2><?php _e('Recent news from Some-Other Blog:'); ?></h2>
<?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');
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) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a href='<?php echo esc_url( $item->get_permalink() ); ?>'
title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
<?php echo esc_html( $item->get_title() ); ?></a>
</li>
<?php endforeach; ?>
</ul>
You just need to put that in your sidebar.php, wherever you need to show the feed.
Edit the line:
$rss = fetch_feed('http://example.com/rss/feed/goes/here');
...and point the url to the correct feed.
And:
$maxitems = $rss->get_item_quantity(5);
...to specify how many items to show (five in the example)
Then check the foreach inside the UL, there you can style how the feed are shown.
By default, fetch_feed function will cache the feeds for 12 hours.
If you need to do it every 30 days, you can use the wordpress's Transients API to accomplish that without hassle.

Related

Relationship Field ACF Repeater Badly Needed

<?php
// check if the repeater field has rows of data
if (have_rows('star_role')):
// loop through the rows of data
while (have_rows('star_role')):
the_row();
$myposts = get_sub_field('star_location'); // This is a relationship field
if ($myposts):?>
<ul>
<?php foreach ($myposts as $post_object):?>
<li>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<?php echo get_the_title($post_object->ID);?>
</a>
</li>
<?php the_sub_field('as_on',$post_object);?> / i think this is the line i want to get the specific data same thing below.
<?php the_sub_field('role' ,$post_object);?> / also this one
<?php endforeach; ?>
</ul>
<?php endif;
endwhile;
else:
// no rows found
endif;
?>
Guys! I want to get the current role of specific relationship field like for example.
Actor 1 - As of "Ben" - "Main Role"
Actor 2 - As of "Kacy" - "Supporting Role"
Now, When i'm in post of actor 1 i want to show only the "Ben" and "MainRole" Only i dont want to show the details about Actor 2.
Actually you will look through every actor. This is caused by the foreach.
EDIT
Here you have a code example (not tested) based on the documentation. (Read here)
<?php
// check if the repeater field has rows of data
if (have_rows('star_role')) :
// loop through the rows of data
while (have_rows('star_role')) :
the_row();
$myposts = get_sub_field('star_location'); // This is a relationship field
if ($myposts) : ?>
<a href="<?php echo esc_html( $myposts->post_permalink ); ?>">
<?php echo esc_html( $myposts->post_title ); ?>
</a>
<?php the_sub_field('as_on', $post_object); ?> / i think this is the line i want to get the specific data same thing below.
<?php the_sub_field('role', $post_object); ?> / also this one
< <?php endif;
endwhile;
else :
// no rows found
endif;

How to display posts with plugin (advanced custom fields) field groups?

I have a php/wordpress code (present inside abc.php file) as shown below which displays images of particular size.
php/wordpress (inside abc.php file):
<ul id="list-thumbs" class="list-grid">
<?php
while ( have_posts() ) : the_post();
?>
<li class="list-grid__thumb shows-grid__item">
<a class="list-grid__img-link" href="<?php echo esc_url( get_the_permalink() ); ?>" tabindex="0">
<?php if ( has_post_thumbnail() ) { ?>
<?php
$image_id = get_post_thumbnail_id( get_the_ID() );
WORLD\Images\the_img_fit_figure( $image_id, 'list-grid__image', '(min-width: 450px) 50vw, 70vw', false ); ?>
<?php } ?>
</a>
</li>
<?php endwhile; ?>
</ul>
The code above displays around 30-40 images from top to bottom with 4 images in one row.
(a) All of those images are featured image in wordpress associated with a post of post_type hello-world
(b) Every post has two fields, Row View (in_row_view) and Column View (in_column_view) coming from Advanced Custom Fields plug in. I am not sure if we can call those two fields as custom fields.
(c) Row View and Column View both are switch which can be enabled to Yes or No. By default, it is set to No.
(d) For Row View, I am using:
if(get_field('in_row_view' ) == '1'){ }
For Column View, I am using:
if( get_field('in_column_view' ) == '1'){ }
What I want to achieve is I want to display those posts which have Row View switch enabled to Yes.
Problem Statement:
I am wondering what changes I need to make in the php/wordpress code above so that it displays only those posts which have Row View switch enabled to Yes.
Probably I need to integrate this code if(get_field('in_row_view' ) == '1'){ } in the php/wordpress code above.
Quick/Sloppy Solution:
<ul id="list-thumbs" class="list-grid">
<?php
while ( have_posts() ) : the_post();
// This will skip the loop
if(get_field('in_row_view' ) != '1') { continue; }
?>
<li class="list-grid__thumb shows-grid__item">
<a class="list-grid__img-link" href="<?php echo esc_url( get_the_permalink() ); ?>" tabindex="0">
<?php if ( has_post_thumbnail() ) { ?>
<?php
$image_id = get_post_thumbnail_id( get_the_ID() );
WORLD\Images\the_img_fit_figure( $image_id, 'list-grid__image', '(min-width: 450px) 50vw, 70vw', false ); ?>
<?php } ?>
</a>
</li>
<?php endwhile; ?>
</ul>
... the issue with the "Quick Solution" is that it will not render the desired "posts per page". The easier way is to include or exclude at the query level with meta_query ... this can be done by grabbing the query "in flight" with the pre_get_posts hook.
The More Elegant Solution
function pw_filter_query( $query ) {
// Your conditions for NOT applying the meta query ... use wp conditionals! :)
if( !$query->get('post_type') != 'your_post_type ) { return $query; }
$query->set('meta_key', 'ecpt_color');
$query->set('meta_value', 'green');
}
}
add_action('pre_get_posts', 'pw_filter_query', 9999);
Source Reference

Get Wordpress Featured image form external RSS feed

I used the following code in my FUNCTIONS file:
add_action('rss2_item', 'add_my_rss_node');
function add_my_rss_node() {
global $post;
if(has_post_thumbnail($post->ID)):
$thumbnail = get_attachment_link(get_post_thumbnail_id($post->ID));
echo("<image>{$thumbnail}</image>");
endif;
}
Now I try to call that image and display it from site A to my site B with a custom RSS tempalte. This is the code of the template.
<?php get_header(); ?>
<?php include_once(ABSPATH.WPINC.'/feed.php');
$rss = fetch_feed('https://notrealurl.net/categoryname-feed/');
$maxitems = $rss->get_item_quantity(30);
$rss_items = $rss->get_items(0, $maxitems);
?>
<ul>
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a href="<?php echo $item->get_permalink(); ?>">
<?php echo $item->get_title(); ?>
<?php echo $item->get_date('j F Y # g:i a'); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php get_footer(); ?>
I see the image url in my rss feed now ( thanks to that functions script) and it is in a tag.
Looks like this: https://www.mywebsite./url-of-the-image/
They dont have a .jpg or .png extension at the end of the url. Not sure if that is how it needs to be. When I open that link I can see the image, and it works.
Can somebody please help me to figure this out ? I'm working on this thing for 2 days and can't figure it out. My PHP isn't great so that is most likely the reason why I can't figure it out.
Thanks in advance :)
I think you want your function to get the attachment src instead of the link, and echo that src into a <url> tag inside the <image> RSS element (see here).
Try this:
function add_my_rss_node() {
global $post;
if(has_post_thumbnail($post->ID)):
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
echo("<image><url>{$thumbnail}</url></image>");
endif;
}

Displaying wp post from another WP site

I have two WordPress installations, one is main domain and the other one is sub-domain. Also, both sites use different database. Now I want to automatically post content from subdomain to Main Domain.Is there a way to show the most recent post from the second(sub-domain) site on my first(main domain) sites?
Thanks
I'm Able to solve this.
here is the way:
Step 1: Copy the following code wherever you want to display your original WordPress posts:
<?php
include_once(ABSPATH . WPINC . '/feed.php');
$rss = fetch_feed('http://my-subdomain.com/feed/');
if(!empty($rss)):
$maxitems = $rss->get_item_quantity(4);
$rss_items = $rss->get_items(0, $maxitems);
endif;
?>
Step 2: Add this to your code (preferably right after the snippet above):
<ul>
<?php if ($maxitems == 0) echo 'No news.';
else
foreach ( $rss_items as $item ) : ?>
<li><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></li>
<?php endforeach; ?>
</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.

Resources