Nested group php call
- This topic has 5 replies, 2 voices, and was last updated 8 years, 7 months ago by
sergeysemenov.
-
AuthorPosts
-
October 22, 2016 at 1:08 PM #4354
sergeysemenov
ParticipantHi!
I can't figure out how to display my nested groups in the frontend.
I'm average in php.
Could you provide me the final php code to display this in the frontend with my params?
I hope your answer would be useful to all 'average php' users like me.
Thanks in advance...Here is my functions.php
// Group array( 'id' => "{$prefix}sector", 'type' => 'group', 'clone' => true, 'sort_clone' => false, 'fields' => array( array( 'name' => __( 'Name1', 'rw_' ), 'id' => "{$prefix}sector-heading", 'type' => 'heading', ), array( 'name' => __( 'Name2', 'rw_' ), 'id' => "{$prefix}sector-title", 'type' => 'textarea', 'cols' => 10, 'rows' => 1, ), // Group nested array( 'id' => "{$prefix}sector-object", 'type' => 'group', 'clone' => true, 'sort_clone' => true, 'fields' => array( array( 'name' => __( 'Name3', 'rw_' ), 'id' => "{$prefix}sector-object-img", 'type' => 'image_advanced', 'max_file_uploads' => 1, ), array( 'name' => __( 'Name4', 'rw_' ), 'id' => "{$prefix}sector-object-description", 'type' => 'textarea', 'cols' => 10, 'rows' => 4, ), ), ), ), ), ) );
October 24, 2016 at 2:58 PM #4364Anh Tran
KeymasterNo problem, here you are:
<?php $prefix = ''; $sectors = rwmb_meta( "{$prefix}sector" ); if ( ! empty( $sectors ) ) { foreach ( $sectors as $sector ) { $sector_heading = isset( $sector["{$prefix}sector-heading"] ) ? $sector["{$prefix}sector-heading"] : ''; echo $sector_heading; $sector_title = isset( $sector["{$prefix}sector-title"] ) ? $sector["{$prefix}sector-title"] : ''; echo $sector_title; $objects = isset( $sector["{$prefix}sector-object"] ) ? $sector["{$prefix}sector-object"] : array(); foreach ( $objects as $object ) { $object_img = isset( $object["{$prefix}sector-object-img"] ) ? $object["{$prefix}sector-object-img"] : ''; if ( $object_img ) { echo '<img src="' . wp_get_attachment_image_url( $object_img, 'size' ) . '">'; } $object_desc = isset( $object["{$prefix}sector-object-description"] ) ? $object["{$prefix}sector-object-description"] : ''; echo $object_desc; } } }
October 24, 2016 at 3:43 PM #4367sergeysemenov
ParticipantThanks for assistance, Anh Tran.
One more question.
What if my field is in nested group, has 10 images uploaded and is used as a slider in the frontend? How do I call these images?array( 'name' => __( 'Name3', 'rw_' ), 'id' => "{$prefix}sector-object-img", 'type' => 'image_advanced', 'max_file_uploads' => 10, ),
October 24, 2016 at 6:28 PM #4370sergeysemenov
ParticipantI suppose there's something wrong with image_advanced field in nested groups.
I tried to exclude nested group, so for now my metabox looks like:// Group array( 'name' => 'Сектор инфраструктуры', 'id' => "{$prefix}sector", 'type' => 'group', 'clone' => true, 'sort_clone' => false, 'fields' => array( array( 'name' => __( 'Название сектора', 'rw_' ), 'id' => "{$prefix}sector-title", 'type' => 'textarea', 'cols' => 10, 'rows' => 1, ), array( 'name' => __( 'Описание сектора', 'rw_' ), 'id' => "{$prefix}sector-description", 'type' => 'textarea', 'cols' => 10, 'rows' => 4, ), array( 'name' => __( 'Изображение объекта', 'rw_' ), 'id' => "{$prefix}sector-object-image", 'type' => 'image_advanced', 'max_file_uploads' => 10, ), ), ),
And I call it in frontend
<?php $sectors = rwmb_meta( 'rw_sector' ); if ( ! empty( $sectors ) ) { foreach ( $sectors as $sector ) { echo '<div class="object-block"> <div class="object"> <div class="sector-object-text">'; $title = isset( $sector['rw_sector-title'] ) ? $sector['rw_sector-title'] : ''; echo '<h3>' . $title . '</h3>'; $description = isset( $sector['rw_sector-description'] ) ? $sector['rw_sector-description'] : ''; echo '<p>' .$description.'</p>'; echo '</div>'; $object_img = isset( $sector['rw_sector-object-image'] ) ? $sector['rw_sector-object-image'] : ''; if ( $object_img ) { echo '<div class="sector-object-img"> <div class="slider slider-for"> <img src="' . wp_get_attachment_image_url( $object_img, 'type=image_advanced&size=full&multiple=true' ) . '"> </div> </div>'; } echo '</div> </div>'; } } ?>
Title and description are displayed, but no images.
I even tried with'max_file_uploads' => 1,
but no luckOctober 25, 2016 at 11:01 AM #4378Anh Tran
KeymasterAh, sorry, for image fields, the returned value is always an array. You need to change the code to:
$object_imgs = isset( $sector['rw_sector-object-image'] ) ? $sector['rw_sector-object-image'] : array(); if ( ! empty( $object_imgs ) ) { foreach ( $object_imgs as $object_img ) { echo '<div class="sector-object-img"> <div class="slider slider-for"> <img src="' . wp_get_attachment_image_url( $object_img, 'type=image_advanced&size=full&multiple=true' ) . '"> </div> </div>'; } }
October 25, 2016 at 11:59 PM #4383sergeysemenov
ParticipantThanks, Anh Tran! It's all done! Topic closed
-
AuthorPosts
- The topic ‘Nested group php call’ is closed to new replies.