Hide row if ACF sub field is checked - php

I got a ACF Reaper field with a couple of rows I am trying to show. However, I only want the show row if it has a checkbox checked (Checkbox is a subfield within the repeater). I am trying to accomplish that by using if in_array as described in ACF documentation under "Conditional logic":
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) )
I am outputting the result in a WordPress shortcode. For now, my code kinda works, except it shows all results within the repeater field (also those that are unchecked). What am I missing ??
My code:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo get_field( 'upload_dokument' );
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );

Managed to solve issue and with the help from #maggiathor's answer. For some reason echo was causing issue. I had to use return insted:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
$content = '<ul class="dokumenter">';
foreach( $rows as $row ) {
if( !in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$pdf = $row['upload_dokument'];
$content = $content . '<li>' . $pdf . '</li>';
}
}
}
$content = $content . '</ul>';
return $content;
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );

You cannot use get_sub_field() within the foreach loop, either you need to use a have_rows-while-loop or access it from the associative array:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo $row['upload_dokument'];
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );

Related

Wordpress ACF Get Block Field

I'm running into an issue with ACF, and I just can't figure out what's going on, and nothing on the internet is helping out.
I've added some fields to the Image Slider block:
But no matter what I try inside of our custom block code: image-slider.php I cannot get the values of any of the auto_play fields. get_field always returns null. I know the value is there, because if I dump out get_fields( $postID ), I can see the ['page_builder'][2] element has the value I want. I could get to it that way, but I can't seem to determine which index I'm on (the 2) programmatically.
So if you know either, how I can access the field directly, or figure out my current 'page_builder' index, that would be extremely helpful.
It's super confusing, because the have_rows( 'slide_setting' ) call obviously knows where to look, and works as expected.
The custom block php looks like:
<?php
if(have_rows( 'slide_setting' ) ) {
$digits = 3;
$randID = rand(pow(10, $digits-1), pow(10, $digits)-1);
echo '<div class="container"><div class="row"><div id="swiper_'.$randID.'" class="col-md-12 wiche-swiper-top-navigation-wrapper">';
echo '<div class="swiper-container wiche-swiper-top-navigation">';
// var_dump( get_fields( get_the_ID() )['page_builder'][2] );
// var_dump( get_post_field( 'auto_play' ) );
// var_dump(get_field('image_slider_settings_auto_play'));
// var_dump(get_row_index());
// var_dump(get_field_objects( $post->ID ));
// var_dump( get_row_index() );
// var_dump( acf_get_field_group( 'slide_setting' ) );
// die();
if ( get_field( 'auto_play' ) ) {
echo '<div class="swiper-wrapper" data-swiper-autoplay="' . get_field( 'auto_play_delay' ) . '" data-swiper-disable-on-interaction="' . get_field( 'auto_play_disable_on_interaction' ) . '">';
} else {
echo '<div class="swiper-wrapper">';
}
while( have_rows( 'slide_setting' ) ) {
the_row();
$title = get_sub_field( 'title' );
$image = get_sub_field( 'image' );
$content = get_sub_field( 'content' );
if ( $image || $content ) {
echo '<div class="swiper-slide swiper-banner-slide swiper-no-swiping">';
if ( $title ) {
echo '<div class="text-center slider-top-title">';
echo $title;
echo '</div>';
}
if ( $image ) {
echo '<div class="banner-image">';
echo wp_get_attachment_image( $image, 'full', '', array( 'loading' => false ) );
echo '</div>';
}
if ( $content ) {
echo '<div class="banner-content">';
echo $content;
echo '</div>';
}
echo '</div>';
}
}
echo '</div>';
echo '</div>';
echo '<div class="swiper-button-next swiper-button-next-outsite">Next</div><div class="swiper-button-prev swiper-button-prev-outsite">Prev</div>';
echo '</div></div></div>';
}
So I wasn't able to get a perfect answer to my question, looks like the API to get what I want doesn't exist (dumb).
What I ended up with - I set up a new function in my theme's functions.php file that looks like the following:
$post_slider_config_index = 0;
function get_the_slider_config( $post_id ) {
global $post_slider_config_index;
$page_builder = get_fields( $post_id )['page_builder'];
$slider_config = null;
foreach ($page_builder as $key => $value) {
if ( $value['acf_fc_layout'] === 'image_slider_settings' ) {
if ( $key > $post_slider_config_index ) {
$slider_config = $value;
$post_slider_config_index = $key;
break;
}
}
}
return $slider_config;
}
And then inside my image-slider.php file I call it like so:
$slider_config = get_the_slider_config( get_the_ID() );
if ( $slider_config[ 'auto_play' ] ) {
echo '<div class="swiper-wrapper" data-swiper-autoplay="' . $slider_config[ 'auto_play_delay' ] . '" data-swiper-disable-on-interaction="' . $slider_config[ 'auto_play_disable_on_interaction' ] . '">';
} else {
echo '<div class="swiper-wrapper">';
}
The $post_slider_config_index variable keeps track of the last index retrieved so that if there are multiple sliders on a page, it'll grab the right one as its rendered.
It's not perfect, it's not super efficient, but it does what I needed. Annoying WP doesn't just give you the information it obviously has already regarding where you are in the page.

