Get Wordpress Featured image form external RSS feed - wordpress

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;
}

Related

the link of the product tab appears 2 characters /

I am using WCFM plugin. Everything works fine, there is only one problem that I have not solved yet, that is the link of the product tab appearing 2 // characters, and it is wrong with some other functions on my web.
Here is the screenshot
all other tabs are fine, only the product tab appears 2 characters / as below.
ex: mydomain.com/store/storename//#tab_links_area
And I want it to turn into the following
mydomain.com/store/storename/#tab_links_area
or: mydomain.com/store/storename/product/#tab_links_area
And I have checked that the code that affects this place is in store-tabs.php file of WCFM plugin as follows
<?php do_action( 'wcfmmp_store_before_tabs', $store_user->get_id() ); ?>
<div id="tab_links_area" class="tab_links_area">
<ul class="tab_links">
<?php foreach( $store_tabs as $store_tab_key => $store_tab_label ) { ?>
<li class="<?php if( $store_tab_key == $store_tab ) echo 'active'; ?>"><?php echo $store_tab_label; ?></li>
<?php } ?>
</ul>
</div>
<div class="wcfm-clearfix"></div>
<?php do_action( 'wcfmmp_store_after_tabs', $store_user->get_id() ); ?>
I don't know anything about code, can you help me fix this issue using CSS or using code to insert to function.php in my child theme.
I am very grateful and look forward to your feedback
It seems like the store link is register with a "/" at the end in the database whereas the others are not. Maybe there was a change in the permalink structure between the creation of product page and the other pages.
First thing you can do is to go to settings->permalinks and click the "save changes" to make sure all your links will have the same structure.
If this not solve your issue, you can handle it with your code.
You can test if the last caracter of the string that contains your pages link "$store_user->get_store_tabs_url( $store_tab_key )" end up with a "/" and if it the case delete it before it is used in the anchor tag.
So, in those lines =>
<?php foreach( $store_tabs as $store_tab_key => $store_tab_label ) { ?>
<li class="<?php if( $store_tab_key == $store_tab ) echo 'active'; ?>">
<?php echo $store_tab_label; ?>
</li>
<?php } ?>
you should add the test and have something like that :
<?php foreach( $store_tabs as $store_tab_key => $store_tab_label ) { ?>
<li class="<?php if( $store_tab_key == $store_tab ) echo 'active'; ?>">
<?php
/* Get the link */
$tab_link = $store_user->get_store_tabs_url( $store_tab_key );
/* If the link end up with "/" */
if(substr($tab_link, -1) == "/"){
$tab_link = substr_replace($tab_link ,"",-1); // Delete the last caracter.
}
?>
<?php echo $store_tab_label; ?>
</li>
<?php } ?>

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>

Wordpress - List all the post's categories with custom code in the single page

