显示上周最受欢迎的帖子

时间:2020-04-12 19:05:35

标签: php wordpress

我无法获得此流行的邮政编码以正确输出,而是在我的foreach上不断获取无效的参数,这些参数仅应从数组中获取信息并显示流行的帖子。但是我不确定为什么它的foreach循环无效,因为我有一个类似的设置来显示最近的帖子。

以下是热门帖子的代码:

<?php

  $popular_post_args = array(
    'meta_key'  => 'post_views_count',
    'orderby'    => 'meta_value_num',
    'order'      => 'DESC',
    'posts_per_page' => 10,
    'date_query' => array(
        array(
            'after' => '1 week ago',
        ),
    ),
  );
  foreach( $popular_posts_args as $p ){
  ?>

   <div class="paddingarea text-dark">

     <div class="the-image" style="margin-top: -20px;">
         <a href="<?php echo get_permalink($p['ID']) ?>"><img src="<?php echo get_the_post_thumbnail_url($p['ID'], array(440, 240)); ?>"/></a>
     </div>

   <a class="posttitle" href="<?php echo get_permalink($p['ID']) ?>" style="font-weight: 600; font-size: 16px;"><span href="#" class="badge badge-primary" style="border-radius: 0;"><?php
   foreach(get_the_category($p['ID']) as $category) {
   echo $category->name;
 }
 ?></span>
 <?php echo $p['post_title']?></a><br />
   </div>

   <?php
   $authorname = get_the_author();

   echo '<p class="authortext">From ' . '<strong class="colorauthor">' . $authorname . '</strong>' . '</p>';

    ?>


   <?php
  }
 ?>

这是我最近的帖子的代码:

<?php

$result = wp_get_recent_posts(array(
'numberposts' => 10,
'category' => '',
'post_status' => 'publish',
));

foreach( $result as $p ){
?>

<div class="paddingarea text-dark">

<div class="the-image" style="margin-top: -20px;">
    <a href="<?php echo get_permalink($p['ID']) ?>"><img src="<?php echo get_the_post_thumbnail_url($p['ID'], array(440, 240)); ?>"/></a>
</div>

<a class="posttitle" href="<?php echo get_permalink($p['ID']) ?>" style="font-weight: 600; font-size: 16px;"><span href="#" class="badge badge-primary" style="border-radius: 0;"><?php
foreach(get_the_category($p['ID']) as $category) {
echo $category->name;
}
?></span>
<?php echo $p['post_title']?></a><br />
</div>

<?php
$authorname = get_the_author();

echo '<p class="authortext">From ' . '<strong class="colorauthor">' . $authorname . '</strong>' . '</p>';

?>


<?php
}
?>

如您所见,它的代码基本相同,只是重新用于显示受欢迎的帖子,但是什么也没有显示,并且最上面的代码只是给出了无效的foreach循环错误。所以我不确定为什么要给出这些,因为不是同一查询被数组拉出吗?

1 个答案:

答案 0 :(得分:0)

您好像有错字。您的变量定义为$popular_post_args。在您的foreach中,您使用的是$popular_posts_args,它没有定义,因此会出错。

相关问题