echo category in rss feed - wordpress

I am pulling an rss feed into my page templates using the default /feed.php. I can echo out the title, permalink and description just fine, but I can't seem to get the categories to display. Ideally I would like to use the category to insert a background image into a div by class.
This is what I have:
<?php if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php');
$feed = fetch_feed('http://www.*link to the feed*');
$limit = $feed->get_item_quantity(50);
$items = $feed->get_items(0, $limit);
}
if ($limit == 0) echo 'The feed is either empty or unavailable.';
else foreach ($items as $item) : ?>
<div class="feedItem">
<div class="<?php echo $item->get_categories(); ?>"></div>
<ul>
<li class="feedtitle"><a href="<?php echo $item->get_permalink(); ?>"
target="_blank"> <?php echo $item->get_title(); ?></a> </li>
<li class="feeddate"><?php echo $item->get_date('m/d/y'); ?></li>
<li><p><?php echo($item->get_description()); ?></p></li>
</ul>
</div><!--item-->
<?php endforeach; ?>
But the <?php echo $item->get_categories(); ?> only echos out <div class="Array"></div>
How do I get the names of the categories from the rss feed?
Any help is appreciated.

Look at your code.
<div class="<?php echo $item->get_categories(); ?>"></div>
You're putting the result of get_categories in the class attribute of your <div>.
According to the documentation, get_categories returns an Array. Therefore, <div class="Array"></div> is exactly what's expected.
I assume you'd rather see something like <div class="category1 category2 category3"></div>? If so, you'll need to work with the array that get_categories returns.
Edited code, based on http://simplepie.org/api/class-SimplePie_Category.html
<?php
$cat_terms = array();
for ($item->get_categories() as $cat) {
$cat_terms[] = $cat->get_term();
}
?>
<div class="<?php echo implode(" ", $cat_terms); ?>"></div>

Related

How can I get ACF data on WooCommerce template-homepage.php

I cannot get my repeater to work on template-homepage.php.
I have created a group called 'homepage_settings' and in this group is a repeater called 'categories'. I cannot get this content to show on the template-homepage.php file.
<ul class="medium-block-grid-3 small-block-grid-1" data-equalizer>
<?php
$rows = get_field('categories');
if($rows) {
foreach($rows as $row) {
?>
<li>
<a href="<?php echo $row['link']; ?>">
<span class="bg" style="background-image: url('<?php echo $row['image']; ?>')"></span>
<span class="title"><?php echo $row['category_name']; ?></span>
</a>
</li>
<?php
}
}
?>
</ul>
No content is being displayed and no errors?
It sounds like you need to grab "homepage_settings" and not "categories" first. Does this line of code print anything?
<?php
$rows = get_field('categories');
echo '<pre>'.print_r($rows,1).'</pre>';
?>
If that doesn't produce anything, then you are grabbing the wrong field. First grab homepage_settings and see if that has anything inside.
<?php
$rows = get_field('homepage_settings');
echo '<pre>'.print_r($rows,1).'</pre>';
?>
That should have your "categories" data inside (make sure you have put content into the fields in the CMS). Then, you can use a foreach to loop through it.
<?php
foreach($rows['categories'] as $row) {
echo '<pre>'.print_r($row,1).'</pre>';
}
?>
That should be it. Take it one variable at a time, and go from there.

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

woocommerce retriving category name as div class?

