Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to get an attachment from a post in a Wordpress loop but getting all, from all posts
    primarykey
    data
    text
    <p>First of all - this is a question mainly about <strong>the loop</strong>™. I'm also having problems with the attachment thing, but that is somehow secondary since there are snippets around there I think could be useful.</p> <p>Ok, so this is want I want to do in a front page:</p> <ul> <li>get 7 posts from custom post type 'portfolio'</li> <li>only with the first one, get an specific attachment (in any possible way, be it filename, order in the media manager... whatever is the best, more clean way)</li> <li>with the other 6, just get the_post_thumbnail() and little more.</li> </ul> <p>Now, I have this:</p> <pre><code>&lt;?php $args = array ( 'post_type' =&gt; 'portfolio', 'order' =&gt; 'DESC', 'posts_per_page' =&gt; 7 ); $query = new WP_Query( $args ); $first_post = true; /* we will take the latest work first */ ?&gt; &lt;?php if ( $query-&gt;have_posts() ) : ?&gt; &lt;?php while ( $query-&gt;have_posts() ) : $query-&gt;the_post(); ?&gt; &lt;?php if ($first_post): $first_post = false; ?&gt; &lt;div&gt; /* This is the div for the first item */ &lt;?php /* let's take the first attachment */ $args = array( 'post_type' =&gt; 'attachment', 'numberposts' =&gt; 1, 'order' =&gt; 'DESC' ); $attachments = get_posts( $args ); if ( $attachments ) : foreach ( $attachments as $attachment ) : echo wp_get_attachment_image( $attachment-&gt;ID, 'full' ); endforeach; endif; ?&gt; &lt;/div&gt; &lt;?php else: ?&gt; /* Do something with the other 6 posts and their post_thumbnail */ &lt;?php endif ?&gt; &lt;?php endwhile; ?&gt; </code></pre> <p>And now for the questions:</p> <ul> <li><p>First and foremost: if I set 'numberposts' to all (-1) when trying to recover the attachment, I get ALL attachments from ALL 'portfolio' posts. Shouldn't I be interacting only with the current post (the_post())? I can't quite grasp the concept of the loop here, this is the main question.</p></li> <li><p>That code won't get me the first attachment, even if it's placed in the first place in that post's media manager.</p></li> </ul> <p>Should I go for secondary or nested loops? I've read and re-read the codex and other tutorials but still can't wrap my head around it.</p> <p>Many thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload