按特定顺序破解的代码


Code broken in a particular order

多年来我一直是一名设计师,但是我一直在为客户使用WordPress,并且一直在设法通过使用hooks…然而,我现在处于停滞状态。

在主页上,我有两个区域将从数据库中提取信息:

<?php 
    $thumb_posts = get_posts(array('category' => '6', 'orderby' => 'rand', 'numberposts' => 2, 'meta_key' => '_thumbnail_id' ));
if($thumb_posts) { ?>
<?php foreach( $thumb_posts as $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>';
} ?>

这不是我写的,而是调用带有标题和缩略图的随机帖子的facebook评论数。

我的问题是……一旦我把我的第二个代码后,它抛出计数/图像检索。然而,如果我把这段代码放在前面,一切都很好。
<?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 as $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; ?>

在我的思考过程中,我需要以某种方式使第二个代码不会以某种方式脱离第一个独立的代码。

由于这组行,它不是"从相同的参数中提取":

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

:

  1. 通过做global $post,你正在提升对Wordpress设置的全局变量$post的引用到你的作用域。$post为当前岗位
  2. 然后你从WP得到一些帖子,并将其设置为$myposts
  3. 什么是NOT好,然而,是你然后循环$myposts作为唯一的元素$post。最后一个赋值会导致Wordpress的全局$post被重置,并破坏所有其他的。

考虑这样做:

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

这根本不修改全局作用域,这是Wordpress保存一些东西的地方。