How to display meta_value only once if the value is the same

I have created a loop to display the meta value but wanted to show it only once if they are the same of value. I have tried using array_unique but it doesn't seem to work
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
$menusInList = [];
while ( $_query->have_posts() ) {
$query->the_post();
$menu = get_post_meta($post->ID, 'awarded', true);
if (in_array($menu, $menusInList)) {
continue;
}
$menusInList[] = $menu;
echo '<li class="'.$menus .'" >' . $menu . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Save $menu in an array $menusInList and check via in_array. if it returns true use continueto skip.
$menusInList = [];
while ( $query->have_posts() ) {
$query->the_post();
$menu = get_post_meta($post->ID, 'award', true);
if (in_array($menu, $menusInList)) {
continue;
}
$menusInList[] = $menu;
// ...
}

Pinterest Wordpress Plugin Display Image

I am currently trying to create a plugin using Pinterest for Wordpress. I have ran into a slight problem, I've been trying to return an image with a link but only been able to only return a link.
<?php
function displayPinterest()
{
$request = wp_remote_get( 'https://api.pinterest.com/v1/boards/mercurizen/shoeswatches/pins/?access_token=AXgIMQmjCFDXyJsHni0wYUdZxshcFETISS1nfSRDAz1G7WBDJgAAAAA&fields=image,note,link&limit=5' );
$pins = json_decode( $request['body'], true );
if( !empty( $pins['data'] ) ) {
echo '<ul>';
foreach( $pins['data'] as $pin ) {
echo '<li>' . $pin['note']. '</li>';
}
echo '</ul>';
}
}
?>
Anyone experienced with Wordpress Plugins and Pinterest can help me with this problem ?
Try to change your code as below. You will get text in $pin['note'] and image url in $img['url'].
function displayPinterest()
{
$request = wp_remote_get( 'https://api.pinterest.com/v1/boards/mercurizen/shoeswatches/pins/?access_token=AXgIMQmjCFDXyJsHni0wYUdZxshcFETISS1nfSRDAz1G7WBDJgAAAAA&fields=image,note,link&limit=5' );
$pins = json_decode( $request, true );
if( !empty( $pins['data'] ) ) {
echo '<ul>';
foreach( $pins['data'] as $pin ) {
$img = $pin['image']['original'];
echo '<li>' . $pin['note']. '</li>';
}
echo '</ul>';
}
}

WordPress get_terms not outputting URL

I've got the following PHP code that I'm using to output a list of all custom taxonomy values, then group them into alphabetical order by first letter. This is working fine, except that the URL isn't being outputted. Anyone able to help?
<?php
$list = '';
$tags = get_terms( 'film-categories' );
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;
}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= '<div class="cat-group"><h3>' . apply_filters( 'the_title', $letter ) . '</h3>';
$list .= '<ul>';
foreach( $tags as $tag ) {
$url = esc_attr( get_tag_link( $tag->term_id ) );
$name = apply_filters( 'the_title', $tag->name );
$list .= '<li>' . $name . '</li>';
}
$list .= '</ul></div>';
}
}
} else $list .= '<p>Sorry, but no tags were found</p>';
echo $list;
?>
I'm afraid you've confused.
According to your second line - you're fetching terms of custom tax and not tags.
$tags = get_terms( 'film-categories' );
Therefore , any function related to tags won't work correctly.
In order to get the url of the term use the get_term_link() function.
Just replace the current line with:
$url = esc_attr( get_term_link( $tag ) );
Should work.

Sort Wordpress foreach results by custom field

