ACF - Looping thru posts, then thru flexible content, then thru repeater - php

What I am trying to do is get images from a repeater field and fill my homepage with images that lead to posts.
This is usually easy for me but the catch here is that there are multiple images for each post in a repeater field, which are in a flexible content field of the advanced custom fields plugin.
The goal is to have an unlimited amount of images coming from the 'square_image' field in completely random order on the homepage that lead to their particular post. So with 15 posts that each have 15 'square_images' there would be 225 images on the homepage in completely random placement linking to their respective posts.
I am wondering if I need to build an array inside the while loops so that I can get that ultimate random order at the end, instead of just the first 15 images from post 1 in random order, and then the next 15 images from post 2 in a random order which is not what i am trying to do.
<?php get_header(); /* Template Name: Homepage Looper */ ?>
<div id="homepage">
<?php
// the query
$args = array(
'post_type' => 'projects',
'posts_per_page' => '-1',
'orderby' => 'rand',
);
$the_query = new WP_Query( $args );$count = 1; ?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="row">
<!-- the loop -->
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
if( have_rows('the_page_content') ):
while ( have_rows('the_page_content') ) : the_row();
if(get_sub_field('images')) :
while(has_sub_field('images')) : the_row();
?>
<?php
$image = get_sub_field('square_image');
if ( !empty($image) ):
// vars
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
?>
<div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 home-tile-holder">
<a href="<?php the_permalink(); ?>" rel="<?php the_title(); ?>">
<img src="<?php echo $url; ?>" alt="<?php echo $alt; ?>" class="img-responsive home-tile" />
<span class="title"><?php the_title(); ?><br><img src="<?php echo get_stylesheet_directory_uri(); ?>/img/magnify.png"></span>
</a>
</div>
<?php endif; ?>
<?php
if($count % 6 == 0) {echo '</div><div class="row">';}
$count++;
?>
<?php
endwhile; // while(has_sub_field('images')
endif; // get_sub_field('images')
endwhile; // have_rows('the_page_content')
endif; // have_rows('the_page_content')
endwhile; // $the_query->have_posts ?>
<!-- end of the loop -->
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; // $the_query->have_posts ?>
</div>
<div id="single_project" style="position: absolute; margin-left:100%; width:100%;">
</div>
<?php get_footer(); ?>
THE ACF EXPORT IS BELOW
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_project-fields',
'title' => 'Project Fields',
'fields' => array (
array (
'key' => 'field_534efd890cd30',
'label' => 'Hero Image',
'name' => 'hero_image',
'type' => 'image',
'instructions' => 'Upload the large version for this post that will be used as the hero image on the top of the page.',
'save_format' => 'url',
'preview_size' => 'thumbnail',
'library' => 'all',
),
array (
'key' => 'field_534efdb70cd31',
'label' => 'Page Content',
'name' => 'the_page_content',
'type' => 'flexible_content',
'instructions' => 'Build out the content for this project.',
'layouts' => array (
array (
'label' => 'Image Row',
'name' => 'image_row',
'display' => 'row',
'min' => '',
'max' => '',
'sub_fields' => array (
array (
'key' => 'field_534efdea0cd32',
'label' => 'Images',
'name' => 'images',
'type' => 'repeater',
'column_width' => '',
'sub_fields' => array (
array (
'key' => 'field_53716523cfc52',
'label' => 'Square Image',
'name' => 'square_image',
'type' => 'image',
'column_width' => '',
'save_format' => 'object',
'preview_size' => 'thumbnail',
'library' => 'all',
),
array (
'key' => 'field_53716532cfc53',
'label' => 'Full Size Image',
'name' => 'full_size_image',
'type' => 'image',
'column_width' => '',
'save_format' => 'object',
'preview_size' => 'thumbnail',
'library' => 'all',
),
),
'row_min' => 1,
'row_limit' => 6,
'layout' => 'table',
'button_label' => 'Add Image Set',
),
),
),
array (
'label' => 'Text + Images',
'name' => 'text_images',
'display' => 'row',
'min' => '',
'max' => '',
'sub_fields' => array (
array (
'key' => 'field_5356d3a6a8ac9',
'label' => 'Title',
'name' => 'title',
'type' => 'text',
'instructions' => 'Enter in the title of this project if you wish for it to show above the text.',
'column_width' => '',
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'formatting' => 'html',
'maxlength' => '',
),
array (
'key' => 'field_534eff4ae5d31',
'label' => 'Text Content',
'name' => 'text_content',
'type' => 'wysiwyg',
'column_width' => '',
'default_value' => '',
'toolbar' => 'full',
'media_upload' => 'yes',
),
array (
'key' => 'field_534eff32e5d30',
'label' => 'Images',
'name' => 'images',
'type' => 'repeater',
'column_width' => '',
'sub_fields' => array (
array (
'key' => 'field_53716549cfc54',
'label' => 'Square Image',
'name' => 'square_image',
'type' => 'image',
'column_width' => '',
'save_format' => 'object',
'preview_size' => 'thumbnail',
'library' => 'all',
),
array (
'key' => 'field_53716557cfc55',
'label' => 'Full Size Image',
'name' => 'full_size_image',
'type' => 'image',
'column_width' => '',
'save_format' => 'object',
'preview_size' => 'thumbnail',
'library' => 'all',
),
),
'row_min' => '',
'row_limit' => 3,
'layout' => 'table',
'button_label' => 'Add Image Set',
),
),
),
array (
'label' => 'Images + Text',
'name' => 'images_text',
'display' => 'row',
'min' => '',
'max' => '',
'sub_fields' => array (
array (
'key' => 'field_534effafb045d',
'label' => 'Images',
'name' => 'images',
'type' => 'repeater',
'column_width' => '',
'sub_fields' => array (
array (
'key' => 'field_53716567cfc56',
'label' => 'Square Image',
'name' => 'square_image',
'type' => 'image',
'column_width' => '',
'save_format' => 'object',
'preview_size' => 'thumbnail',
'library' => 'all',
),
array (
'key' => 'field_53716576cfc57',
'label' => 'Full Size Image',
'name' => 'full_size_image',
'type' => 'image',
'column_width' => '',
'save_format' => 'object',
'preview_size' => 'thumbnail',
'library' => 'all',
),
),
'row_min' => '',
'row_limit' => 3,
'layout' => 'table',
'button_label' => 'Add Image Set',
),
array (
'key' => 'field_5356d3dba8aca',
'label' => 'Title',
'name' => 'title',
'type' => 'text',
'instructions' => 'Enter in the title of this project if you wish for it to show above the text.',
'column_width' => '',
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'formatting' => 'html',
'maxlength' => '',
),
array (
'key' => 'field_534effafb045c',
'label' => 'Text Content',
'name' => 'text_content',
'type' => 'textarea',
'column_width' => '',
'default_value' => '',
'placeholder' => '',
'maxlength' => '',
'rows' => '',
'formatting' => 'br',
),
),
),
),
'button_label' => 'Add Row',
'min' => '',
'max' => '',
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'projects',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
0 => 'the_content',
1 => 'excerpt',
2 => 'custom_fields',
3 => 'discussion',
4 => 'comments',
5 => 'format',
6 => 'featured_image',
7 => 'send-trackbacks',
),
),
'menu_order' => 0,
));
}

