I have this bit of code that doesn't render on page. Does anybody know what the issue is?
<div class="b b--alt">
<div class="container">
<div class="tab-widget tab-widget--team js-tab-widget">
<?php if( have_rows('the_commusoft_story') ) { ?>
<?php $i = 0; ?>
<ul class="tab-widget__list">
<?php while( have_rows('the_commusoft_story') ): the_row(); ?>
<?php $i++ ?>
<li class="tab-widget__item">
<a href="#tab-panel-<?php echo $i; ?>" class="tab-widget__link">
<span class="ss-icon <?php the_sub_field('icon'); ?>"></span>
<h2><?php the_sub_field('section_title'); ?></h2>
<?php the_sub_field('section_description'); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php }; ?>
<?php if( have_rows('the_commusoft_story') ) { ?>
<?php $i = 0; ?>
<div class="tab-widget__tabs">
<?php while( have_rows('the_commusoft_story') ): the_row(); ?>
<?php $i++ ?>
<div class="tab-widget__tab-content">
<h2 id="tab-panel-<?php echo $i; ?>"><?php the_sub_field('section_title'); ?></h2>
<?php the_sub_field('content'); ?>
</div>
<?php endwhile; ?>
</div>
<?php }; ?>
</div>
</div>
</div>
SO on the page are two sections, the problem appears when I try to move it as second section and it disappears...
Any idea what is going on, and where the mistake is?
Thanks
Your code can definately be simplified. I've removed the extra IF statement - it's not required. I've also added line terminations to the $i++ operators:
<div class="b b--alt">
<div class="container">
<div class="tab-widget tab-widget--team js-tab-widget">
<?php if( have_rows('the_commusoft_story') ) : ?>
<?php $i = 0; ?>
<ul class="tab-widget__list">
<?php while( have_rows('the_commusoft_story') ): the_row(); ?>
<?php
$i++; // This starts at 1.
?>
<li class="tab-widget__item">
<a href="#tab-panel-<?php echo $i; ?>" class="tab-widget__link">
<span class="ss-icon <?php the_sub_field('icon'); ?>"></span>
<h2><?php the_sub_field('section_title'); ?></h2>
<?php the_sub_field('section_description'); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php $i = 0; ?>
<div class="tab-widget__tabs">
<?php while( have_rows('the_commusoft_story') ): the_row(); ?>
<?php
$i++;
?>
<div class="tab-widget__tab-content">
<h2 id="tab-panel-<?php echo $i; ?>"><?php the_sub_field('section_title'); ?></h2>
<?php the_sub_field('content'); ?>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
This, in theory, should be a working piece of code. So - if what I have written doesn't work for you; I'd advise checking for PHP errors, or possibly looking for CSS styling conflicts.
Keep us posted how you get on :)
Related
I am trying to get a block to split content after so many entry's have been selected by the user in the backend of my website.
This is what is is supposed to look like
However at the moment I cannot get it to break off a second line. here is the code below to show how I am rendering my block along with an image of what is being rendered
function roomFacilitiesLists(){
$roomFacilitiesList1 = get_field ("facilities_list_1") ;
$roomFacilitiesList2 = get_field ("facilities_list_2") ;
$roomFacilitiesList3 = get_field ("facilities_list_3") ;
$roomFacilitiesLeftSideImage = get_field ("room_facilities_left_side_image") ;
$roomFacilitysBackgroundImage = get_field ("room_facilitys_background_image") ;
?>
<div class="left-side-image-block-parent">
<div class="left-sided-image">
<img src="<?php echo $roomFacilitiesLeftSideImage['url']; ?>" ; />
</div>
</div>
<div class="right-side-room-facilitys">
<div class="room-facilities-lists" style="background-image: url(<?php echo $roomFacilitysBackgroundImage['url']; ?>)"> ;
</div>
<div class="main-lists-container">
<div class="column-1-list">
<?php
if( $roomFacilitiesList1 ): ?>
<ul>
<?php foreach( $roomFacilitiesList1 as $roomFacilitiesList1 ): ?>
<div><?php echo $roomFacilitiesList1; ?></div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<div class="column-3-list columns-2">
<?php
if( $roomFacilitiesList3 ): ?>
<ul>
<?php foreach( $roomFacilitiesList3 as $roomFacilitiesList3 ): ?>
<div><?php echo $roomFacilitiesList3; ?></div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<div class="column-2-list">
<?php
if( $roomFacilitiesList2 ): ?>
<ul>
<?php foreach( $roomFacilitiesList2 as $roomFacilitiesList2 ): ?>
<div><?php echo $roomFacilitiesList2; ?></div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
Now this makes up 3 columns of data however the other 2 are not the issue here is "columns-3" which is the data output causing the issues.
I have tried using https://www.php.net/manual/en/function.str-split.php as well as https://www.php.net/manual/en/function.explode but with no prevail any help here would be great.
I'm using Bootstrap, I have a list of posts and I want to wrap every 2 posts on a row. Each post is wrapped on a <div col>. You can see live here.
I tried with this but it wrap the row each one post:
<?php
$num = 0;
// Check if there are any posts to display
if ( have_posts() ) : ?>
<?php
// The Loop
while ( have_posts() ) : the_post();
if($num%2) {
echo "<div class='row' style='margin-bottom: 2.3em;''>";
}
?>
<div class="col-xs-12 col-sm-6 col-md-6">
<h2 class="category-encabezado">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Enlace a <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>
<small>Hace
<?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ''; ?>
</small>
<div class="entry">
<p>
<?php the_excerpt(); ?>
</p>
<?php
$my_shortcode = get_field('audio-field');
echo do_shortcode( $my_shortcode );
?>
</div>
</div>
<?php
if($num %2) {
echo '</div>';
}
$num++
?>
<?php endwhile; // End Loop
?>
</div>
<?php
You have to put div.row Out of the Loop while
I've built a form on a WordPress custom post type archive page that filters said posts using values pulled from a taxonomy and set with select multiple fields. The form submits to the same page. It's probably built "the long way round" but it works.
However, now I'm being asked to change the select multiple fields to Checkboxes and I can't seem to get it right. The checked values aren't being passed when the form submits.
Here's my code with my attempt to turn one of the two selects into a set of checkboxes. This first batch just sets the values to be pulled in:
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post();
$topics = get_field('program_topic');
foreach( $topics as $topic ): ?>
<?php $program_topic_names[] = $topic->name ; ?>
<?php $program_topic_links[] = $topic->slug ; ?>
<?php $topic_name_results = array_unique($program_topic_names) ?>
<?php $topic_link_results = array_unique($program_topic_links) ?>
<?php endforeach;
$types = get_field('program_type');
foreach( $types as $type ): ?>
<?php $program_type_names[] = $type->name ; ?>
<?php $program_type_links[] = $type->slug ; ?>
<?php $type_name_results = array_unique($program_type_names) ?>
<?php $type_link_results = array_unique($program_type_links) ?>
<?php endforeach;
endwhile;
$final_topic_results = array_combine($topic_name_results, $topic_link_results);
$final_type_results = array_combine($type_name_results, $type_link_results);
endif;
//print_r($final_topic_results);
//print_r($final_type_results);
wp_reset_postdata();
And the Filter Row / Form:
<!-- Filter row -->
<div class="row filter_row">
<div class="container">
<div class="col span_3_of_12">
<h2>Find a Program</h2>
</div>
<div class="col span_9_of_12">
<form id='program_filters' class='program-filters multiple' method="post" onsubmit="showHide(); return false">
<h4 class="span_12_of_12 no_padding" style="font-weight: 700;">Select one or more Topics and/or Types</h4>
<div class="styled-select multiple span_7_of_12">
<fieldset name="program_topic" id="program_topic">
<?php
foreach( $final_topic_results as $key => $value ):
echo "<input type='checkbox' ".checked( $_GET['program_topic'], $key )." value='$key' name='$key' /> $key <br>";
endforeach;
?>
</fieldset>
</div>
<div class="styled-select span_3_of_12">
<select multiple name="program_type" id="program_type">
<?php
foreach( $final_type_results as $key => $value ):
echo "<option ".selected( $_GET['program_type'], $key )." value='$value'>$key</option>";
endforeach;
?>
</select>
</div>
<div class="button_col span_2_of_12">
<input type='button' id='submitFormData' value='Find' onclick="this.form.submit()";>
<input type='button' value='Reset' onclick="window.location.href='/programs/'">
</div>
</form>
</div>
<div style="clear:left;"></div>
</div>
</div>
<!-- Filter row -->
And here's the Post type code I didn't include before:
<!-- Program row -->
<div class="container program-list-wrap"><ul>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
$dateformatstring = "l, F d";
$unixtimestamp = strtotime(get_field('program_date'));
$instructor = get_field('program_instructor_name');
$instructorInfo = get_field('program_instructor_info');
$program_date = get_field('program_date');
$current = time(); ?>
<?php if($current < $unixtimestamp || empty($program_date) ) : ?>
<li><div class="program list">
<div class="row program-intro">
<div class="col span_9_of_12">
<div class="program-vitals">
<h2><?php the_title(); ?></h2>
<?php if(get_field('program_date')) : ?><div class="program-date-time"><h4><?php echo date_i18n($dateformatstring, $unixtimestamp); if(get_field('program_time')) : ?> — <?php the_field('program_time');?><?php endif; ?></h4></div><?php endif; ?>
<?php $types = get_field('program_type');
if( $types ): ?><div class="types">
<?php $i = 1; ?>
<?php foreach( $types as $type ): ?>
<strong><?php echo $type->name; ?></strong><?php $rowCount = count( get_field('program_type') ); if($i < $rowCount): echo ', '; endif; $i++; ?>
<?php endforeach; ?></div>
<?php $topics = get_field('program_topic');
if( $topics ): ?><div class="topics">
<?php $i = 1; ?>
<?php foreach( $topics as $topic ): ?>
<strong><?php echo $topic->name; ?></strong><?php $rowCount = count( get_field('program_topic') ); if($i < $rowCount): echo ', '; endif; $i++; ?>
<?php endforeach; ?></div>
<?php endif; ?>
<?php if($instructor):?><strong>Instructor:</strong> <?php echo $instructor; if($instructorInfo):?>, <?php echo $instructorInfo; endif; endif; ?>
<?php endif; ?></div>
<?php if(get_field('program_intro')) : ?><div><?php the_field('program_intro'); ?></div><?php endif; ?>
</div>
<div class="col span_3_of_12">
<?php $register = get_field('program_registration_url'); $registerAlum = get_field('program_alum_registration_url'); $cost = get_field('program_amount'); $brochure = get_field('program_brochure_url') ?>
<div class="button_col"><h2><?php echo $cost ?></h2>Register HereUMKC Alumni Registration <?php if( $brochure) :?>Download a brochure<?php endif; ?></div>
Learn More
</div>
</div>
<div class="program-extras">
<?php if(get_field('program_includes')) : ?><div><h4><strong><?php the_title() ?> Includes:</strong></h4><?php the_field('program_includes'); ?></div><?php endif; ?>
<?php if(get_field('program_learn')) : ?><div><h2>What You Will Learn</h2><?php the_field('program_learn'); ?></div><?php endif; ?>
<?php if(get_field('program_key_topics')) : ?><div><h2>Key Topics</h2><?php the_field('program_key_topics'); ?></div><?php endif; ?>
<?php if(get_field('program_attendee')) : ?><div><h2>Who Should Attend</h2><?php the_field('program_attendee'); ?></div><?php endif; ?>
<?php if(get_field('program_instructor')) : ?><div><h2>About the Instructor</h2>
<?php $image = get_field('program_instructor_photo');
if( !empty($image) ): ?><div class="program_instructor_photo"><img src="<?php the_field('program_instructor_photo'); ?>);" /></div><?php endif; ?>
<?php the_field('program_instructor'); ?></div>
<?php endif; ?>
<div style="clear: left;"></div>
</div>
</div></li>
<?php endif; ?>
<div style="clear: left;"></div>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endwhile; ?>
<?php endif; ?>
<div style="clear: left;"></div>
</ul></div>
<!-- Program row -->
I feel like I'm missing something obvious. Any help would be appreciated.
I have a PHP while loop and i'm trying to use a counter to display the total number of items (.question) that are within it. I'm trying to echo the contents of $child_i outside of the while loop to do this but the code below doesn't work. Whats the best way of achieving this?
EDIT
Updated to show full code
<?php if( have_rows('faqs') ): ?>
<ul class="responsive-accordion responsive-accordion-default bm-larger">
<?php while( have_rows('faqs') ): the_row(); ?>
<li>
<div class="responsive-accordion-head"><span class="ico arrow-right"></span><?php the_sub_field('category_name'); ?> <span class="faq-counter">
<!-- TRYING TO ECHO $CHILD_I ON THE LINE BELOW BUT NOT WORKING -->
<?php echo($child_i); ?> Questions</span></div>
<!-- $CHILD_I IS DEFINED ON THE NEXT LINE -->
<?php if( have_rows('questions') ): $child_i = 0; ?>
<!-- THIS ECHO WORKS -->
<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('questions') ): ?>
</div>
</li>
<?php endwhile; // while( has_sub_field('to-do_lists') ): ?>
</ul>
<?php endif; // if( get_field('to-do_lists') ): ?>
Try this
$questions = get_field('questions'); $size=count($questions); echo $size;
Thanks to some answers on here, I've managed to distinguish my posts into the latest post and all the rest. However, it needs to be the oldest post. Here is my current loop:
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; $c=0;?>
<?php while (have_posts()) : the_post(); ?>
<!-- first post -->
<?php $c++;
if( $c == 1) :?>
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="down">a</div>
</div>
</div>
<?php else :?>
<!-- second post -->
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="up">b</div>
</div>
</div>
<?php endif; ?>`
I saw somewhere that you can use a while loop to target the last post from a post.length. However, I am unsure how to implement this.
Yes, you're right. Use count.
Suppose the total posts is 5 for $total_posts = count($posts);.
You'll have to check your $counter for total - 1 as the array is $posts[0], $posts[1], $posts[2], $posts[3], $posts[4].
<?php
if ( have_posts() ) :
$counter = 0;
$total_posts = count($posts) - 1;
while ( have_posts() ) :
the_post();
if( $counter == $total_posts ) {
?>
<div class="container"><?php // CUSTOM CODE FOR LAST ARTICLE ?></div>
<?php
} else {
?>
<div class="container"><?php // CUSTOM CODE FOR THE REST OF ARTICLES ?></div>
<?php
}
$counter++;
endwhile;
endif;
Note that you only need to open <?php and close ?> when changing from PHP to HTML, using them at every line is very confusing.