Advanced Custom Field True/False field not working - php

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

Related

Hide div container if ACF Repeater sub field is empty

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

How Do I Add More Latest Post To Homepage

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.

Echo a variable outside of while loop in PHP

I have a PHP while loop and I'm trying to echo the contents of $child_i outside of it, how can I do this?
<li>
<div class="responsive-accordion-head"><span class="ico arrow-right"></span><?php the_sub_field('category_name'); ?> <span class="faq-counter">4 Questions</span></div>
<?php if( have_rows('questions') ): $child_i = 0; ?>
<!-- Trying to echo $child_i on the line below -->
<div class="responsive-accordion-panel <?php echo($child_i); ?>">
<?php while( have_rows('questions') ): the_row(); ?>
<div class="question">
<h6><?php the_sub_field('question'); ?></h6>
<p><?php the_sub_field('answer'); ?></p>
</div>
<?php $child_i++; endwhile; ?>
</div>
<?php endif; //if( get_sub_field('items') ): ?>
</div>
</li>
can put the results into your own array then access it via its number
<li>
<div class="responsive-accordion-head"><span class="ico arrow-right"></span><?php the_sub_field('category_name'); ?> <span class="faq-counter">4 Questions</span></div>
<?php if( have_rows('questions') ): $child_i = 0; ?>
<!-- Trying to echo $child_i on the line below -->
<div class="responsive-accordion-panel <?php echo($child_i); ?>">
<?php while( have_rows('questions') ): the_row(); ?>
<div class="question">
<h6><?php the_sub_field('question'); ?></h6>
<p><?php the_sub_field('answer'); ?></p>
</div>
<?php $results[] = $child_i; $child_i++; endwhile; ?>
</div>
<?php endif;
print_r($results);
//OR foreach($results as $result){echo $result;}
//OR echo $result[1].$result[1]; //etc
//if( get_sub_field('items') ): ?>
</div>
</li>
The easiest way to do this is to move your class inside your while() loop and then use the counter
Example
<li>
<div class="responsive-accordion-head"><span class="ico arrow-right"></span><?php the_sub_field('category_name'); ?> <span class="faq-counter">4 Questions</span></div>
<?php if( have_rows('questions') ): $child_i = 0; ?>
<!-- Trying to echo $child_i on the line below -->
<?php while( have_rows('questions') ): the_row(); ?>
<div class="responsive-accordion-panel <?php echo ($child_i); ?>">
<div class="question">
<h6><?php the_sub_field('question'); ?></h6>
<p><?php the_sub_field('answer'); ?></p>
</div>
</div>
<?php $child_i++; endwhile; ?>
<?php endif; //if( get_sub_field('items') ): ?>
</div>
</li>
Try to insert
echo '<xmp>'; var_dump($child_i); echo '</xmp>';
instead of
echo($child_i);
($child_i can contain an empty string)

wordpress post content duplicate

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.

Adding different class to first div in loop

I have the following code:
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="item">
<?php the_post_thumbnail('full');?>
<div class="container">
<div class="carousel-caption">
<h1>
<?php the_title(); ?>
</h1>
<p>
<?php the_excerpt(); ?>
</p>
</div>
</div>
</div>
<?php endwhile; ?>
And I need to add the class "active" to the first div (next to "item")
Use a boolean variable to test, set it to true after first pass so that further loops will not mark it active
<?php $firstMarked = false; ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="item <?php echo !$firstMarked ? "active":"";?>">
<?php the_post_thumbnail('full');?>
<div class="container">
<div class="carousel-caption">
<h1>
<?php the_title(); ?>
</h1>
<p>
<?php the_excerpt(); ?>
</p>
</div>
</div>
</div>
<?php $firstMarked = true;?>
<?php endwhile; ?>
Add a flag:
<?php
$isFrist = true;
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="item">
<?php the_post_thumbnail('full');?>
<div class="container<?php if ($isFirst): ?> active<?php endif ?>">
<div class="carousel-caption">
<h1>
<?php the_title(); ?>
</h1>
<p>
<?php the_excerpt(); ?>
</p>
</div>
</div>
</div>
<?php
$isFrist = false;
endwhile;
?>
if($loop->current_post == 1){
echo 'class';
}
<script type="text/javascript">
$('.carousel-inner > :first-child').addClass("active");
</script>

Categories