I've been stuck on this issue all day, and I just keep getting nowhere with it, hoping someone can help!
I'm building a site for a restaurant that has multiple locations and need to list out each drink that exists at each specific location. These drinks need to then be sorted by category (brown, white, tomato, beer, wine). I feel like I'm extremely close to a solution, but I've been banging my head for the last while.
Here's my code:
$drinks = get_posts('post_type=drinks&numberposts=-1');
$show_brown_title = false;
$show_white_title = false;
$show_tomato_title = false;
$show_wine_title = false;
$show_beer_title = false;
if ( $drinks ) {
foreach( $drinks as $drink ) {
$id = $drink->ID;
$drink_location = $drink->drink_location;
if($drink->drink_category == 'Brown' && $drink_location && in_array($site_slug, $drink_location)) {
if($show_brown_title == false) {
echo '<h4><span>Brown</span> Cocktails</h4>';
echo '<ul>';
$show_brown_title = true;
}
echo '<li>';
echo '<span class="drink_title">'.$drink->post_title.'</span>';
echo '<span class="drink_ingredients">'.$drink->drink_ingredients.'</span>';
echo '<span class="drink_price">'.$drink->price_oz.'</span>';
echo '</li>';
}
if($drink->drink_category == 'White' && $drink_location && in_array($site_slug, $drink_location)) {
if($show_white_title == false) {
echo '<h4><span>White</span> Cocktails</h4>';
echo '<ul>';
$show_white_title = true;
}
echo '<li>';
echo '<span class="drink_title">'.$drink->post_title.'</span>';
echo '<span class="drink_ingredients">'.$drink->drink_ingredients.'</span>';
echo '<span class="drink_price">'.$drink->price_oz.'</span>';
echo '</li>';
}
}
}
For the most part, this works. However, I'm running into 2 issues.
I can't figure out how to close the unordered list after I'm done with each category.
This groups by category for the most part, however, if I have a drink that comes later, it will not actually put it into the right category, it'll just go into whatever category is at the bottom. I'm not sure if this is because I'm not closing the unordered list, or if because of the order that the posts are being pulled from WP.
Let me know if I'm explaining this alright, and I really appreciate any help you guys can offer!
Z
Instead of get_posts, you should use WP_Query. Info: https://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts/50762#50762
$show_brown_title = false;
$show_white_title = false;
$show_tomato_title = false;
$show_wine_title = false;
$show_beer_title = false;
$args = array(
'post_type' => 'drinks',
'numberposts' => '-1'
);
$query = new WP_Query( $args );
if( $query->have_posts() ):
while( $query->have_posts() ): $query->the_post();
echo '<pre>';
print_r( $query );
echo '<pre>';
endwhile;
endif;
wp_reset_postdata();
Inside the loop you will construct again your listing, use the print_r step by step, use your objects elements smart.
Aproach #2:
$show_brown_title = false;
$show_white_title = false;
$show_tomato_title = false;
$show_wine_title = false;
$show_beer_title = false;
foreach((get_the_category()) as $cat) {
$args = array(
'post_type' => 'drinks',
'numberposts' => '-1',
'cat' => $cat->cat_ID
);
$query = new WP_Query( $args );
if( $query->have_posts() ):
echo '<ul>';
while( $query->have_posts() ): $query->the_post();
echo '<h4><span>'. $query->category_name .'</span> Cocktails</h4>';
echo '<li>';
echo '<span class="drink_title">'.$query->post_title.'</span>';
echo '<span class="drink_ingredients">'.$query->drink_ingredients.'</span>';
echo '<span class="drink_price">'.$query->price_oz.'</span>';
echo '</li>';
endwhile;
echo '</ul>';
endif;
wp_reset_postdata();
}
With the specification that $query->category_name maybe should be written other way. Use print_r to see the correct field name/element of your object.
Bellow is my solution.
Note: get_posts uses the orderby parameter to order the posts by drink_category.
The code is simplified to only start <ul> when drink_category changes and to end </ul> on category change too. Code also checks that html to output has been assigned a value and appends to it a final </ul> in case one is not appended already.
$drinks = get_posts('post_type=drinks&numberposts=-1&orderby=drink_category');
$current_category = '';
$html = '';
if ( $drinks ) {
foreach( $drinks as $drink ) {
$id = $drink->ID;
$drink_location = $drink->drink_location;
if ($current_category != $drink->drink_category) {
if ($current_category != '') {
$html .= '</ul>';
}
$current_category = $drink->drink_category;
$html .= '<h4><span>' . $current_category . '</span> Cocktails</h4>';
$html .= '<ul>';
}
if($drink_location && in_array($site_slug, $drink_location)) {
$html .= '<li>';
$html .= '<span class="drink_title">'.$drink->post_title.'</span>';
$html .= '<span class="drink_ingredients">'.$drink->drink_ingredients.'</span>';
$html .= '<span class="drink_price">'.$drink->price_oz.'</span>';
$html .= '</li>';
}
}
}
if (strlen($html) > 0 && !endsWith($html, '</ul>')) {
$html .= '</ul>';
}
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}

Categories