【问题标题】:How can I get the number of times my function in functions.php has echo?如何获得functions.php中的函数有回声的次数?
【发布时间】:2021-01-29 21:04:04
【问题描述】:

我是 WordPress 新手,我试图在我的首页上显示我拥有的特定数量的帖子,然后添加一个“加载更多文章”按钮。点击按钮时会加载另外三个帖子,如果还有帖子会再次出现,否则不会显示。

我目前遇到的问题是我无法计算我的函数的次数(functions.php 中显示帖子的函数,它有get_posts(); 和末尾的回显)正在回显(要显示多少帖子)。

我尝试在用于计数的函数之外声明一个全局变量以将其值分配给它,但它不起作用,这就是我所做的:

global $counter;

function post () {
   $posts = get_posts();
   $counter = count( get_posts() );

   foreach($posts  as $post ) {
      $output .= '<div>
                    <h2>'. $post-> get_title() .'</h2>
                 </div>';
   }

echo $output;

}

任何人都知道我如何才能获得从get_posts(); 获得的帖子数量?

【问题讨论】:

  • 或者搜索浏览器或cookie或会话的本地存储。

标签: php wordpress custom-wordpress-pages


【解决方案1】:

你可以像这样返回多个数据

function post(){
  $posts = get_posts();
  $output = '';
  foreach($posts  as $post )
    $output .= '<div><h2>'. $post->get_title() .'</h2></div>';
  return ['counter'=>count($posts) , 'posts'=>$output]
}
$posts=post();
echo $posts['counter'];
echo $posts['posts'];

【讨论】:

  • 谢谢!但我换了一个方向,我为每个项目分配了一个 id,并使用$('.class_name_tey_all_share').last()[0].id 到达最后一个元素
猜你喜欢
  • 2023-04-07
  • 1970-01-01
  • 2014-08-26
  • 2016-09-26
  • 2019-01-05
  • 2020-11-27
  • 2021-06-16
  • 2018-03-29
  • 1970-01-01
相关资源
最近更新 更多