First of all it's supposed to be used in the single page. After the single post I want to display the categories that the post belongs to.
The basic code for this is <?php the_category(' | '); ?> which outputs a simple link. Then we have <?php echo get_the_category_list(); ?> which outputs a more specific code (http://codex.wordpress.org/Function_Reference/get_the_category_list):
<ul class="post-categories">
<li>
Business
</li>
</ul>
However I need to decompose this code. For example, I want the <a> tag to be before the <li> tag. I've got a code that does what I want, but it's supposed to display all the categories available in my page, which is:
<?php
$categories = get_categories();
foreach($categories as $category)
{
?>
<li><?php echo $category->name ?> <span class="lower">(<?php echo $category->count ?>)</span></li>
<?php
}
?>
Any idea how I can make this work?
Thanks!
You can use get_the_category() its return you category objects that you can loop and do with them what you want.
$category = get_the_category( /* $post_id */ );
print_r($category);
foreach($category as $cat) {
?>
<?php echo $cat->name; ?>
<?php
}
<?php
$category = get_the_category($post_id);
foreach($category as $cat)
{
?>
<li><?php echo $cat->name ?></li>
<?php
}
?>

wordpress the_content not showing video

I am developing a separate website and for showing the blogs i am using the worpress.I have used following code to display blogs.It shows the textual contents properly but for video it just shows the player bar and not click able.
I have also checked themes index.php there is no the_excerpt.
When i check preview of the post using wordpress admin it shows the video properly.
can anyone help me resolve this?
here is my code..
<?php
global $more;
$posts = get_posts('category=3&numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php $more = 1; ?>
<?php the_date(); echo "<br />"; ?>
<span style="color:#1C1644;font-size:1.3em !important;font-weight: bold;">
<?php the_title(); ?>
</span>
<div id="lol"><?php the_content(); ?>
</div>
<hr>
<?php
endforeach;
?>
Please try this
<?php
global $more;
$posts = get_posts('category=3&numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) :
setup_postdata( $post ); ?>
<?php $more = 1; ?>
<?php the_date(); echo "<br />"; ?>
<span style="color:#1C1644;font-size:1.3em !important;font-weight: bold;">
<?php echo the_title(); ?>
</span>
<div id="lol">
<?php echo the_content(); ?>
</div>
<hr>
<?php
endforeach;
?>
All you need to do to embed something into a post or page is to post the URL to it into your content area. Make sure that the URL is on its own line and not hyper-linked (clickable when viewing the post).
For example:
http://www.youtube.com/watch?v=dQw4w9WgXcQ
WordPress will automatically turn that into a YouTube embed when the post is viewed.
You can also optionally wrap the URL in the [embed] shortcode. It will accomplish the same effect, but does not require the URL to be on its own line.
It also allows you to set a maximum (but not fixed) width and height, like so:
[embed width="123" height="456"]http://www.youtube.com/watch?v=dQw4w9WgXcQ[/embed]
If WordPress fails to embed your URL you will get a hyper-link to the URL.
Use wp custom fields. Add video_embed custom field your post and add code.
<?php echo get_post_meta($post->ID, 'video_embed', true); ?>
Edit:
if(get_post_meta($post->ID, 'video_embed', true)){
echo get_post_meta($post->ID, 'video_embed', true);
}
else
{
the_content();
}

Filtering out RSS items with Magpie that contain a specific image

Working with Wordpress on this. I'm using Magpie to grab an RSS feed, but I only want it to display items that include a specific image. Here is the particular tag that I want to search for:
<media:thumbnail url="http://media.publicbroadcasting.net/wfpl/events/images/ourPick5.gif"/>
And here's the specific code I'm using in this application. It's basic, and I'm not incredibly adept at Magpie or PHP (I'm assuming there are more elegant ways of doing this for someone with a more robust knowledge of PHP):
<?php include_once(ABSPATH.WPINC.'/rss.php'); $feed = fetch_rss('http://www.publicbroadcasting.net/wfpl/.eventsfeed'); $items = array_slice($feed->items, 0, 4);?>
<?php if (!empty($items)) : ?>
<?php foreach ($items as $item) : ?>
<?php if (!preg_match('/<media:thumbnail\surl\="http:\/\/media.publicbroadcasting.net\/wfpl\/events\/images\/ourPick5.gif".+?>/i', $item['description']))
continue; ?>
<li><a href="<?php echo $item['link']; ?>"><div class="item-headline"><?php echo $item['title']; ?></div>
<div class="item-info"><?php echo $item['description']; ?>
</div></a></li></ul>
<?php endforeach; ?>
<?php endif; ?>
Again, I only want to display the items with that specific image url in the "media:thumbnail" tag and discard all other items.
Add this part,
<?php if (!preg_match('/<media:thumbnail\surl\="http:\/\/media.publicbroadcasting.net\/wfpl\/events\/images\/ourPick5.gif".+?>/i', $item['description']))
continue; ?>
before
<li><a href="<?php echo $item['link']; ?>"><div class="item-headline"><?php echo $item['title']; ?></div>
<div class="item-info"><?php echo $item['description']; ?>
</div></a></li>

Categories

Resources