I have a ACF repeater. How can I hide the div with class "brand" if the sub field in the repeater is empty?
<?php if( have_rows('partners', 'option') ): ?>
<div class="brand">
<div class="container">
<?php
while ( have_rows('partners', 'option') ) : the_row(); ?>
<div class="single-brand">
<?php
$partner_logo = get_sub_field('partner_logo', 'option');
if( !empty($partner_logo) ): ?>
<img src="<?php echo $partner_logo['url']; ?>" alt="<?php echo $partner_logo['alt']; ?>" />
<?php endif; ?>
</div>
<?php
endwhile;
?>
</div>
</div>
<?php endif; ?>
I think this will solve your issue.
<?php if( have_rows('partners', 'option') ): ?>
<?php
$flag = 0;
while ( have_rows('partners', 'option') ) : the_row();
$partner_logo = get_sub_field('partner_logo', 'option');
if($partner_logo){
$flag = 1;
}
endwhile;
endif;
?>
<?php if($flag == 1):?>
<div class="brand">
<div class="container">
<?php while ( have_rows('partners', 'option') ) : the_row(); ?>
<div class="single-brand">
<?php
$partner_logo = get_sub_field('partner_logo', 'option'); ?>
<img src="<?php echo $partner_logo['url']; ?>" alt="<?php echo $partner_logo['alt']; ?>" />
</div>
<?php
endwhile;
?>
</div>
</div>
<?php endif; ?>
Related
I have this code to display custom ACF fields on a page. When the subfields are empty, i would like to hide the div containers (class= price-container and ikon-container) completely, as it takes space in the column for another div container which is supposed to fill out the space instead.
How to solve this?
Thanks
<?php if ( have_rows( 'menuer_layout') ) : ?>
<div class="col-sm-12 col-md-12 col-lg-4" style="padding:0">
<div class="sticky-top">
<div class="price-container">
<div id="pricebox-content" class="slick-slide-pricebox">
<div class="pricebox-container">
<div class="pricebox-top">
<h4 class="pricebox-header">One</br>Two</h3>
<span class="price-subtitle">Here goes..</span>
</div>
<?php while ( have_rows( 'menuer_layout' ) ) : the_row(); ?>
<div class="menucard-container">
<h5 style="margin:0"><?php the_sub_field( 'menu_overskrift' ); ?></h5>
<span><?php the_sub_field( 'menu_tekst' ); ?></span>
<?php $menu_knap_url = get_sub_field( 'menu_knap_url' ); ?>
<?php if ( $menu_knap_url ) { ?>
<div class="pricebox-link-container" style="padding-top: 5px">
<a class="knap-2" href="<?php echo $menu_knap_url['url']; ?>" target="<?php echo $menu_knap_url['target']; ?>"><?php the_sub_field( 'menu_knap_tekst' ); ?></a>
</div>
<?php } ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php // no rows found ?>
<?php endif; ?>
</div>
</div>
</div>
<?php $hotel_ikoner_images = get_field( 'hotel_ikoner', 'option' ); ?>
<?php if ( $hotel_ikoner_images ) : ?>
<div class="ikon-container">
<ul class="ikon-liste">
<?php foreach ( $hotel_ikoner_images as $hotel_ikoner_image ): ?>
<li class="hotel-ikon-spec">
<img class="hotel-ikon-small" src="<?php echo esc_url( $hotel_ikoner_image['sizes']['thumbnail'] ); ?>" alt="<?php echo esc_attr( $hotel_ikoner_image['alt'] ); ?>" /> <span class="hotel-ikon-text"><?php echo esc_html( $hotel_ikoner_image['caption'] ); ?></span>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</section>
You Can Use get_sub_field
https://www.advancedcustomfields.com/resources/get_sub_field/
<?php if(!get_sub_field('field_name')){ ?>
<div>
Hide This Content If Field Empty
</div>
<?php } ?>
I have a standard wordpress blog here: http://webserver-meetandengage-com.m11e.net/insights/ and I've created a new category called clients.
The clients posts on this archive page will have different meta data that the standard blog post, so I want to get rid of the excerpt, date and author etc.
To achieve this I tried adding a conditional bit of code that said, IF the category of this post area is 'client' then echo style="display:none;" inside the div.
Here's the line of code I'm trying:
<p<?php if ( in_category( 'client' )) { echo 'style="display:none;"' }?>>This is not client</p>
Here's the loop it appears in:
<div class="container blog-card-container">
<div class="row">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="col-md-4">
<a href="<?php the_permalink(); ?>">
<div class="card">
<div class="blog-thumb-container">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
</div>
<div class="blog-clients-card-block">
<?php if( get_field('quote_name') ): ?><p class="client-name" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_name'); ?></p><?php endif; ?>
<p<?php if ( in_category( 'client' )) { echo 'style="display:none;"' }?>>This is not client</p>
<p class="blog-cat-label"><?php the_category(', '); ?></p>
<h2 class="blog-card-title"><?php the_title(); ?></h2>
<p class="card-text"><?php the_excerpt(__('(more…)')); ?></p>
<p><strong><?php the_author(); ?></strong> | <?php the_date(); ?> </p>
</div>
</div>
</a>
</div>
<?php understrap_pagination(); ?>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
</div>
But including it here breaks the loop and the page doesn't load... I'm not sure what I'm doing wrong, or even if there might be a better solution?
I essentially want to show one set of meta for post thumbnails with the category 'client' and then another set for all other categories in the blog.
I guess it could be IF category of container is client then show META1 else show META2.
Any help would be massively appreciated :)
Managed to achieve this with two IF ELSE statements:
<div class="container blog-card-container">
<div class="card-columns">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>">
<div class="card">
<!-- Image if loop =========================================== -->
<?php if ( in_category('14') ) : ?>
<div class="client-header-logo-card" style="background-color: <?php the_field('client_brand_colour'); ?>;">
<?php
$image = get_field('client_logo');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
<?php else: ?>
<div class="blog-thumb-container">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
</div>
<?php endif ?>
<!-- Meta Data if loop =========================================== -->
<div class="blog-clients-card-block">
<?php if ( in_category('14') ) : ?>
<p class="blog-cat-label"><?php the_category(', '); ?></p>
<h2><?php the_title(); ?></h2>
<?php if( get_field('quote') ): ?><p class="client-quote"><?php echo custom_field_excerpt(); ?></p><?php endif; ?>
<?php if( get_field('quote_name') ): ?><p class="client-name" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_name'); ?></p><?php endif; ?>
<?php if( get_field('quote_position') ): ?><p class="client-position" style="color:<?php the_field('client_brand_colour'); ?>;"><?php the_field('quote_position'); ?></p><?php endif; ?>
<?php if( get_field('button_text') ): ?>
<a class="btn btn-sm btn-client-archive" href="<?php the_permalink(); ?>" style="background-color:<?php the_field('client_brand_colour'); ?>;" role="button"><?php the_field('button_text'); ?></a>
<?php endif; ?>
<?php if( get_field('video_url') ): ?>
<div class="embed-container">
<?php the_field('video_url'); ?>
</div>
<?php endif; ?>
<?php else: ?>
<p class="blog-cat-label"><?php the_category(', '); ?></p>
<h2 class="blog-card-title"><?php the_title(); ?></h2>
<p class="card-text"><?php the_excerpt(__('(more…)')); ?></p>
<p><strong><?php the_author(); ?></strong> | <?php the_date(); ?> </p>
<?php endif ?>
</div>
</a>
</div>
You need to set it up in a Variable and echo the variable. For instance if client display:none is stored in the $style var if not then it doesn't place a style
<?php
if(in_category('client')){$style = 'display:none;';} else {$style = '';}
?>
<p style="<?php echo $style; ?>">This is not client</p>
<?php if( have_rows('team_members') ): ?>
<?php while( have_rows('team_members') ): the_row(); ?>
<div class="team-member-section">
<div class="container">
<?php if( get_row_layout() == 'team_members' ): ?>
<section class="team-member-section">
<?php
$team_title = get_sub_field('team_title');
$team_member = get_sub_field('team_member');
$description = get_sub_field('description');
?>
<div class="team-title">
<?php echo $team_title; ?>
</div>
<?php if( $team_member ): ?>
<?php if ( get_field('has_description') == 'yes') : ?>
<?php echo 'has_description'; ?>
<?php else : ?>
<?php echo 'has_no_description'; ?>
<?php endif; ?>
<?php endif; ?>
</section>
<?php endif; ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
It displays only the value "has_no_description", why is my true/false field not working and how to make it work so both situations will be displayed.
UPDATE:
<?php if( have_rows('team_members') ): ?>
<?php while( have_rows('team_members') ): the_row(); ?>
<div class="team-member-section">
<div class="container">
<?php if( get_row_layout() == 'team_members' ): ?>
<section class="team-member-section">
<?php
$team_title = get_sub_field('team_title');
$team_member = get_sub_field('team_member');
$description = get_sub_field('description');
?>
<div class="team-title">
<?php echo $team_title; ?>
</div>
<?php if( $team_member ): ?>
<?php if ( get_field('has_description') ) : ?>
<?php foreach( $team_member as $post): ?>
<?php setup_postdata($post); ?>
<div class="col-sm-4">
<div class="team-member">
<div class="member-img">
<?php the_post_thumbnail(); ?>
</div>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php echo "Has description"; ?>
<?php $overlay_text = get_field('overlay_text'); ?>
<?php if($overlay_text != ''): ?>
<div class="overlay-text">
<p><?php echo $overlay_text; ?></p>
</div>
<?php endif; ?>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php endforeach; ?>
<?php else : ?>
<?php foreach( $team_member as $post): ?>
<?php setup_postdata($post); ?>
<div class="col-sm-4">
<div class="team-member">
<div class="member-img">
<?php the_post_thumbnail(); ?>
</div>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php echo "Doesn not have description"; ?>
<?php $overlay_text = get_field('overlay_text'); ?>
<?php if($overlay_text != ''): ?>
<div class="overlay-text">
<p><?php echo $overlay_text; ?></p>
</div>
<?php endif; ?>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endif; ?>
</section>
<?php endif; ?>
</div>
</div>
<?php endwhile; ?>
I posted my entire code.
I think the issue here is that the field 'has_description' is showed only on "Team" Custom Post Type and that's why the field is returning null, but I am not sure. Can someone make it work?
<?php if( have_rows('team_members') ): ?>
<?php while( have_rows('team_members') ): the_row(); ?>
<div class="team-member-section">
<div class="container">
<?php if( get_row_layout() == 'team_members' ): ?>
<section class="team-member-section">
<?php
$team_title = get_sub_field('team_title');
$team_member = get_sub_field('team_member');
$description = get_sub_field('description');
?>
<div class="team-title">
<?php echo $team_title; ?>
</div>
<?php if( $team_member ): ?>
<?php foreach( $team_member as $post): ?>
<?php if ( get_field('has_description') ) : ?>
<?php setup_postdata($post); ?>
<div class="col-sm-4">
<div class="team-member">
<div class="member-img">
<?php the_post_thumbnail(); ?>
</div>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php echo "Has description"; ?>
<?php $overlay_text = get_field('overlay_text'); ?>
<?php if($overlay_text != ''): ?>
<div class="overlay-text">
<p><?php echo $overlay_text; ?></p>
</div>
<?php endif; ?>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<div class="col-sm-4">
<div class="team-member">
<div class="member-img">
<?php the_post_thumbnail(); ?>
</div>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php echo "NOOOOOOO Description"; ?>
<?php $overlay_text = get_field('overlay_text'); ?>
<?php if($overlay_text != ''): ?>
<div class="overlay-text">
<p><?php echo $overlay_text; ?></p>
</div>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
</section>
<?php endif; ?>
</div>
</div>
<?php endwhile; ?>
It works like this, I moved the true/false <?php if ( get_field('has_description') ) : ?> field inside the foreach since true/false field belongs to the team custom post type, being out of the loop will always make it NULL
I can work pretty good with CSS and HTML but know nothing about PHP. I'm working on a wordpress client project and he wants more than one Latest Post/Recent Post to display on the static homepage. I'm pulling my hair out trying to figure this out. From the posted code, can someone show me what to delete and what to replace it with to make 5 of the most latest post appear? I really appreciate everyone's help.
<?php
/**
* Template Name: Homepage
*/
?>
<?php $mts_options = get_option(MTS_THEME_NAME); ?>
<?php get_header(); ?>
<?php if ($mts_options['mts_banner_show'] == '1') { ?>
<div class="b_first">
<div class="main-container">
<div class="container">
<div class="blog_first">
<!--first content-->
<div id="first_b">
<div class="b_right" <?php if ( isset( $_GET['mailchimp_signup'] ) || !empty( $_GET['aweber_signedup'] ) ) echo 'style="display:none;"'; ?>>
<h2 class="front-view-title">
<?php echo $mts_options['mts_banner_title']; ?>
</h2>
<div class="front-view-content">
<?php echo $mts_options['mts_banner_texts']; ?>
</div>
<?php if(!empty($mts_options['mts_button_text'])) { ?>
<div class="readMore" style="background:<?php echo $mts_options['mts_banner_button_bg']; ?>">
<?php echo $mts_options['mts_button_text']; ?>
<?php if(!empty($mts_options['mts_arrow_image'])) { ?>
<div class="b_dollor">
<img src="<?php echo $mts_options['mts_arrow_image']; ?>">
</div>
<?php } ?>
</div>
<?php } ?>
</div>
<!--Rightside Content-Option-1-->
</div>
<div id="second_b" <?php if ( !isset( $_GET['mailchimp_signup'] ) && empty( $_GET['aweber_signedup'] ) ) echo 'style="display:none;"'; ?>>
<div class="blog_first_alternative">
<h2 class="front-view-title">
<?php echo $mts_options['mts_banner_title']; ?>
</h2>
<div class="form_wrap">
<?php if(!empty($mts_options['mts_form_image'])) { ?>
<div class="form_wrap_left">
<img src="<?php echo $mts_options['mts_form_image']; ?>">
</div>
<?php } ?>
<div class="form_wrap_right">
<?php dynamic_sidebar('Home Subscribe Widget'); ?>
</div>
</div>
</div>
<!--Rightside content alternative option-->
<script type="text/javascript">
function hide_b() {
jQuery('#first_b').hide();
jQuery('#second_b').show();
}
</script>
</div>
</div>
<!--End of first content-->
</div>
</div>
</div>
<?php } ?>
<div class="main-container">
<div id="page">
<div class="artcl article">
<div id="content_box">
<?php if ($mts_options['mts_banner2_show'] == '1') { ?>
<!--Second Content-->
<div class="blog_second">
<div class="b_left">
<h2 class="front-view-title">
<?php echo $mts_options['mts_social_title']; ?>
</h2>
<?php if ( !empty($mts_options['mts_banner_social']) && is_array($mts_options['mts_banner_social'])) { ?>
<div class="social-icons">
<ul>
<?php foreach( $mts_options['mts_banner_social'] as $header_icons ) : ?>
<?php if( ! empty( $header_icons['mts_banner_icon'] ) && isset( $header_icons['mts_banner_icon'] ) ) : ?>
<li><span class="fa fa-<?php print $header_icons['mts_banner_icon'] ?>"></span></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
<?php } ?>
</div>
<div class="b_right">
<h2 class="front-view-title">
<?php echo $mts_options['mts_books_title']; ?>
</h2>
<div class="b_readings">
<ul>
<?php if(!empty($mts_options['mts_books_image'])){ ?>
<?php foreach( $mts_options['mts_books_image'] as $slide ) : ?>
<li> <?php echo wp_get_attachment_image( $slide['mts_book_image'], false, array('title' =>'') ); ?></li>
<?php endforeach; ?>
<li class="more-books"><?php echo $mts_options['mts_more_book_text']; ?><i class="fa fa-angle-double-right"></i></li>
<?php } ?>
</ul>
</div>
</div>
</div>
<?php } ?>
<?php if(!empty($mts_options['mts_featured_posts']) && !empty($mts_options['mts_featured_post_cat'])) { ?>
<div class="home_article">
<?php
$featured_cat = implode( ",", $mts_options['mts_featured_post_cat'] );
$featured_query = new WP_Query('cat='.$featured_cat.'&posts_per_page=5');
if ($featured_query->have_posts()) : while ( $featured_query->have_posts() ) : $featured_query->the_post(); ?>
<article class="latestPost featuredpost excerpt">
<!--Featured Post-->
<header>
<h3 class="title front-view-title"><?php echo get_the_category_by_ID($featured_cat); ?></h3>
<?php if(has_post_thumbnail()) { ?>
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" id="featured-thumbnail">
<div class="featured-thumbnail">
<?php the_post_thumbnail('steadyincome-featured',array('title' => '')); ?> <?php if (function_exists('wp_review_show_total')) wp_review_show_total(true, 'latestPost-review-wrapper'); ?>
</div>
</a>
<?php } ?>
</header>
<div class="latestpost_wrap">
<h2 class="front-view-title">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>
"><?php the_title(); ?></a>
</h2>
<?php mts_the_postinfo( 'home' ); ?>
<div class="front-view-content">
<?php echo mts_excerpt(45); ?>
</div>
<?php mts_readmore(); ?>
</div>
</article>
<?php endwhile; wp_reset_query(); endif; ?>
<?php $j = 0;
if (get_query_var('page') > 1) {
$paged = get_query_var('page');
} elseif (get_query_var('paged')) {
$paged = get_query_var('paged');
} else {
$paged = 1;
}
$args= array('paged' => $paged, 'post_type' => 'post');
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if($j ==0){ ?>
<article class="latestPost latestpost excerpt">
<!--Latest Post-->
<header>
<h3 class="title front-view-title"><?php _e('Latest Post','steadyincome'); ?></h3>
<a href="<?php the_permalink() ?>" title="Menu widget article" id="featured-thumbnail">
<div class="featured-thumbnail">
<?php the_post_thumbnail('steadyincome-featured',array('title' => '')); ?>
<?php if (function_exists('wp_review_show_total')) wp_review_show_total(true, 'latestPost-review-wrapper'); ?>
</div>
</a>
</header>
<div class="latestpost_wrap">
<h2 class="front-view-title">
<?php the_title(); ?>
</h2>
<?php mts_the_postinfo( 'home' ); ?>
<div class="front-view-content">
<?php echo mts_excerpt(40); ?>
</div>
<?php mts_readmore(); ?>
</div>
</article>
<?php } ?>
<?php $j++; endwhile; wp_reset_query(); endif; ?>
</div>
<?php } ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
Looks like you might have some setting in your theme settings. However if you have no setting there.
You can see this line that is limiting latest posts to just one Post:
<?php if($j ==0){ ?>
You may try to change it to number you want to try and see if it loops more posts there:
<?php if($j < 5){ ?>
This should allow the loop to run for five posts now. Give it a try and see if it helps.
I've made a home page with four different categories to be shown.I think it was working well, but for now the all posts have the same content as the first post. Links, and featured images, are good, but text somehow is being overwritte.
Heress the screen all texts are the same:
http://imagizer.imageshack.us/v2/800x600q90/713/m1j6.jpg
Edited: so this works with <?php setup_postdata( $post ); ?> inside loops
My code:
<div class="bmw">
<h2>bmw news</h2>
<?php $k = 1;
$posts = get_posts('category=7&orderby=date&numberposts=2'); foreach($posts as $post) { ?>
<?php setup_postdata( $post ); ?>
<div id="home_post<?php if($k%2 == 0) echo "last" ;?>">
<?php if ( has_post_thumbnail() ) {?>
<a href="<?php the_permalink() ?>"><div id="img">
<span>
<?php the_post_thumbnail(); ?>
</span>
</div>
</a>
<?php }; ?>
<h3><?php the_title(); ?></h3>
<span id="tags"><?php the_tags(); ?></span>
<?php the_content('...'); ?>
<div class="button_more">Czytaj więcej<span><img style="vertical-align:middle;width:auto;margin-left:5%" src="http://test.startujac.pl/images/strzalka_czytaj_wiecej.png"></span></div>
</div>
<?php $k++; ?>
<?php } ?>
</div>
<div class="news">
<h2>VW news</h2>
<?php $v = 1;
$posts = get_posts('category=12&orderby=date&numberposts=2'); foreach($posts as $post) { ?>
<?php setup_postdata( $post ); ?>
<div id="home_post<?php if($v%2 == 0) echo "last" ;?>">
<?php if ( has_post_thumbnail() ) {?>
<a href="<?php the_permalink() ?>"><div id="img">
<span>
<?php the_post_thumbnail(); ?>
</span>
</div></a>
<?php } ?>
<h3><?php the_title(); ?></h3>
<span id="tags"><?php the_tags(); ?></span>
<?php the_content('...'); ?>
<div class="button_more">Czytaj więcej<span><img style="vertical-align:middle;width:auto;margin-left:5%" src="http://test.startujac.pl/images/strzalka_czytaj_wiecej.png"></span></div>
</div>
<?php $v++; ?>
<?php }?>
</div>
<div class="japan">
<h2>japan news</h2>
<?php $k = 1;
$posts = get_posts('category=13&orderby=date&numberposts=2'); foreach($posts as $post) { ?>
<?php setup_postdata( $post ); ?>
<div id="home_post<?php if($k%2 == 0) echo "last" ;?>">
<?php if ( has_post_thumbnail() ) {?>
<a href="<?php the_permalink() ?>"><div id="img">
<span>
<?php the_post_thumbnail(); ?>
</span>
</div></a>
<?php } ?>
<h3><?php the_title(); ?></h3>
<span id="tags"><?php the_tags(); ?></span>
<?php the_content('...'); ?>
<div class="button_more">Czytaj więcej<span><img style="vertical-align:middle;width:auto;margin-left:5%" src="http://test.startujac.pl/images/strzalka_czytaj_wiecej.png"></span></div>
</div>
<?php $k++; ?>
<?php } ?>
</div>
<div class="events">
<h2>imprezy i zloty</h2>
<?php $k = 1;
$posts = get_posts('category=1&orderby=date&numberposts=4'); foreach($posts as $post) { ?>
<?php setup_postdata( $post ); ?>
<div id="home_post<?php if($k == 1) echo "first" ;?>">
<?php if ( has_post_thumbnail() ) {?>
<div id="img">
<span>
<?php the_post_thumbnail(); ?>
</span>
</div>
<?php } ?>
<div id="content">
<h3><?php the_title(); ?></h3>
<span id="tags"><?php the_tags(); ?></span>
<?php the_content('...'); ?>
</div>
</div>
<?php $k++; ?>
<?php } ?>
</div>
Have you tried putting the_post(); on the beginning of your template file? This solves sometimes this kind of problems.