ACF Repeatable Content Widget - WordPress - php

I've created a Field group called 'Team Members' and set the field type as repeatable with three sub fields; Image, Position and Name.
The rule for this group is to show within a custom widget I've created. When I drag the widget in the Appearance > Widgets area of WordPress I can see it is indeed there and can add members / images to my widget.
Where I've hit a brick wall is trying to output this data on the front end through my widget template.
This is my code:
<?php if( have_rows('team_members') ): ?>
<ul class="slides">
<?php while( have_rows('team_members') ): the_row();
// vars
$image = get_sub_field('image');
$content = get_sub_field('name');
$link = get_sub_field('position');
?>
<li class="slide">
<?php if( $link ): ?>
<a href="<?php echo $link; ?>">
<?php endif; ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<?php if( $link ): ?>
</a>
<?php endif; ?>
<?php echo $content; ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Where am I going wrong?

To get values from your custom widget via ACF the syntax is:
if ( have_rows( 'team_members', 'widget_' . $widget_id ) ) :
while have_rows( 'team_members', 'widget_' . $widget_id ) ) : the_row();
// get sub fields ...
endwhile;
endif;

Related

Broken thumbnails are displaying instead of product images in post object (field type) ACF

I am displaying product images on product page using ACF. Only permalink and title is displaying and product pictures are not displaying instead of that broken thumbnails are displaying. My field type and return format is post object and here is my code:
<div class="col-md-12">
<?php
$featured_posts = get_field('products_images');
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post);
$title = get_the_title( $featured_post);
$image = get_field('products_images');
?>
<li>
<?php
if ( !empty($image) ); ?>
<img src="<?php echo $image['url']; ?>" />
<?php echo esc_html( $title ); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>

Post Archive page - If else statement advanced custom fields

