php - Code broken in a particular order -


i have been designer trade years working wordpress client , have been managing working hooks... am, @ standstill.

on homepage have 2 areas pulling information database:

<?php      $thumb_posts = get_posts(array('category' => '6', 'orderby' => 'rand', 'numberposts' => 2, 'meta_key' => '_thumbnail_id' )); if($thumb_posts) { ?> <?php foreach( $thumb_posts $post ) { echo '<div class="feature"><div class="thumbnail"> <a href="' . get_permalink($header_thumb->id) . '">' . get_the_post_thumbnail($header_thumb->id,array(240,170)) . '</a></div>'; $url = get_permalink($post->id); $filecontent = file_get_contents('https://graph.facebook.com/?ids=' . $url); $json = json_decode($filecontent); $count = $json->$url->comments; if ($count == 0 || !isset($count)) { $count = '0'; } elseif ( $count == 1 ) { $count = '1'; } else { $count .= ''; } echo '<h3 class="title"> <a href="' . get_permalink($header_thumb->id) . '#comments"     class="comments">' . $count . '</a> <a href="' . get_permalink($header_thumb->id) . '"> ' . get_the_title($id) . ' </a> </h3></div>'; } ?> 

which did not write, calls facebook comment count of random post title , thumbnail.

here problem... once place second code after throws off count/image retrieval. however, if place code before works great.

<?php  $thumb_posts = get_posts(array('category' => '6', 'orderby' => 'rand', 'numberposts' => 2, 'meta_key' => '_thumbnail_id' )); if($thumb_posts) {  ?>  <?php  global $post; $myposts = get_posts('numberposts=20'); foreach($myposts $post) : ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> |  <span class="post-info"> <?php echo human_time_diff( get_the_time('u'), current_time('timestamp') ) . ' ago'; ?> </span></li> <?php endforeach; ?> 

in thought process, need somehow make second code doesn't somehow throw off first independent code.

it not "pulling same parameters" due set of lines:

global $post; $myposts = get_posts('numberposts=20'); foreach($myposts $post) : 

in order:

  1. by doing global $post, hoisting reference global variable $post set wordpress scope. $post represents current post.
  2. you number of posts wp , set $myposts. fine.
  3. what not fine, however, you're looping on $myposts unique element $post. last assignment causes wordpress's global $post reset , corrupts rest.

consider doing instead:

<?php  global $post; $myposts = get_posts('numberposts=20'); foreach($myposts $current_post) :  ?> 

this not modify global scope @ all, wordpress saves few things.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -