0

I wanna make a portfolio on-click opening images gallery using meta fields. So firstly I make array of portfolio images and loop them. Then I loop gallery images of every portfolio item, but it loops the same data, it's look like not saving properly. How to save properly so that every portfolio item will have own gallery images? Any help will be appreciated

Here my code:

for ($i = 0; $i < $portfolio_item_no; $i++ ){
    $html = "<img id='m_img_preview_m_img_$i' src='"  .$pi_main_img[$i] . "' alt='" . $pi_main_img_alt[$i] . "' />
    <input type='number' name='pi_slide_no[]' id='slideNo-$i' class='form-control' value='" . $pi_slide_no[$i] . "' />";

    for ($i2 = 0; $i2 < $pi_slide_no[$i]; $i2++ ){
        $html .= "<img id='slide_m_img_sld-$i-$i2' src='" . $pi_slide_img_link[$i2] . "' />
         <input type='hidden' id='slide_link_m_img_sld-$i-$i2' name='pi_slide_img_link[]' value='" . $pi_slide_img_link[$i2] . "' />";
    }
}

// To call
$pi_main_img = get_post_meta($post->ID, "_pim_main_img2", true);
$pi_main_img = ($pi_main_img != '') ? json_decode($pi_main_img) : array();
// ...

// To save
if ( isset( $_REQUEST['pi_main_img'] ) ) {
$pi_main_img = (isset($_POST['pi_main_img']) ? $_POST['pi_main_img'] : '');
$pi_main_img = strip_tags(json_encode($pi_main_img, JSON_UNESCAPED_UNICODE));
update_post_meta($post_id, "_pim_main_img2", $pi_main_img); 
}

if ( isset( $_REQUEST['pi_slide_img_link'] ) ) {
$pi_slide_img_link = (isset($_POST['pi_slide_img_link']) ? $_POST['pi_slide_img_link'] : '');
$pi_slide_img_link = strip_tags(json_encode($pi_slide_img_link, JSON_UNESCAPED_UNICODE));
update_post_meta($post_id, "_pim_slide_img_link2", $pi_slide_img_link); 
}
  • first thing that bugs me, is that you create a new variable $counterXC & $slideNo while you have $i & $i2. what happens, when you replace $counterXC & $slideNo with your $is and get rid of the $counterXC++ & $slideNo++ completely..? – honk31 Jan 22 at 10:13
  • @honks31 ok, then? :) – Marat Jan 22 at 10:21
  • so what are you doing with $html? the way i see it, you overwrite it with each iteration of your first for loop and than you do nothing with it..? maybe place $html = ''in front of everything and than in the first for loop replace $html = with $html .=..? – honk31 Jan 22 at 10:36
  • @honk31 its already displaying content, outer loop works ok, but inner loop saves one data for all, it loop same object every time. Problem with saving inner loop items – Marat Jan 22 at 11:53
  • @honk31 any idea? – Marat Jan 23 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.