Support

Account

Home Forums General Issues One field inside of while loop show the same content for every entry

Unread

One field inside of while loop show the same content for every entry

  • Hello, I am writing a cpt with acf, and one of the fields, show the exact same text for every post in the CPT (the first one). If I set it as draft it moved to the second.
    The strange part is that every other field in the loop works perfectly, only this one is problematic. The field is ‘testimonial_text’;

    Here is my code:

    // Testimonial Slider Shortcode
    function testimonial_slider_shortcode($atts) {
    $atts = shortcode_atts( array(
    'category' => '',
    ), $atts, 'testimonial-slider' );
    $args = array(
    'post_type' => 'testimonials',
    'posts_per_page' => -1,
    'tax_query' => array(

    array(
    'taxonomy' => 'testimonial_category',
    'field' => 'slug',
    'terms' => $atts['category'],
    'operator' => 'AND'
    )

    )
    );

    $testimonials = new WP_Query($args);
    $output = '<div id="testimonial-slider" class="carousel slide full-width" data-ride="carousel">';
    $output .= '

      ';

      $i = 0;
      while($testimonials->have_posts()) {

      $testimonials->the_post();
      $output .= '<li data-target="#testimonial-slider" data-slide-to="'.$i.'" class="'.($i == 0 ? 'active' : '').'">';
      $i++;
      }

      wp_reset_postdata();

      $output .= '

    <div class="carousel-inner">';

    $i = 0;
    while($testimonials->have_posts()) {
    $testimonials->the_post();
    $post = get_post();
    $person_name = get_field('person_name');
    $testimonial_text = get_field('testimonial_text');
    $company_text = get_field('person_company');
    $person_photo = get_field('person_photo');
    $person_photo_url = $person_photo['sizes']['large'];

    $output .= '<div class="item '.($i == 0 ? 'active' : '').'">';
    $output .= '<div class="inner-testimonial">';

    $output .= '<div class="carousel-caption">';
    $output .= '<div class="block-quote-img">';
    $output .= '';
    $output .= 'test';
    $output .= '<span class="author-text" maxlength="2">' .$testimonial_text. '</span>';

    $output .= '</div>';
    $output .= '<p class="author">' .' - ' .$person_name.' , '.$company_text.'</p>';
    $output .= '</div>';
    $output .= '<div class="img-div">';
    $output .= ''.$person_name.'';
    $output .= '</div></div></div>';
    $i++;
    }

    wp_reset_postdata();

    $output .= '</div><span class="glyphicon glyphicon-chevron-left"></span><span class="glyphicon glyphicon-chevron-right"></span></div>';

    return $output;
    }
    add_shortcode('testimonial-slider', 'testimonial_slider_shortcode');

    // Enqueue styles and scripts
    // Global plugin styles should be added to testimonails slider.css
    // Per project styles should be added to testimonial-project.css
    function testimonial_slider_scripts() {
    wp_enqueue_style( 'testimonial-slider-style', plugins_url( '/testimonial-slider.css', __FILE__ ) );
    wp_enqueue_style( 'testimonial-project-style', plugins_url( '/testimonial-project.css', __FILE__ ) );
    // wp_enqueue_style('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css');
    // wp_enqueue_script(
    // 'testimonial-block-script',
    // plugins_url( 'blocks/testimonial-block.js', __FILE__ ),
    // array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components' )
    // );

    }

    add_action( 'wp_enqueue_scripts', 'testimonial_slider_scripts' );

    // Register Gutenberg Block

    add_action('init', 'wp_testimonial_block_init');

    function wp_testimonial_block_init() {

    register_block_type(__DIR__ . '/build', array(
    'render_callback' => 'theHTML'
    ));

    }

    function theHTML($atts) {
    $args = array(
    'post_type' => 'testimonials',
    'posts_per_page' => -1,
    'tax_query' => array(

    array(
    'taxonomy' => 'testimonial_category',
    'field' => 'slug',
    'terms' => $atts,
    'operator' => 'IN'
    )

    )
    );

    $testimonials = new WP_Query($args);
    $output = '<div id="testimonial-slider" class="carousel slide full-width" data-ride="carousel">';
    $output .= '

      ';

      $i = 0;
      while($testimonials->have_posts()) {

      $testimonials->the_post();
      $output .= '<li data-target="#testimonial-slider" data-slide-to="'.$i.'" class="'.($i == 0 ? 'active' : '').'">';
      $i++;
      }

      wp_reset_postdata();

      $output .= '

    <div class="carousel-inner">';

    $i = 0;
    while($testimonials->have_posts()) {
    $testimonials->the_post();
    $post = get_post();
    $person_name = get_field('person_name');
    $testimonial_text = get_field('testimonial_text');
    $company_text = get_field('person_company');
    $person_photo = get_field('person_photo');
    $person_photo_url = $person_photo['sizes']['large'];
    $output .= '<div class="item '.($i == 0 ? 'active' : '').'">';
    $output .= '<div class="inner-testimonial">';

    $output .= '<div class="carousel-caption">';
    $output .= '<div class="block-quote-img">';
    $output .= '';
    $output .= '<span class="author-text" maxlength="20">' .$testimonial_text. '</span>';

    $output .= '</div>';
    $output .= '<p class="author">' .' - ' .$person_name.' , '.$company_text.'</p>';
    $output .= '</div>';
    $output .= '<div class="img-div">';
    $output .= ''.$person_name.'';
    $output .= '</div></div></div>';

    $i++;
    }

    wp_reset_postdata();

    $output .= '</div><span class="glyphicon glyphicon-chevron-left"></span><span class="glyphicon glyphicon-chevron-right"></span></div>';

    return $output;
    }

    This is the code for the acf register

    // Add Custom Fields
    function add_testimonial_custom_fields() {
    if(function_exists('acf_add_local_field_group')):

    acf_add_local_field_group(array(
    'key' => 'group_testimonial',
    'title' => 'Testimonial Fields',
    'fields' => array(
    array(
    'key' => 'field_person_name',
    'label' => 'Person Name',
    'name' => 'person_name',
    'type' => 'text',
    'required' => true,
    'show_in_rest' => true,
    'wrapper' => array(
    'width' => '50',
    ),
    ),
    array(
    'key' => 'field_testimonial_text',
    'label' => 'Testimonial Text',
    'name' => 'testimonial_text',
    'type' => 'wysiwyg',
    'required' => true,
    'show_in_rest' => true,
    'wrapper' => array(
    'width' => '50',
    ),
    ),
    array(
    'key' => 'field_person_photo',
    'label' => 'Person Photo',
    'name' => 'person_photo',
    'type' => 'image',
    'preview_size' => 'full',
    'required' => false,
    'show_in_rest' => true,
    ),
    array(
    'key' => 'field_person_company',
    'label' => 'Company',
    'name' => 'person_company',
    'type' => 'text',
    'required' => false,
    'show_in_rest' => true,
    ),
    ),
    'location' => array(
    array(
    array(
    'param' => 'post_type',
    'operator' => '==',
    'value' => 'testimonials',
    ),
    ),
    ),
    ));

    endif;
    }
    add_action('acf/init', 'add_testimonial_custom_fields');

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.