Understanding genesis_markup() in Genesis Framework - wordpress

I don't understand how the genesis_markup function turns:
array(
'open' => '<aside class="widget-areainfo-onder-content">',
'context' => 'widget-area-wrap',
'echo' => false,
'params' => array(
'id' => $id,
)
into
"<div class="widget-area">"
I do not understand why the id is not used in the output at all and the aside class is also not used....I would have thought that it should be.....
When I do this in the code:
$before_markup_args = array(
'open' => '<aside class="widget-areainfo-onder-content">',
'context' => 'widget-area-wrap',
'echo' => false,
'params' => array(
'id' => $id,
)
);
d($before_markup_args);
d(genesis_markup( $before_markup_args ));

First, I would like to confirm the missing space:
```
$before_markup_args = array(
'open' => '<aside class="widget-area info-onder-content">',
'context' => 'widget-area-wrap',
'echo' => false,
'params' => array(
'id' => $id,
)
);
```
"Onder" is Dutch for "Below". Thus, "info-onder-content" means "info-below-content". Possibly a title for the widget area.
This is a code example that is used for the genesis_widget_area function, typically found in a functions.php file in a WordPress Genesis child theme.
$widget_area_args = array('before' => genesis_markup($before_markup_args),
);
genesis_widget_area($id, $widget_area_args);
We can understand how this works by looking up the definition of the genesis_widget_area function in the /var/www/html/wp-content/themes/genesis/lib/functions/widgetize.php file.
function genesis_widget_area( $id, $args = array() ) {
if ( ! $id ) {
return false;
}
$defaults = apply_filters(
'genesis_widget_area_defaults',
array(
'before' => genesis_markup(
array(
'open' => '<aside class="widget-area">' . genesis_sidebar_title( $id ),
'context' => 'widget-area-wrap',
'echo' => false,
'params' => array(
'id' => $id,
),
)
),
'after' => genesis_markup(
array(
'close' => '</aside>',
'context' => 'widget-area-wrap',
'echo' => false,
)
),
'default' => '',
'show_inactive' => 0,
'before_sidebar_hook' => 'genesis_before_' . $id . '_widget_area',
'after_sidebar_hook' => 'genesis_after_' . $id . '_widget_area',
),
$id,
$args
);
$args = wp_parse_args( $args, $defaults );
if ( ! $args['show_inactive'] && ! is_active_sidebar( $id ) ) {
return false;
}

Related

Add woocommerce variation GTIN to structured data

If a webshop has variable products, in some cases the variations have unique GTINs. In our case, it is EAN.
How to add these different GTINS to the structured data? I made a function to insert multiple "offer" in "offers", but the GTIN is not recognized here.
This is the function:
//add structured data to the markup
function set_structured_data( $markup, $product ) {
if ($product->is_type('simple')){
$markup['brand'] = array('#type' => 'Brand', 'name' => get_post_meta( $product->get_id(), '_brand', true ) );
$markup['gtin8'] = get_post_meta( $product->get_id(), '_EAN', true );
}
if ($product->is_type('variable')){
$available_variations = $product->get_available_variations();
foreach ($available_variations as $variation) {
$variation_id = $variation['variation_id'];
$variproduct = wc_get_product($variation_id);
$i++;
$stock = $product->get_stock_status();
$offers[] = array(
'type' => 'Offer',
'price' => $variproduct->get_price(),
'priceValidUntil' => $priceuntil,
'priceSpecification' => array(
'price' => $variproduct->get_price(),
'priceCurrency' => get_woocommerce_currency(),
'valueAddedTaxIncluded' => 'http://schema.org/True'
),
'priceCurrency' => get_woocommerce_currency(),
'availability' => 'http://schema.org/'.$stock.'',
'url' => get_the_permalink(),
'seller' => array (
'type' => 'Organization',
'name' => 'HeatPerformance®',
'url' => get_the_permalink(),
));
}
$markup['offers'] = $offers;
$markup['brand'] = array('#type' => 'Brand', 'name' => get_post_meta( $product->get_id(), '_brand', true ) );
}
return $markup;
}
add_filter( 'woocommerce_structured_data_product', 'set_structured_data', 99, 2 );
Any ideas?
Ok after a couple of days puzzling i found the path to a solution here:
https://support.google.com/merchants/answer/6386198?hl=en
Result is that i corrected the standardized method from woocommerce and made each variation a unique product.
So first remove the markup in case of a variable product like this:
function set_structured_data( $markup, $product ) {
if ($product->is_type('variable')) {
$markup = array();
}
return $markup;
}
add_filter( 'woocommerce_structured_data_product', 'set_structured_data', 99, 2 );
And then build up the ld+json script again, but then the desired way:
function set_structured_data_variable() {
global $product;
if (isset($product)){
if ($product->is_type('variable') && !empty($product)){
$available_variations = $product->get_available_variations();
$date = date("Y-m-d",strtotime(" + 3months"));
$commenttext = '';
$commentauthor = '';
$commentdate = '';
$comments = get_comments(array( 'post_id' => $product->get_id(), 'number' => '1' ));
foreach($comments as $comment) {
$commenttext = $comment->comment_content;
$commentauthor = $comment->comment_author;
$commentdate = $comment->comment_date;
}
foreach ($available_variations as $variation) {
$variation_id = $variation['variation_id'];
$variproduct = wc_get_product($variation_id);
$i++;
$gtin = 0000000000001;
if (!empty(get_post_meta( $variation_id, '_EAN', true ))){
$gtin = get_post_meta( $variation_id, '_EAN', true );
}
$stock = $product->get_stock_status();
$arrays[$i] = array(
'#context' => 'https://schema.org/',
'#type' => 'Product',
'sku' => $variproduct->get_sku(),
'gtin13' => $gtin,
'image' => get_the_post_thumbnail_url($product->get_id()),
'name' => implode(" / ", $variproduct->get_variation_attributes()),
'description' => wp_strip_all_tags(get_the_excerpt($product->get_id())),
'brand' => array('#type' => 'Brand', 'name' => get_post_meta( $product->get_id(), '_brand', true ) ),
'review' => array(
'#type' => 'Review',
'reviewRating' => array (
'#type' => 'Rating',
'ratingValue' => $product->get_review_count(),
'bestRating' => '5',
'worstRating' => '1',
),
'author' => array(
'#type' => 'person',
'name' => $commentauthor,
'reviewBody' => $commenttext,
'datePublished' => $commentdate,
)),
'aggregateRating' => array (
'#type' => 'AggregateRating',
'ratingValue'=> $product->get_average_rating(),
'reviewCount' => $product->get_rating_count(),
),
'inProductGroupWithID' => $product->get_sku(),
'offers' => array(
'#type' => 'Offer',
'price' => $variproduct->get_price(),
'priceValidUntil' => $date,
'priceSpecification' => array(
'price' => $variproduct->get_price(),
'priceCurrency' => get_woocommerce_currency(),
'valueAddedTaxIncluded' => 'http://schema.org/True'
),
'priceCurrency' => get_woocommerce_currency(),
'availability' => 'http://schema.org/'.$stock.'',
'url' => get_the_permalink(),
'seller' => array (
'type' => 'Organization',
'name' => 'HeatPerformance®',
'url' => get_the_permalink(),
)));
}
echo '<script type="application/ld+json" class="buronoort">[';
$i = 0;
foreach ($arrays as $array){
$i++;
echo json_encode($array);
if ($i < array_key_last($arrays)){
echo ',';
}
}
echo ']</script>';
}
}
}
add_action('wp_head','set_structured_data_variable', 19);
Tested in the test tool for structured data and it is working.
A little concern is the product review however, i have to look into that what happens after multiple reviews.
Another concern is the EAN, i have set it standard to "0000000000001" as not all EANS are inputted yet. So this is a debatle solution, but if someone has a better idea, keep me posted.

Hide/display a link in BuddyPress toolbar based on user role

How do I hide/display a link in BuddyPress toolbar if the user has a particular role?
function your_bp_admin_bar_add() {
global $wp_admin_bar, $bp;
if ( !bp_use_wp_admin_bar() || defined( 'DOING_AJAX' ) )
return;
$user_domain = bp_loggedin_user_domain();
if (current_user_can('lp_teacher')) {
$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-create-course',
'title' => __( 'Create Course', 'your-plugin-domain' ),
'href' => trailingslashit( 'https://mywebsite.com/wp-admin/post-new.php?post_type=lp_course&tab=course_settings' ),
'meta' => array( 'class' => 'menupop' )
) );
}
$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-account-dogs',
'title' => __( 'Become an Instructor', 'your-plugin-domain' ),
'href' => trailingslashit( 'https://mywebsite.com/become-a-teacher-2/' ),
'meta' => array( 'class' => 'menupop' )
) );
}
add_action( 'bp_setup_admin_bar', 'your_bp_admin_bar_add', 300 );
I don't understand why it only displays Become an Instructor even when the logged-in user is an instructor. It is supposed to display Create Course.
I don't understand why it only displays Become an Instructor even when the logged-in user is an instructor.
Are you sure that the role / capability for instructors is lp_teacher?
If you want to only show Create Course to instructors, then try adding an else:
if (current_user_can('lp_teacher')) {
$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-create-course',
'title' => __( 'Create Course', 'your-plugin-domain' ),
'href' => trailingslashit( 'https://mywebsite.com/wp-admin/post-new.php?post_type=lp_course&tab=course_settings' ),
'meta' => array( 'class' => 'menupop' )
) );
} else {
$wp_admin_bar->add_menu( array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-account-dogs',
'title' => __( 'Become an Instructor', 'your-plugin-domain' ),
'href' => trailingslashit( 'https://mywebsite.com/become-a-teacher-2/' ),
'meta' => array( 'class' => 'menupop' )
) );
}