I am using Advanced Custom Fields and Facet WP.
Currently there is a directory listing showing on a page that shows a business preview by displaying a business title, image and an excerpt - the same as you typically see from a post archive page.
I have added a new ACF checkbox field 'Are you ready to publish' and would like to amend the php so the Title, excerpt an image will only show for user profile listings that have checked 'yes' to publish.
I am fairly new to php and ACF and cannot get it to work.
I have tried to variations:
<?php
global $post;
?>
<div class="business-directory-results">
<?php if( get_field('ready_to_publish') == 'yes' ) { ?>
<?php while ( have_posts() ): the_post(); ?>
<a class="business-directory-result box-shadow" href="/member-profile/<?php the_field('user_login');?>/">
<img src="<?php echo wp_get_attachment_image_src(get_post_meta( $post->ID, 'meta-business_featured_image', true ), "full")[0];?>" />
<div class="business-directory-result-content">
<h2><?php the_field('meta-business_name');?></h2>
<?php $excerpt = wp_trim_words( get_field('meta-business_profile_content' ), $num_words = 25, $more = '...' ); ?>
<p><?php echo $excerpt; ?></p>
</div>
</a>
<?php endwhile; ?>
<?php else { ?>
<!-- do nothing -->
<?php } ?>
</div>
and
<?php
global $post;
?>
<div class="business-directory-results">
<?php while ( have_posts() ): the_post(); ?>
<?php if( get_field('ready_to_publish') == 'yes' ) { ?>
<a class="business-directory-result box-shadow" href="/member-profile/<?php the_field('user_login');?>/">
<img src="<?php echo wp_get_attachment_image_src(get_post_meta( $post->ID, 'meta-business_featured_image', true ), "full")[0];?>" />
<div class="business-directory-result-content">
<h2><?php the_field('meta-business_name');?></h2>
<?php $excerpt = wp_trim_words( get_field('meta-business_profile_content' ), $num_words = 25, $more = '...' ); ?>
<p><?php echo $excerpt; ?></p>
</div>
</a>
<?php else { ?>
<!-- do nothing -->
<?php } ?>
<?php endwhile; ?>
</div>
If you create posts first and then add an additional ACF field, they will not work. To make it work, you will need to update all your posts after adding an ACF field. In your case, you will need update each user profile and only then they will have the ACF field attached with them.
Don't place the condition outside of the loop. That means your second variation is correct.
<?php
global $post;
?>
<div class="business-directory-results">
<?php while ( have_posts() ): the_post(); ?>
<?php if( get_field('ready_to_publish') == 'yes' ) { ?>
<a class="business-directory-result box-shadow" href="/member-profile/<?php the_field('user_login');?>/">
<img src="<?php echo wp_get_attachment_image_src(get_post_meta( $post->ID, 'meta-business_featured_image', true ), "full")[0];?>" />
<div class="business-directory-result-content">
<h2><?php the_field('meta-business_name');?></h2>
<?php $excerpt = wp_trim_words( get_field('meta-business_profile_content' ), $num_words = 25, $more = '...' ); ?>
<p><?php echo $excerpt; ?></p>
</div>
</a>
<?php else { ?>
<!-- do nothing -->
<?php } ?>
<?php endwhile; ?>
</div>

ACF repeater showing just 1 row

So when I try to use ACF repeater field instead of showing me all the fields I get just the first one. The code is as follows.
<?php if( have_rows('vsi_projekti') ): ?>
<ul class="posts-grid">
<?php while( have_rows('vsi_projekti') ): the_row();
// vars
$image = get_sub_field('vsi_projekti_image');
$content = get_sub_field('project_name');
$link = get_sub_field('link_to_post');
?>
<li class="post-grid">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<div class="post-title-hover"><?php echo $content; ?></div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Any advice on what I'm doing wrong to be getting out just 1 row instead of multiple?
I don't know if this has anything to do with my problem or not, but I'm adding just 1 row in every post. But in the end I should get out more then just row I think?
I think that you are confused what a ACF repeater field does. If you enter just one row in every post witrh a repeater, it's normal that you get only one row... because your code works perfectly fine ... for a repeater within a post... when you add 15 rowds in your post you will get all 15rows as output...
But if you want to output every row of every repeater of every post, yopur code doesn't work. You should try this instead:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$posts = get_posts($args);
if( $posts ): ?>
<ul class="posts-grid">
<?php foreach( $posts as $post ): setup_postdata( $post ); ?>
<?php if( have_rows('vsi_projekti') ): ?>
<?php while( have_rows('vsi_projekti') ): the_row();
// vars
$image = get_sub_field('vsi_projekti_image');
$content = get_sub_field('project_name');
$link = get_sub_field('link_to_post');
?>
<li class="post-grid">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<div class="post-title-hover"><?php echo $content; ?></div>
</a>
</li>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; //foreach( $posts as $post ) ?>
<?php wp_reset_postdata(); ?>
</ul>
<?php endif; // if( $posts ) ?>
This code gets all posts and loops throug them... In every loop the repeater field is put out....

ACF: Show a list of a subfield and show the corresponding subfield in another div?

Using ACF (Advanced Custom Fields), I set up a field (slides) with two subfields (title) and (slide). In my header.php file, I have this code which outputs a list of the title subfield.
// header.php //
<nav id="site-navigation" class="main-navigation" role="navigation">
<?php $frontpage_id = get_option('page_on_front'); ?>
<?php if ( is_singular() && have_rows('slides', $frontpage_id) ): ?>
<ul>
<?php while ( have_rows('slides', $frontpage_id) ) : the_row(); ?>
<li class="group"><?php the_sub_field('title'); ?></li>
<?php endwhile;?>
</ul>
<?php endif; ?>
</nav>
Then in my index.php file, I have div.content. In this div, I would like to output a list of the corresponding slide subfield. How can I do this?
// index.php //
<div class="content"></div>
You could repeat a similar loop in the index.php file -
<?php
$frontpage_id = get_option('page_on_front');
if( have_rows('slides', $frontpage_id) ):
while( have_rows('slides', $frontpage_id) ) : the_row();
// if your 'slide' field is an image
$slide = get_sub_field('slide'); ?>
<img src="<?php echo $slide['url']; ?>" alt="<?php echo $slide['alt']; ?>" />
<?php endwhile;
endif;
?>
Or you could grab the entire array of data associated with the repeater using get_field('slides', $frontpage_id); - set that as a global variable and then use a foreach loop in both files. I think the first option is easier.

Highlight (Active) menu post title in a category.php

This is the website i´m working on: http://canal.es/wordpress/
The problem I have is that, if you go, to "Pasteleria dulce" (which is a category.php file) it shows a left sidebar with the active title of the category, it shows a post on the center and it shows below the image a list of the post of the same category.
I haven´t find a solution to highlight the post title that opens automatically when I enter to the category. This is the code i´m using on the category.php file:
<!-- Highlight menu from current category -->
<?php
if (is_category()) {
$this_category = get_category($cat);
}
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id&title_li=&child_of='.$this_category->category_parent."&echo=0"); else
$this_category = wp_list_categories('orderby=id&title_li=&child_of='.$this_category->cat_ID."&echo=0");
if ($this_category) { ?>
<ul class="sub-menu">
<?php echo $this_category; ?>
</ul>
<?php } ?>
<div class="producto_column">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="producto_image">
<img src="<?php the_field('imagen_producto'); ?>" alt=""/>
</div>
<div class="share_item">
<a class="minimal" id="premium">
<img src="<?php bloginfo('template_directory') ?>/images/share.png" alt="share">
</a>
<a class="maillink" href="http://canal.local/?page_id=220">
<img src="<?php bloginfo('template_directory') ?>/images/email.png" alt="email">
</a>
</div>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<!-- Post list from current category -->
<ul id="submenu_productos" class="clearfix">
<?php
$IDOutsideLoop = $post->ID;
while( have_posts() ) {
the_post();
foreach( ( get_the_category() ) as $category )
$my_query = new WP_Query('category_name=' . $category->category_nicename . '&orderby=title&order=asc&showposts=100');
if( $my_query ) {
while ( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<li<?php print ( is_single() && $IDOutsideLoop == $post->ID ) ? ' class="test"' : ''; ?>>
<?php the_title(); ?> |
</li>
<?php
}
}
}
?>
</ul>
How can I make active the current post title that appears when I open the category page?
Try to use this solution:
<ul>
<?php
$lastposts = get_posts('numberposts=5&orderby=rand&cat=-52');
foreach($lastposts as $post) :
setup_postdata($post); ?>
<li<?php if ( $post->ID == $wp_query->post->ID ) { echo ' class="current"'; } else {} ?>>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>

Categories