Create an array of the images during the initial posts loop, randomize that array, then output the html in a loop through the images array.
<?php
$args = array(
'post_type' => 'projects',
'posts_per_page' => '-1',
'orderby' => 'rand',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
$images = array();
while ( $the_query->have_posts() ) : $the_query->the_post();
if( have_rows('the_page_content') ):
while ( have_rows('the_page_content') ) : the_row();
if( have_rows('images') ) :
while( have_rows('images') ) : the_row();
$image = get_sub_field('square_image');
if ( !empty($image) ):
// add image properties and related data to main images array
$images[] = array(
'url' => $image['url'],
'title' => $image['title'],
'alt' => $image['alt'],
'postdata' => array(
'id' => get_the_ID(),
'title' => get_the_title(),
'permalink' => get_permalink()
)
);
endif;
endwhile; // while(have_rows('images'))
endif; // have_rows('images')
endwhile; // have_rows('the_page_content')
endif; // have_rows('the_page_content')
endwhile; // $the_query->have_posts
wp_reset_postdata();
endif; // $the_query->have_posts
// Loop through the images array and output html for each image
shuffle($images); // Randomize the order of the images
$count = 1;
foreach($images as $img) :
?>
<div class="row">
<div class="col-xs-6 col-sm-4 col-md-3 col-lg-2 home-tile-holder">
<a href="<?php echo $img['postdata']['permalink']; ?>" rel="<?php echo $img['postdata']['title']; ?>">
<img src="<?php echo $img['url']; ?>" alt="<?php echo $img['alt']; ?>" class="img-responsive home-tile" />
<span class="title"><?php $img['postdata']['title']; ?><br><img src="<?php echo get_stylesheet_directory_uri(); ?>/img/magnify.png"></span>
</a>
</div>
<?php if($count % 6 == 0) echo '</div><div class="row">';?>
<?php $count++; ?>
<?php endforeach; ?>
</div>

Related

custom vc elements won't render on translated pages wpml plugin

I have created a custom post type for a slider posts and then a custom visual composer element to gather those posts from that post type and then assemble it to create a slider but it won't render on the translated pages like if I create a german page, the custom vc element (slider) won't render or show unto that page. Any help, ideas please?
here's the vc element
<?php class axada_custom_elements extends WPBakeryShortCode {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'axada_custom_elements_mapping' ) );
add_shortcode( 'axada_slider', array( $this, 'axada_slider_html' ) );
}
// Element Mapping
public function axada_custom_elements_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
// Map the block with vc_map()
vc_map(
array(
'name' => __('Axada Slider', 'text-domain'),
'base' => 'axada_slider',
'description' => __('Axada Slider', 'text-domain'),
'category' => __('Axada Elements', 'text-domain'),
"as_parent" => array('except' => ''), // Use only|except attributes to limit child shortcodes (separate multiple values with comma)
"content_element" => true,
"show_settings_on_create" => true,
"is_container" => true,
'icon' => get_template_directory_uri().'/img/axada-short-logo.png',
'params' => array(
array(
'type' => 'textfield',
'holder' => 'div',
'class' => 'title-class',
'heading' => __( 'Slider ID', 'text-domain' ),
'param_name' => 'slider_id',
'value' => __( '', 'text-domain' ),
'description' => __( 'Slider ID', 'text-domain' ),
'admin_label' => false,
'weight' => 0,
'group' => 'Custom Group',
),
array(
'type' => 'textfield',
'holder' => 'div',
'class' => 'title-class',
'heading' => __( 'Slider Classes', 'text-domain' ),
'param_name' => 'slider_class',
'value' => __( '', 'text-domain' ),
'description' => __( 'Slider Classes', 'text-domain' ),
'admin_label' => false,
'weight' => 0,
'group' => 'Custom Group',
),
array(
'type' => 'textfield',
'holder' => 'div',
'class' => 'title-class',
'heading' => __( 'Slider Custom Styles', 'text-domain' ),
'param_name' => 'slider_custom_styles',
'value' => __( '', 'text-domain' ),
'description' => __( 'Slider Custom Styles', 'text-domain' ),
'admin_label' => false,
'weight' => 0,
'group' => 'Custom Group',
),
),
)
);
}
// Element HTML
public function axada_slider_html( $atts ) {
// Params extraction
extract(
shortcode_atts(
array(
'slider_id' => '',
'slider_class' => '',
'slider_custom_styles' => '',
),
$atts
)
);
$slider_custom_styles = $slider_custom_styles !== '' ? 'style="'.$slider_custom_styles.'"' : '';
// Fill $html var with data
//query for slider
ob_start();
//query for slider
$args = array(
'post_type' => 'axada_slider',
'meta_key' => '_arrangement_value_key',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish',
);
$slider = new WP_Query( $args );
if ( $slider->have_posts() ) :
//start slider
?>
<section class="kenburns image-slider slider-all-controls controls-inside pt0 pb0 height-70 vid-bg bg-dark" id="home-slider-wrapper">
<ul<?php echo $slider_id !== '' ? ' id="'.$slider_id.'" ' : ' '; ?>class="slides<?php echo $slider_class !== '' ? ' '.$slider_class : ''?>"<?php echo $slider_custom_styles !== '' ? ' style="'.$slider_custom_styles.'" ' : ''; ?> >
<?php while ( $slider->have_posts() ) : $slider->the_post(); ?>
<li>
<?php if(get_post_meta( get_the_ID(), '_axada_slider_video_value_key', true ) && get_post_meta( get_the_ID(), '_axada_slider_video_value_key', true )!==''){?>
<div class="player" data-video-id="<?php echo get_post_meta( get_the_ID(), '_axada_slider_video_value_key', true ); ?>" data-start-at="0"></div>
<div class="background-image-holder bg-size-cover">
<img alt="image" class="background-image" src="<?php the_post_thumbnail_url(); ?>" />
</div>
<?php }else{ ?>
<div class="background-image-holder bg-size-cover">
<img alt="image" class="background-image" src="<?php the_post_thumbnail_url('full'); ?>">
</div>
<div class="container v-align-transform">
<div class="row text-center">
<div class="full-width divider mb-xs-64"></div>
<?php the_content(); ?>
</div>
</div>
<?php } ?>
</li>
<?php endwhile; ?>
</ul>
</section>
<?php
//end slider
endif;
return ob_get_clean();
}

