I can’t seem to get checkboxes working in an Advanced Custom Fields repeater field. The goal is that a different image will be spit out based on what checkbox has been ticked off. The time/title/description part is working great... just need some assistance understanding how to work in the checkbox values.
<?php if( have_rows('agenda') ): ?>
<ul>
<?php while( have_rows('agenda') ): the_row();
$time = get_sub_field('time');
$title = get_sub_field('title');
$description = get_sub_field('description');
$image = get_sub_field('imagepicker');
?>
<li>
<h3><?php echo $time; ?><?php echo $title; ?></h3>
<?php if( $description ): ?>
<?php echo $description; ?>
<?php endif; ?>
<?php if ( 'option1' == get_sub_field('imagepicker') ): ?>
<img src="/option1.jpg" />
<?php elseif ( 'option2' == get_sub_field('imagepicker') ): ?>
<img src="/option2.jpg" />
<?php elseif ( 'option3' == get_sub_field('imagepicker') ): ?>
<img src="/option3.jpg" />
<?php else: ?>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
(and yes I have gone over the other similar S.O. answers but no luck)
finally figured it out... working snippet below in case anyone else runs into the same thing.
<?php if ($image[0] == 'option1') : ?>
<img src="/option1.jpg" />
<?php elseif ($image[0] == 'option2') : ?>
<img src="/option2.jpg" />
<?php elseif ($image[0] == 'option3') : ?>
<img src="/option3.jpg" />
<?php endif; ?>
Related
After creating customfield from ACF. I select multiple articles. How can I show only the last 2 posts? Thank!
Show only 2 post from relationship ACF
I would use the post_object field. With this field, you can select which objects you would like to display, when you configure it to "multiple values".
<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ) { ?>
<ul>
<?php foreach( $featured_posts as $post ) {
setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php } ?>
</ul>
<?php
wp_reset_postdata(); ?>
<?php } ?>
Is it possible to display a view inside another view?
I have the following code:
<?php if ($result->type === 'brochure') : ?>
<div>
// massive template block
</div>
<?php elseif ($result->type === 'library') : ?>
<div>
// massive template block different from above
</div>
<?php else : ?>
<div>
// massive template block different from both above
</div>
<?php endif; ?>
I want to replace those with a content block so to speak. I had a look at view blocks but I'm either using it wrong or it doesn't do what I want it to do.
Is this possible in CakePHP 3?
you can use elements for that.
first you should create elements in src/Template/Element directory with .ctp format like this
// in brochure.ctp file in src/Template/Element
<div>
// your massive template block
</div>
then you can call elements like this :
<?php if ($result->type === 'brochure') : ?>
<?= $this->element("brochure") ?>
<?php elseif ($result->type === 'library') : ?>
<?= $this->element("library") ?>
<?php else : ?>
<?= $this->element("default") ?>
<?php endif; ?>
I'm using Advanced Custom Fields to call my related posts, which is all working fine, but how can I adapt this code to only call 3 posts at random?
Thank you for any help in advance!
<?php
$posts = get_field('associate_adverts');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<img src="<?php the_field('advert'); ?>">
<?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
I associated it the opposite way in the end and this solution worked..
<?php
$adverts = get_posts(array(
'post_type' => 'adverts',
'posts_per_page' => '3',
'meta_query' => array(
array(
'key' => 'associate_adverts', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
?>
<?php if( $adverts ): ?>
<?php foreach( $adverts as $advert ): ?>
<?php $photo = get_field('advert', $advert->ID); ?>
<img src="<?php echo $photo['url']; ?>"/>
<?php endforeach; ?>
<?php endif; ?>
I must stres out that i’m not a developer so i might use some “unsupported” terms :).
Ok the issue, i have created custom post type called Firm. Also i have created field group with 7 fields (text fields mainly, including website URL field and Google map field) and i have made template that displays those fields on frontend page. Once it’s saved all data is saved in database and new post under post type Firm is created. So that all works great. The main problem/question is:
How can i display all new posts in that post type (Firm) on one page? I know i must create some loop, array for those posts, but as i said i’m not developer so i’m kind of stuck with this one.
If someone could give me a hint or some link, or any kind of pointers so i could figure out in what direction to go. Thanks in advance for your answers.
You can refer Wordpress Codex.
Check sample code given below
$args = array( 'post_type' => 'product', 'posts_per_page' => 10 ); // for more parameter check link http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
// Displays Advanced custom field value
the_field('field-name');
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
Ok thanks so far, thank you #Arun, i have come up with this:
<?php
$args = array( 'post_type' => 'company', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
echo '<p>' . '<span>Official Wesite</span>' . '<span> : </span>' . get_field('web_site') . '</p>';
echo '</div>';
endwhile; ?>
<?php
$location = get_field('location');
if( !empty($location) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
</div>
<?php endif; ?>
So im strugling now with how to incorporate this google map in loop. I do have title and website link for all posts but i need to have maps for each post as well.
Ok this is what i have at the end, and it's working.
<?php
$args = array( 'post_type' => 'company', 'posts_per_page' => 15 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="boxy">
<div class="acf_company_name">
<h5>Company Name: </h5>
<p><?php the_field('company_name'); ?></p>
</div>
<div class="acf_full_name">
<h5>Full Name: </h5>
<?php the_field('full_name'); ?>
</div>
<div class="acf_vat">
<h5>VAT: </h5>
<?php the_field('vat'); ?>
</div>
<div class="acf_email">
<h5>E-mail: </h5>
<?php the_field('email'); ?>
</div>
<div class="acf_website">
<h5>Official website: </h5>
<?php the_field('website'); ?>
</div>
<?php if ( get_field('logo_company') ) : ?>
<div class="acf_logo_company"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php $image = get_field(logo_company); ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /></a></div>
<?php endif; ?>
<div class="acf_company_location"><?php $location = get_field('company_location');?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>" data-lng="<?php echo $location['address']; ?>"></div>
</div></div>
<div class="acf_company_location">
<h5>Company location: </h5>
<?php the_field('company_location'); ?>
</div>
</div>
<?php endwhile; ?>
I have two loops on custom page, I want from the second loop to omit the post with Post ID from the first loop (of course show all other posts from category 36).
Edit: this is the whole code in the php file:
<?php
global $udesign_options;
// construct an array of portfolio categories
$portfolio_categories_array = explode( ',', $udesign_options['portfolio_categories'] );
if ( $portfolio_categories_array != "" && post_is_in_category_or_descendants( $portfolio_categories_array ) ) :
// Test if this Post is assigned to the Portfolio category or any descendant and switch the single's template accordingly
include 'single-Portfolio.php';
else : // Continue with normal Loop (Blog category)
get_header();
$content_position = ( $udesign_options['blog_sidebar'] == 'left' ) ? 'grid_16 push_8' : 'grid_16';
if ( $udesign_options['remove_single_sidebar'] == 'yes' ) $content_position = 'grid_24';
?>
<div id="content-container" class="container_24">
<div id="main-content" class="<?php echo $content_position; ?>">
<div class="main-content-padding">
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="entry" style="margin:-30px 0 00px 0;">
<?php // Post Image
if( $udesign_options['display_post_image_in_single_post'] == 'yes' ) display_post_image_fn( $post->ID, false );
the_content(__('<p class="serif">Read the rest of this entry »</p>', 'udesign'));
wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
<?php
$args = array( 'category' => 36, 'post_type' => 'post' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php echo do_shortcode('[divider_top]'); ?>
<?php endforeach; ?>
<div><?php comments_template();
endwhile; else: ?>
<p><?php esc_html_e("Sorry, no posts matched your criteria.", 'udesign'); ?></p>
<?php endif; ?></div>
</div><!-- end main-content-padding -->
</div><!-- end main-content -->
<?php
if( ( !$udesign_options['remove_single_sidebar'] == 'yes' ) && sidebar_exist('BlogSidebar') ) { get_sidebar('BlogSidebar'); }
?>
</div><!-- end content-container -->
<?php endif; // end normal Loop ?>
<div class="clear"></div>
<?php get_footer(); ?>
I figured it out, this code needs to be inserted $args = array('exclude' =>theExcludedID, 'category' => 36, 'post_type' => 'post' );
According to the question's update, it seems the first loop displays all categories in portfolio_categories from a custom query, most probably including those from category 36 also.
The only way I can think off to not repeat the posts in the second loop is to exclude all category 36 posts from the first loop.
I can't test the code, but here is an idea of how to do it:
Add 3 lines of code after the Loop, like this:
if (have_posts()) : while (have_posts()) : the_post(); // This is the Loop
$Category = get_the_category( $post->ID );
$CatID = $Category[0]->cat_ID ; // The 0 assumes the post has only one category. If there are more, the number must be changed accordingly.
if ($CatID == 36) continue;
Use the following at the top of your loop:
if(get_the_ID() == theExcludedID){
continue;
}
To skip any iteration.