0

I use a foreach to print out an attachment_image from a blog post. But for some reason a button duplicates the amount of posts and I dont know what I am doing wrong.

I am using sage 10 with bootstrap 5:

@php
   $images =& get_children( array (
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'post_mime_type' => 'image'
));
@endphp
<section class="masonry-grid">
  <div class="container masonry__container">
    <div class="masonry__items">
      <div class="item masonry__item">
        <div class="masonry__images">
          @if ( empty($images) )
          @else
          @foreach ( array_unique($images) as $attachment_id => $attachment )
          <div class="masonry__image">
            {!! wp_get_attachment_image( $attachment_id, false ) !!}
          </div>
          @endforeach
          @endif
        </div>
        <div class="masonry__info">
          <div class="masonry__category">
            {{ get_the_category( $id )[0]->name }}
          </div>
          <div class="masonry__title">
            <a href="{{ get_permalink() }}">
              {!! $title !!}
            </a>
          </div>
        </div>
        <div class="masonry__socials">
          <div class="masonry__social">
            <div classs="masonry__authors">
              <i class="fas fa-at"></i><span class="masonry__author">@include('partials/entry-meta')</span>
            </div>
            <div class="masonry__views">
              <i class="fas fa-eye"></i><span class="masonry__author">149 views</span>
            </div>
            <div class="masonry__likes">
              <i class="far fa-thumbs-up"></i><span class="masonry__author">8 likes</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="masonry__buttons">
      <div class="masonry__loadmore">
        <button type="button" class="masonry__button btn btn-outline-secondary"><i class="fas fa-spinner"></i>Load more</button>
      </div>
    </div>
  </div>
</section>

I intend to display multiple items and a single button, yet, instead of the behavior I expect, as many buttons are displayed as many items there are.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
dennis
  • 3
  • 1
  • 3
  • Can you add more information of how your problem manifests? – Lajos Arpad May 20 '21 at 17:47
  • so in Wordpress i have created multiple blog items in the dashboard. that is showing correctly. The idea is to have around 10 items and if there is more you can press on the button to show more. ( that comes later) Every blog item has an preview image, so i made a foreach loop to print the image. The image is showing correctly. But for some reason the buttons shows under each blog item. So i have 10 blog items and 10 buttons. I hope this information helps? – dennis May 25 '21 at 07:16
  • I think I understand the problem statement. If so, then I will probably answer. Is this describing your problem accurately? "I intend to display multiple items and a single button, yet, instead of the behavior I expect, as many buttons are displayed as many items there are" – Lajos Arpad May 25 '21 at 15:09
  • That is correct. Sorry for mine bad explanation. Maybe also good to know, i have been working in partial/content.blade.php – dennis May 26 '21 at 11:19
  • Dennis, I still need some information: what does `{!! wp_get_attachment_image( $attachment_id, false ) !!}` return? – Lajos Arpad May 26 '21 at 20:17
  • That shows the featured image from a blog item. So in code: https://developer.wordpress.org/reference/functions/wp_get_attachment_image/ – dennis May 27 '21 at 08:11
  • Based on the code you have in the question, the cycle only contains a div, into which the response of ```wp_get_attachment_image``` is being generated. Let's perform another experiment: if you temporarily remove all the content from the foreach, do you still see the button being replicated many times? – Lajos Arpad May 27 '21 at 09:00
  • hmmm, i have removed the foreach and the button is still duplicated. So the foreach isn't the problem?! I think i know what's happening. The button has been placed in content.blade.php( that duplicates the button), isn't it better if i make a template and include the button to the template? – dennis May 27 '21 at 12:27
  • Dennis, there must be a loop involved somewhere which includes the template and the button. You will need to remove the button from the template and create a separate template, included outside the loop. You will need to debug until you find out which loop is responsible for duplicating the button. You were correct from the start that the button is duplicated by a loop. The misunderstanding on your part was that you assumed some other loop of causing this issue than the actual "guilty" loop. – Lajos Arpad May 28 '21 at 12:48
  • Thank you for your patience and support! I think what to do now! – dennis May 31 '21 at 07:48

0 Answers0