unable to pass array variable to select field in metabox

I'm using this code to get data from text type metabox.. This will create an array
$args = array('post_type' => 'locations_add');
$prefix = 'nilgiris_';
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
while ($the_query->have_posts()):
$the_query->the_post();
$l_names[] = rwmb_meta($prefix . 'l_name');
$lo_name = array();
foreach( $l_names as $l_namea ){
$lo_name[] = array(
$l_namea => $l_namea
);
}
endwhile;
endif;
My problem is that when using the wp_query function along with metabox the metabox will not load..
Hers is the entire code
<?php
$args = array('post_type' => 'locations_add');
$prefix = 'nilgiris_';
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
while ($the_query->have_posts()):
$the_query->the_post();
$l_names[] = rwmb_meta($prefix . 'l_name');
$lo_name = array();
foreach( $l_names as $l_namea ){
$lo_name[] = array(
$l_namea => $l_namea
);
}
endwhile;
endif;
add_filter('rwmb_meta_boxes', 'website_register_meta_boxes');
function website_register_meta_boxes($meta_boxes) {
$prefix = 'nilgiris_';
$meta_boxes[] = array(
'id' => 'location_types',
'title' => __('location name', $prefix),
'post_types' => array('locations_add'),
'context' => 'normal',
'priority' => 'high',
'autosave' => true,
// List of meta fields
'fields' => array(
array(
'name' => __('Enter Location Name', $prefix),
'id' => "{$prefix}l_name",
'desc' => __('Enter name', $prefix),
'type' => 'text',
//'clone' => true,
),
),
);
$meta_boxes[] = array(
'id' => 'location_types',
'title' => __('location name', $prefix),
'post_types' => array('Hotels'),
'context' => 'normal',
'priority' => 'high',
'autosave' => true,
'fields' => array(
array(
'name' => __('Enter Location Name', $prefix),
'id' => "{$prefix}H_name",
'desc' => __('Enter name', $prefix),
'type' => 'text',
),
array(
'name' => __('Checkbox list', '$prefix'),
'id' => "{$prefix}H_list",
'type' => 'select',
**'options' => $lo_name,**
),
),
);
return $meta_boxes;
}