ok I am trying to retrive the category name of a woocommerce product displayed in a wordpress loop and use it as the class for a li also inside the loop i've tried this:
<div id="isocontent" class="products">
<ul><?php while (have_posts()) : the_post(); ?>
<li class="<?php echo $product->get_categories(); ?> box">
<?php echo the_post_thumbnail(); ?>
<p><?php the_title(); ?></p>
<span href="<?php the_permalink(); ?> " class="amount price" data-original="<?php echo get_woocommerce_currency(); ?><?php echo $product->get_price(); ?>" data-price="<?php echo $product->get_price(); ?>" title="Original price: <?php echo $product->get_price(); ?>"><?php echo get_woocommerce_currency(); ?><?php echo $product->get_price(); ?></span>
Add to Cart
</li>
<?php endwhile; ?>
</ul>
</div>
this being the part i'm trying to retrive the class with:
<li class="<?php echo $product->get_categories(); ?> box">
but it just outputs this:
<li class="<a href=" http:="" localhost.no="" fanny="" kategori="" interior-sv="" "="" rel="tag">
which does retrieve the category but also screws with the markup breaking the loop.
I've also tried this:
<li <?php post_class('box'); ?>
but because woocommerce uses taxonmys it retrives the tags but not the product category.
any help is much appriciated
Kind regards
Chris
It's not quite as easy as making a single call - get_categories() is designed to show an HTML representation of the product categories. The product categories are actually a custom taxonomy, so you have to use get_the_terms() to get at it.
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_id = $term->term_id;
$category_name = $term->name;
$category_slug = $term->slug;
break;
}

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>

Display WordPress Posts in Static HTML page - Adding more details

I need to add links to WordPress Posts on a static HTML page. I got some information that has been helpful but need a few more elements to make it complete.
Here is the code I have so far:
<?php
$number = 5;
$wordpress_header = "blog/wp-blog-header.php";
// Include wordpress header
if (file_exists($wordpress_header))
{
include ($wordpress_header);
$myposts = get_posts('numberposts=$number&offset=0&category=0');
echo "<ul class='Bloglinks'>";
foreach(array_slice($myposts, 0, $number) as $post)
{
echo '<li><a href="';
the_permalink();
echo '">';
the_date();
echo " ";
the_title();
echo '</a></li>';
}
echo "</ul>";
}
else
{
echo "Unable to connect to Wordpress header file.";
die();
}
?>
This only shows the titles of the most recent posts. I need to be able to display the following:
<h5>The truth about Lazy Eye</h5>
<p class="blog-info">07.16.10 | <a class="comment-ref">3 Comments</a></p>
<h5>More Adult Learning Problems Linked to Eyes</h5>
<p class="blog-info">06.12.10 | <a class="comment-ref">1 Comments</a></p>
<h5>New Vision Examination Instruments Arrived!</h5>
<p class="blog-info">05.10.10 | <a class="comment-ref">13 Comments</a></p>
You should use the function query_posts() with the functions have_posts() and the_post(). Here is an example for the WordPress API:
//The Query
query_posts('posts_per_page=5');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
That will loop through all posts you have queried. So you can just insert your query from the get_posts() function into the query_posts() function.
EDIT: I think if you want to stick with the get_posts() function, you have to call the setup_postdata() function to get the new post (source code for the API):
<ul>
<?php
global $post;
$myposts = get_posts('numberposts=5&offset=1&category=1');
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
But I would recommend to take the query_posts() function instead.
<?php
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
$args = array(
// 'cat' => 3, // Only source posts from a specific category
'posts_per_page' => 6 // Specify how many posts you'd like to display
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
while ( $latest_posts->have_posts() ) {
$latest_posts->the_post(); ?>
<article class="vertical-item content-padding ls">
<div class="item-media">
<img src="<?php the_post_thumbnail() ?>" alt="<?php the_title(); ?>">
</div>
<div class="item-content">
<h4 class="entry-title">
<?php the_title(); ?>
</h4>
<div>
<div class="media-body media-middle greylinks">
<br><a class="small-text" href="#"><?php the_time('l jS F, Y') ?></a>
</div>
</div>
</div>
<div class="item-footer">
<a class="lato lightgrey weight-black" href="<?php the_permalink(); ?>">Read this Article</a>
</div>
</article>
<? }
} else {
echo '<p>There are no posts available</p>';
}
wp_reset_postdata();
?>

Resources