Wordpress / CMB2 Framework / Display data from repeatable fields in frontend

I have a own option-page in the backend-sidebar with this repeatable fields in it. (not a metabox) I cant figure out, how to display the data from the group field in the frontend.
function ww_register_theme_options() {
$ww_prefix = '_ww_';
$ww_contacts = new_cmb2_box( array(
'id' => $ww_prefix . 'ww_option_plugin',
'title' => esc_html__( 'Ansprechpartner', 'ww-contact' ),
'icon_url' => '/wp-content/plugins/ww-contact/assets/images/icons/ww-icon-white.png',
'object_types' => array( 'options-page' ),
'option_key' => 'ww_options',
) );
$ww_group_field = $ww_contacts->add_field(array(
'id' => $ww_prefix . 'contact_repeat_group',
'type' => 'group',
'description' => __('Ansprechparter Liste', 'ww-contact'),
'repeatable' => true,
'options' => array(
'group_title' => 'Ansprechpartner {#}',
'add_button' => __('neuer Ansprechpartner', 'ww-contact'),
'remove_button' => __('Entfernen', 'ww-contact'),
'sortable' => true,
),
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'forname',
'type' => 'text',
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'surname',
'type' => 'text',
));
}
Remove the prefix from your add_group_field ids they aren't needed. Then loop through like so...
$entries = get_post_meta( $parent, 'ww_contact_repeat_group', true );
if($entries){
foreach ( (array) $entries as $key => $entry ) {
if ( isset( $entry['forname'] ) ) {
$forname = $entry['forname'];
}
}
}

