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

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.

Related

Loop out ACF data from flexible content

I have a Flexible content group with 2 columns. 1 for title and one for anchor id.
See pic 1.
pic of flexible content
I’m trying to loop out the anchor links(id) and the title like this:
<?php if( have_rows('links') ): ?>
<?php
$links = get_sub_field('links');
foreach ($links as $link) :
$anchor = ($link['id']) ? 'href="#' . $link['id'] . '"' : "";
?>
<li class="side-menu__item">
<a class="menu__link" <?= $anchor ?>>
<?= $link['title']; ?>
</a>
</li>
<?php endforeach ?>
<?php endif; ?>
However nothing shows up so I'm guessing i'm looping it out the wrong way, or not looping it at all. Any ideas on what I'm doing wrong?

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

Skip custom Field if not exsist?

i have a code like this, to display a custom field on my Page...
<?php
global $wp_query;
$postid = $wp_query->post->ID;
if($immopress_property['assistedLiving']): ?>
<li class="assistedLiving">
<span class="attribute" style="width:200px;display:block;float:left;"><?php echo 'Seniorengerechtes Wohnen' ; ?><span class="wpp_colon">:</span></span>
<span class="value"><?php if (get_post_meta($post->ID, 'assistedLiving' , true)) echo "Ja"; else echo "Nein"; ?> </span>
</li>
<?php wp_reset_query(); ?>
<?php endif; ?>
But when the custom field NAME & VALUE not exist it shall skip to the next Value and Name. I try different ways to do that but no one works.
Hope someone can help me with that.
changed to this and it works...
if ( get_post_meta($post->ID, 'assistedLiving', true) ) { ?>

echo category in rss feed

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>

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>

Resources