How can I use the Advanced Custom Fields plugin with WordPress Media Library?

I'm using the Advanced Custom Fields WordPress plugin to add a category selector to images in my media library. Eventually I would to show all the images that have a specific category.
When I get everything from the library, I'd like to grab the selected category and add it to my $image array, but when I call the_field() and/or get_field() it returns nothing.
This call is outside the WordPress post loop, so I passed each attachment's ID (as per the documentation and this question), but that does not appear to work either.
Am I missing something?
index.php:
<div id="content">
<div id="inner-content" class="wrap cf">
<main id="main" class="cf" role="main" >
<?php echo get_images_from_media_library(); ?>
</main>
</div>
</div>
functions.php:
function get_images_from_media_library() {
$args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => 100,
'orderby' => 'rand',
'post_parent' => $post->ID
);
$attachments = get_posts($args);
$images = array();
if ($attachments) {
foreach ($attachments as $attachment) {
$image = array(
'id' => $attachment->ID,
'src' => $attachment->guid,
'title' => $attachment->post_title,
'tags' => wp_get_post_tags($attachment->ID)
);
array_push($images, $image);
// For debug purposes
// Always returns 'FAILED'...
if ( get_field('category_select', $attachment->ID) ) {
$category = get_field('category_select', $attachment->ID);
echo '<h1>Category: '.$category.'</h1>';
} else {
echo '<h1>FAILED</h1>';
}
//TODO: Add $category to $images.
echo "<div class='gallery'>";
echo '<img src="'.$image['src'].'" class="col span-lg-1-md-2-sm-5 / gallery__image / '.$image['title'].'">';
echo "</div>";
}
}
}
EDIT:
Added the field group export code:
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_media-category',
'title' => 'Media Category',
'fields' => array (
array (
'key' => 'field_56aa8c5f67f98',
'label' => 'Category',
'name' => 'category_select',
'type' => 'radio',
'required' => 1,
'choices' => array (
'icon' => 'Icon',
'logo' => 'Logo',
'cover' => 'Cover',
),
'other_choice' => 0,
'save_other_choice' => 0,
'default_value' => 'icon : Icon',
'layout' => 'vertical',
),
),
'location' => array (
array (
array (
'param' => 'ef_media',
'operator' => '==',
'value' => 'all',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}

How to get wordpress option tree list item data

i use option tree list item for getting social icon & links.but i cannot get data from backend.
array(
'id' => 'social_icon',
'label' => __( 'Footer Social Icons & links', 'theme-text-domain' ),
'desc' => __( '', 'theme-text-domain' ),
'std' => '',
'type' => 'list-item',
'section' => 'header_footer',
'rows' => '',
'post_type' => '',
'taxonomy' => '',
'min_max_step'=> '',
'class' => '',
'condition' => '',
'operator' => 'and',
'settings' => array(
array(
'id' => 'social_icon_fb',
'label' => __( 'link', 'theme-text-domain' ),
'desc' => '',
'std' => '',
'type' => 'text',
'rows' => '10',
'post_type' => '',
'taxonomy' => '',
'min_max_step'=> '',
'class' => '',
'condition' => '',
'operator' => 'and'
),
array(
'id' => 'social_icon_upl',
'label' => __( 'icon', 'theme-text-domain' ),
'desc' => 'the best sixe for icon is 31x31.',
'std' => '',
'type' => 'upload',
'rows' => '10',
'post_type' => '',
'taxonomy' => '',
'min_max_step'=> '',
'class' => '',
'condition' => '',
'operator' => 'and'
),
)
)
and i want to use this in
<ul id="icons">
<li>
<img src="<?php get_option_tree( 'social_icon_upl', '', 'true' ); ?>" alt="">
</li>
</ul>
i use this to get data from backend.is this correct?? if no so what should be the code to get data.
Try this code
<?php
if ( function_exists( 'ot_get_option' ) ) {
/* get the slider array */
$slides = ot_get_option( 'social_icon', array() );
if ( ! empty( $slides ) ) {
foreach( $slides as $slide ) {
echo '
<li>
<img src="'.$slide['social_icon_upl'].'" alt="">
</li>
';
}
}
}
?>
list item always return array. you have to use foreach loop for getting every row of array. code is describe bellow.
*<?php
if ( function_exists( 'ot_get_option' ) ) {
$your_listitems_array = ot_get_option( 'your_listitems_slug', array() );
if ( ! empty( $your_listitems_array ) ) {
foreach( $your_listitems_array as $your_listitem) {
echo $your_listitem['your_listitem_option_slug'];
}
}
}
?>*

How to Save the Repeatable fields data in Reusable Custom box WordPress?

hello I am using reusable custom metaboxes from https://github.com/tammyhart/Reusable-Custom-WordPress-Meta-Boxes..
these are my fields
array(
'label' => __('Setup Slider', 'crispy' ),
'desc' => __('create your slider image/ text using these repeatable options', 'crispy' ),
'id' => $prefix.'repeatable',
'type' => 'repeatable',
'sanitizer' => array(
'title' => 'sanitize_text_field',
'desc' => 'wp_kses_data'
),
'repeatable_fields' => array (
array(
'label' => __(' Slider Text alignment', 'crispy'),
'id' => 'alignment',
'type' => 'radio',
'options' => array (
'one' => array (
'label' => __('Left', 'crispy' ),
'value' => 'left'
),
'two' => array (
'label' => __('Center', 'crispy' ),
'value' => 'center'
),
'three' => array (
'label' => __('Right', 'crispy' ),
'value' => 'right'
)
)
),
array(
'label' => __('Background Image/pattern', 'crispy' ),
'id' => 'image',
'type' => 'image'
),
array(
'label' => __('Title', 'crispy' ),
'id' => 'title',
'type' => 'text'
),
array(
'label' => __('Description', 'crispy' ),
'id' => 'desc',
'type' => 'textarea'
),
)
),
My problem is i don't know how to store the fields value... can anyone resolve my problem!!..
$home_slider_alignment = get_post_meta( $post->ID, 'alignment', true);
i used this but doesn't help!!.. Those fields can be repeatable so the values are stored in array!!.. i don't know how to retrieve stored values from that array??..
Please help me!!.
It is working :)
enter code here
<?php
$projectgallery = get_post_meta($post->ID, 'arcadia_well_projectgallery', true); {
echo '<ul class="custom_repeatable">';
foreach ($projectgallery as $project) {
echo '<li>';
foreach($project as $i=>$value) {
echo $value;
}
echo '</li>';
}
echo '</ul>';
}
?>

Categories