Note that there are some explanatory texts on larger screens.

plurals
  1. POGet a variable from one wordpress template file to another
    text
    copied!<p>I have a template file (<strong>trendingPosts.php</strong>) for showing 2 latest posts with the tag 'trending'. In the while loop for displaying these 2 posts, I take their ID's in an array so I can exclude them from the main wordpress loop later:</p> <pre><code>&lt;div id="trendingWrap" class="clearfix"&gt; &lt;?php $trending = new WP_Query(); $trending-&gt;query("showposts=2&amp;tag=trending"); while($trending-&gt;have_posts()) : $trending-&gt;the_post(); $wp_query-&gt;in_the_loop = true; $currentTrending[] = $post-&gt;ID; ?&gt; &lt;div class="trendingStory"&gt; &lt;h2 class="trendingTitle"&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;" alt="&lt;?php the_title_attribute(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h2&gt; &lt;/div&gt;&lt;!-- end trendingStory --&gt; &lt;?php endwhile; ?&gt; &lt;/div&gt;&lt;!-- end trendingWrap --&gt; </code></pre> <p>The problem is that I have an <strong>index.php</strong> in which I include the <strong>loop.php</strong> via <code>get_template_part( 'loop', 'index' );</code> and I am unable to get the <code>$currentTrending[]</code> array that I made in <strong>trendingPosts.php</strong>. <strong>I need to get that array in my <em>loop.php</em></strong></p> <p>Moreover, in my <strong>loop.php</strong>, I am excluding the 2 posts in the following way.</p> <pre><code>if(have_posts()): while(have_posts()) : the_post(); if( $post-&gt;ID == $currentTrending[0] || $post-&gt;ID == $currentTrending[1] ) continue; </code></pre> <p>Is this the right way to exclude posts? if anybody has a better way of doing this whole thing. Please let me know. <strong>Of course nothing works until I manage to get that array in <em>loop.php</em> so that is the main issue.</strong></p> <p>Thanks! I appreciate all the help.</p>
 

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