Loop out ACF data from flexible content - wordpress

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?

Related

Why the first <li> with flexslider appears as blank <li> on my code?

I have a question about flexslider, I'm working with it on Wordpress theme, but I've a problem, why te first slide appears as blank slide , I hope anyone can help me with this, the PHP code works fine but my problem is with the list in slider, thanks!.
my code :
<div id="lookslider" class="flexslider">
<ul class="slides">
<li>
<div id="lookbox">
<?php $i = 0;
if (have_posts() ) : while ( have_posts() ) : the_post();
if ($i%4 == 0) echo '</li><li>'; ?>
<div id="lookslide-box">
<div class="lookimg">
<?php echo get_the_post_thumbnail( $page->ID, 'featured_image' ); ?>
</div>
<div class="lookhead">
<h3><?php the_title(); ?></h3>
Take a look
</div>
</div>
<?php $i++; endwhile; endif; ?>
</div>
</li>
</ul>
In your code, your increment $i starts with 0 and your if statement inside the loop says ($i%4 == 0). So, this loop will start with </li><li>.
Most of the browsers (if not all) will correct the html code adding a <li> before your </li>.
You probably just need to change your $i (to 1 for example) or just add another criteria in your if. here is a example:
$i = 1;
if (have_posts()) :
echo '<li>';
while (have_posts()) :
the_post();
if ($i%5 == 0) {
echo '</li><li>';
}
// echo stuff
$i++;
endwhile;
echo '</li>';
endif;
I hope i made myself clear, let me know if i misunderstood something.

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>

Check if Wordpress loop has posts

I have a custom loop with posts which are added from the custom meta field checkbox. Only if the checkbox is checked, then the post is being added to the loop. I have a container that holds that loop. What i want to do is to check if that loop got any posts and if it is empty - just hide that container. Because otherwise when the loop is empty the container is remaining on the page:
<div>
<ul>
</ul>
</div>
This is the loop:
<?php
/* Slider ------- */
$slider = new WP_Query('showposts=-1');
if ( $slider->have_posts() ):
?>
<div>
<ul>
<?php while ( $slider->have_posts() ) : $slider->the_post(); ?>
<?php if ( get_post_meta($post->ID, "mf_homeslider", true) == 'slider_on' ){ // Check if post was added to slider ?>
<li>
<?php if (has_post_thumbnail()) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('large'); ?>
</a>
<?php } ?>
<div>
<?php get_template_part('includes/post_meta'); ?>
<h2>
<?php the_title(); ?>
</h2>
</div>
</li>
<?php } ?>
<?php endwhile; ?>
</ul>
</div>
<?php
endif;
wp_reset_query();
?>
Thank you in advance for your help.
You should filter your query by the custom field (see details here), it could also improve performance (an SQL condition vs. a while-if loop). After that, have_posts() returns whether the loop contains any posts.
So instead of
$slider = new WP_Query('showposts=-1');
use
$slider = new WP_Query(array(
'meta_key' => 'mf_homeslider',
'meta_value' => 'slider_on',
'posts_per_page' => -1)
);
and there's no further need for if ( get_post_meta(...
(showposts is deprecated, use posts_per_page instead).
Note: you are mixing braces and if-endif style, the latter would be much readable when nesting.

Resources