get_sub_field on repeater returns empty keys/values

EDIT: Updated with code which outputs the array
Should be noted I have other fields which work correctly, the issue is only with the repeater sub_fields.
// Needs to be updated to be more dynamic i know
function get_flexible_content_sections($name, $option = "") {
// check if the flexible content field has rows of data
if ( have_rows( $name, $option ) ):
// loop through the rows of data
while ( have_rows( $name, $option ) ) : the_row(); ?>
<section class="section <?= get_sub_field( "class" ) ?>">
<div class="section-content clear">
<?php while ( have_rows( "section" ) ) : the_row();
if ( get_row_layout() == 'one_width_cols' ) : ?>
<div class="full-width-col cols-12">
<?php while ( have_rows( "module_1" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<?php elseif ( get_row_layout() == 'two_width_cols' ) : ?>
<div class="half-width-col col-1 cols-6">
<?php while ( have_rows( "module_1" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="half-width-col col-2 cols-6 last">
<?php while ( have_rows( "module_2" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<?php elseif ( get_row_layout() == 'three_width_cols' ) : ?>
<div class="three-width-col col-1 cols-4">
<?php while ( have_rows( "module_1" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="three-width-col col-2 cols-4">
<?php while ( have_rows( "module_2" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="three-width-col col-3 cols-4 last">
<?php while ( have_rows( "module_3" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<?php elseif ( get_row_layout() == 'four_width_cols' ) : ?>
<div class="four-width-col col-1 cols-3">
<?php while ( have_rows( "module_1" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="four-width-col col-2 cols-3">
<?php while ( have_rows( "module_2" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="four-width-col col-3 cols-3">
<?php while ( have_rows( "module_3" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="four-width-col col-4 cols-3 last">
<?php while ( have_rows( "module_4" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<?php elseif ( get_row_layout() == 'five_width_cols' ) : ?>
<div class="five-width-col col-1 cols-2">
<?php while ( have_rows( "module_1" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="five-width-col col-2 cols-2">
<?php while ( have_rows( "module_2" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="five-width-col col-3 cols-2">
<?php while ( have_rows( "module_3" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="five-width-col col-4 cols-2">
<?php while ( have_rows( "module_4" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<div class="five-width-col col-5 cols-2 last">
<?php while ( have_rows( "module_5" ) ) : the_row();
get_layout_modules( get_row_layout() );
endwhile; ?>
</div>
<?php endif;
endwhile; ?>
</div> <!--end section-content-->
</section>
<?php endwhile;
else :
// no layouts found
endif;
}
function layout_module_slider() {
ob_start();
?><div class="layout-module-slider <?= get_sub_field('class'); ?>">
<?php
$files = get_sub_field('img_slider');
// pre is a function which wraps a value with print_r
pre($files); // <-- prints the "empty" array
if(count($files) > 0 ) {
echo slick_slider_func($files);
} else {
echo "No images available";
}
?>
</div><?php
return ob_get_clean();
}
ORIGINAL:
I have a nested repeater field inside layers of flexible contents. The purpose of which is to add images which are outputted to a slider.
The image ID's are stored in the database, but when I try to output them using get_sub_field, this is what i get:
I have build the field group programatically, which works in the backend as seen below. The backend equivalent for the above output:
The entire ACF settings/program:
if ( function_exists( 'acf_add_local_field_group' ) ):
add_action("init", "tryangle_generate_sections_fields");
function tryangle_generate_sections_fields() {
$indhold_gd = array(
'key' => 'group_indhold',
'title' => 'Indhold',
'fields' => array (),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
),
),
),
'position' => 'acf_after_title',
'menu_order' => 0,
);
generate_fields( "indhold224466", $indhold_gd );
}
endif;
function generate_fields($uniqid, $group_data)
{
if(empty($uniqid) || empty($group_data)) return;
// Sets a unique affix on all keys
$uiq = $uniqid;
// Sets the group_data with locations etc.
$group_data['key'] = $group_data['key'] . '_' . $uiq;
// Available modules
$modules = array(
array(
'key' => '5b2cefcfdea9da_'.$uiq,
'name' => 'text',
'label' => 'Text',
'display' => 'block',
'sub_fields' => array(
array(
'key' => 'field_5b2d13ddfb10ba_'.$uiq,
'label' => 'Text',
'name' => 'text',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 0,
),
),
'min' => '',
'max' => '',
),
array (
'key' => '5b3e2a167d2ee_'.$uiq,
'name' => 'image_slider',
'label' => 'Slider',
'display' => 'block',
'sub_fields' => array (
array (
'key' => 'field_5b3e2a3047a45_'.$uiq,
'label' => 'Slider',
'name' => 'img_slider',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => 'Tilføj billede',
'sub_fields' => array (
array (
'key' => 'field_5b3e2a3b47a46_'.$uiq,
'label' => 'Billede',
'name' => 'img',
'type' => 'image',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '20',
'class' => '',
'id' => '',
),
'return_format' => 'array',
'preview_size' => 'thumbnail',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
),
array (
'key' => 'field_5b3e2a5647a47_'.$uiq,
'label' => 'Billedetekst',
'name' => 'img_text',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 0,
),
),
),
),
'min' => '',
'max' => '',
),
array(
'key' => '5b2cf0bbbf1c9a_'.$uiq,
'name' => 'posts_slider',
'label' => 'Indlægsslider',
'display' => 'block',
'sub_fields' => array(
array(
'key' => 'field_5b2d13ddfb110a_'.$uiq,
'label' => 'Indlægskategori',
'name' => 'post_category',
'type' => 'taxonomy',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'taxonomy' => 'category',
'field_type' => 'select',
'allow_null' => 0,
'add_term' => 0,
'save_terms' => 0,
'load_terms' => 0,
'return_format' => 'object',
'multiple' => 0,
),
array(
'key' => 'field_5b2d13ddfb111a_'.$uiq,
'label' => 'Udseende',
'name' => 'style',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array(
'Slider' => 'Slider',
'Grid' => 'Grid',
),
'default_value' => array(),
'allow_null' => 0,
'multiple' => 0,
'ui' => 0,
'ajax' => 0,
'return_format' => 'value',
'placeholder' => '',
),
),
'min' => '',
'max' => '',
),
);
// Section columns
$columns = array(
array(
'key' => 'field_one_width_cols_'.$uiq,
'name' => 'one_width_cols',
'label' => 'En kolonne',
'display' => 'block',
'sub_fields' => array(
),
'min' => '',
'max' => '',
'cols' => 1,
),
array(
'key' => 'field_two_width_cols_'.$uiq,
'name' => 'two_width_cols',
'label' => 'To kolonner',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
'cols' => 2,
),
array(
'key' => 'field_three_width_cols_'.$uiq,
'name' => 'three_width_cols',
'label' => 'Tre kolonner',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
'cols' => 3,
),
array(
'key' => 'field_four_width_cols_'.$uiq,
'name' => 'four_width_cols',
'label' => 'Fire kolonner',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
'cols' => 4,
),
array(
'key' => 'field_five_width_cols_'.$uiq,
'name' => 'five_width_cols',
'label' => 'Fem kolonner',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
'cols' => 5,
)
);
/**
* Add columns to sections
*/
$loops = 1;
foreach ($columns as $key => $col)
{
// loop for the amount of columns available (5 columns)
for ($i = 1; $i <= $col['cols']; $i++) {
$width = 100 / $loops;
$columns[$key]['sub_fields'][] = array(
'key' => 'field_module_'.$i.'_'.$uiq,
'label' => 'Modul',
'name' => 'module_'.$i,
'type' => 'flexible_content',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => $width,
'class' => '',
'id' => '',
),
'min' => '',
'max' => '',
'button_label' => 'Tilføj modul',
'layouts' => $modules,
);
}
$loops++;
}
// Add field group "Indhold"
acf_add_local_field_group($group_data);
// Add sections to the "Indhold" field group
acf_add_local_field(array(
'parent' => 'group_indhold_'.$uiq,
'key' => 'field_sections_'.$uiq,
'label' => 'Sektioner',
'name' => 'sections',
'type' => 'flexible_content',
'button_label' => 'Tilføj sektion',
'layouts' => array(
array(
'key' => 'layout_section_'.$uiq,
'name' => 'section',
'label' => 'Sektion',
'display' => 'block',
'sub_fields' => array(
array(
'key' => 'field_section_'.$uiq,
'label' => 'Sektion',
'name' => 'section',
'type' => 'flexible_content',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'min' => '',
'max' => '',
'button_label' => 'Tilføj kolonner',
'layouts' => $columns,
),
array(
'key' => 'field_section_class_'.$uiq,
'label' => 'Sektion class',
'name' => 'class',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
),
),
));
}
Help is very much appreciated! I've been stuck with this the entire day now.
Thank you!
Ok, I'm not sure why it's empty:
pre($files); // <-- prints the "empty" array
However, you can replace this:
$files = get_sub_field('img_slider');
..with this: (manually build the data for $files)
$files = array();
if ( have_rows( 'img_slider' ) ) {
// Loop through the repeater's fields and manually fill in the `$files` array.
while ( have_rows( 'img_slider' ) ) : the_row();
if ( $image = get_sub_field( 'img' ) ) {
// These are just examples; just rename the keys, add more items, etc.
// it's all up to you.
$files[] = array(
'url' => $image['url'],
'text' => get_sub_field( 'img_text' ),
// ...
);
}
endwhile;
}
//var_dump( $files );
Additional Code
This loop may also help you, which I used to get to the slider items (i.e. image and its caption/text), starting from the sections field, which is the topmost-level field:
$post_id = false; // `false` defaults to the global `$post`
if ( have_rows( 'sections', $post_id ) ) :
while ( have_rows( 'sections', $post_id ) ) : the_row(); // loop #1
// With the current fields defined in `generate_fields()`, the 'class'
// is only available in loop #1.
$section_class = get_sub_field( 'class' );
echo '<div class="' . esc_attr( $section_class ) . '">'; // start the section
if ( have_rows( 'section' ) ) {
while ( have_rows( 'section' ) ) : the_row(); // loop #2
if ( have_rows( 'module_1' ) ) {
while ( have_rows( 'module_1' ) ) : the_row(); // loop #3
// In section #2 only.
the_sub_field( 'text' );
// In section #1 only.
if ( have_rows( 'img_slider' ) ) {
echo '<ul class="slider">';
while ( have_rows( 'img_slider' ) ) : the_row(); // loop #4
echo '<li class="slide-item">';
$image = get_sub_field( 'img' );
echo wp_get_attachment_image( $image['id'] );
the_sub_field( 'img_text' );
echo '</li>';
endwhile; // end loop #4
echo '</ul>';
}
endwhile; // end loop #3
}
endwhile; // end loop #2
}
echo '</div>'; // end section
endwhile; // end loop #1
endif;
Section #1 (image-slider)
Section #2 (lorem-ipsum)
I started over from scratch and redid all the fields in ACF, and added those programmatically carefully with uniquely generated keys (some of the keys were duplicates might have been the issue). The updated code now works thankfully.
Full code posted below (without output, should work with standard ACF docs).
I also added the crawler that replaces the field keys with unique keys.
Not sure where the fault lies in the original post, though this new code works and does entirely the same.
<?php
if ( function_exists( 'acf_add_local_field_group' ) ):
add_action("init", "tryangle_generate_sections_fields");
function tryangle_generate_sections_fields() {
$indhold_gd = array(
'key' => 'group_5b3f61bd82eba',
'title' => 'Indhold',
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
),
),
),
'position' => 'acf_after_title',
'menu_order' => 0,
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
);
generate_fields( "allpages", $indhold_gd );
}
endif;
/**
* Alters the ACF keys of the passed array, with the pass key affix
* #param $fields array Take ACF fields array as input.
* #param $randkey string String which is appended on the key
*
* #return array Returns passed array with altered keys
*/
function crawl_generate_keys($fields, $randkey) {
if(is_array($fields))
{
foreach ( $fields as $fk => $f )
{
if(!empty(is_array($fields[$fk]['sub_fields'])))
{
$fields[$fk]['sub_fields'] = crawl_generate_keys($fields[$fk]['sub_fields'], $randkey);
}
else if(!empty(is_array($fields[$fk]['layouts'])))
{
$fields[$fk]['layouts'] = crawl_generate_keys($fields[$fk]['layouts'], $randkey);
}
$fields[$fk]['key'] = $fields[$fk]['key'] . '_' . $randkey;
}
} else {
$fields['key'] = $fields['key'] . '_' . $randkey;
}
return $fields;
}
function generate_fields($uniqid, $gd)
{
// Declare the available layout modules
// The different layouts, such as slider, text field, statement slider
$layouts = array(
array (
'key' => '5b3f63d3bc38c_' . $uniqid,
'name' => 'text',
'label' => 'Text',
'display' => 'block',
'sub_fields' => array (
array (
'key' => 'field_5b3f641bb0588_' . $uniqid,
'label' => 'Text',
'name' => 'text',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 0,
),
),
'min' => '',
'max' => '',
),
array (
'key' => '5b3f6425b0589_' . $uniqid,
'name' => 'slider',
'label' => 'Slider',
'display' => 'block',
'sub_fields' => array (
array (
'key' => 'field_5b3f6428b058a_' . $uniqid,
'label' => 'Slider',
'name' => 'slider',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => 'Tilføj billede',
'sub_fields' => array (
array (
'key' => 'field_5b3f642fb058b_' . $uniqid,
'label' => 'Billede',
'name' => 'image',
'type' => 'image',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '20',
'class' => '',
'id' => '',
),
'return_format' => 'array',
'preview_size' => 'thumbnail',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
),
array (
'key' => 'field_5b3f6453b058c_' . $uniqid,
'label' => 'Billede tekst',
'name' => 'image_text',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 0,
),
),
),
array (
'key' => 'field_5b40ec12be700_' . $uniqid,
'label' => 'Slider class',
'name' => 'slider_class',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
'min' => '',
'max' => '',
),
array (
'key' => '5b3f64a5b058d_' . $uniqid,
'name' => 'statements',
'label' => 'Udtalelser',
'display' => 'block',
'sub_fields' => array (
array (
'key' => 'field_5b3f64b4b058e_' . $uniqid,
'label' => 'Udtalelser',
'name' => 'statements',
'type' => 'post_object',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'post_type' => array (
0 => 'udtalelser',
),
'taxonomy' => array (
),
'allow_null' => 0,
'multiple' => 1,
'return_format' => 'object',
'ui' => 1,
),
),
'min' => '',
'max' => '',
),
);
// Declare the avaialable columns
// The key indicates amount columns for that column
$columns = array (
1 => array (
'key' => '5b3f636009d7e1_' . $uniqid,
'name' => 'one_width_cols',
'label' => 'En kolonne',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
),
2 => array (
'key' => '5b3f636009d7e2_' . $uniqid,
'name' => 'two_width_cols',
'label' => 'To kolonner',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
),
3 => array (
'key' => '5b3f636009d7e3_' . $uniqid,
'name' => 'three_width_cols',
'label' => 'Tre kolonner',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
),
4 => array (
'key' => '5b3f636009d7e4_' . $uniqid,
'name' => 'four_width_cols',
'label' => 'Fire kolonner',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
),
5 => array (
'key' => '5b3f636009d7e5_' . $uniqid,
'name' => 'five_width_cols',
'label' => 'Fem kolonner',
'display' => 'block',
'sub_fields' => array(),
'min' => '',
'max' => '',
),
);
// Add layout modules to modules, add modules to columns
foreach ($columns as $k => $c)
{
// Loop for every module to be added to the column
for ($i = 1; $i <= $k; $i++)
{
$keyaffix = 'c' . $k . '_m' . $i;
$wrapperWidth = 100 / $k;
$insLayouts = crawl_generate_keys($layouts, $keyaffix);
$columns[$k]['sub_fields'][] = array(
'key' => 'field_5b3f63c1b0586_' . $keyaffix . '_' . $uniqid,
'label' => 'Modul',
'name' => 'module_' . $i,
'type' => 'flexible_content',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => $wrapperWidth,
'class' => '',
'id' => '',
),
'button_label' => 'Tilføj modul',
'min' => '',
'max' => '',
'layouts' => $insLayouts,
);
}
}
acf_add_local_field_group(array (
'key' => $gd['key'] . '_'. $uniqid,
'title' => $gd['title'],
'fields' => array (
array (
'key' => 'field_5b3f61c74f5b2_' . $uniqid,
'label' => 'Sektioner',
'name' => 'sections',
'type' => 'flexible_content',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'button_label' => 'Tilføj sektion',
'min' => '',
'max' => '',
'layouts' => array (
array (
'key' => '5b3f61ce24ec5_' . $uniqid,
'name' => 'section',
'label' => 'Sektion',
'display' => 'block',
'sub_fields' => array (
array (
'key' => 'field_5b3f6359b0585_' . $uniqid,
'label' => 'Sektion',
'name' => 'section',
'type' => 'flexible_content',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'button_label' => 'Tilføj kolonner',
'min' => '',
'max' => '',
'layouts' => $columns,
),
),
'min' => '',
'max' => '',
),
),
),
),
'location' => $gd['location'],
'menu_order' => $gd['menu_order'],
'position' => $gd['position'],
'style' => $gd['style'],
'label_placement' => $gd['label_placement'],
'instruction_placement' => $gd['instruction_placement'],
'hide_on_screen' => $gd['hide_on_screen'],
'active' => $gd['active'],
'description' => $gd['description'],
));
}

Can not create a table - Drupal 7

I am trying to create a table inside one of my forms, but I am having some errors (Notice : Undefined offset: X in theme_tableselect() ) and the content of my table is then wrong.
This is my code if someone could tell me what I miss :
<?php
$headers = array(
t('Nom'),
t('Description'),
t('Nombre vidéos'),
t('Durée'),
t('Mode de lecture'),
t('Date'),
t('Actions'),
);
$form["gestionvideos_array"] = array(
'#type' => 'fieldset',
'#title' => t('Playlists'),
'#description' => t('Something'),
);
foreach( $aPlaylist as $oPlaylist ){
$rows = array(
'Nom' => ucfirst($oPlaylist->sPlaylistName),
'Description' => $oPlaylist->sDescription,
'Nombre vidéos' => $oPlaylist->iTotal,
'Durée' => $oPlaylist->sDuree,
'Mode de lecture' => $oPlaylist->sMode,
'Date' => $oPlaylist->dCreated,
'Actions' => $oPlaylist->sAction,
);
}
$form["gestionvideos_array"]["table"] = array(
'#type' => 'tableselect',
'#header' => $headers,
'#options' => $rows,
);
?>

Resources