Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用IF语句根据帖子大小显示最近的帖子_Php_Wordpress_If Statement_Sidebar - Fatal编程技术网

Php 使用IF语句根据帖子大小显示最近的帖子

Php 使用IF语句根据帖子大小显示最近的帖子,php,wordpress,if-statement,sidebar,Php,Wordpress,If Statement,Sidebar,我在wordpress中有一个侧边栏,显示我最近的帖子。执行此操作的php代码很简单: $recent_posts = wp_get_recent_posts(array("numberposts"=>5)); 我想附上一份IF声明,说明: 如果wordpress帖子超过100个单词,则显示10篇最近的帖子,否则显示5篇 一旦我知道如何实现,我将计算出相关的数字等。您可以使用全局$post检查post\u内容的长度,然后相应地设置$numberpost global $post; $nu

我在wordpress中有一个侧边栏,显示我最近的帖子。执行此操作的php代码很简单:

$recent_posts = wp_get_recent_posts(array("numberposts"=>5));
我想附上一份IF声明,说明:

如果wordpress帖子超过100个单词,则显示10篇最近的帖子,否则显示5篇


一旦我知道如何实现,我将计算出相关的数字等。

您可以使用全局
$post
检查
post\u内容的长度,然后相应地设置
$numberpost

global $post;
$numberposts = 1; // default number of posts
if ( !empty($post) ){
    $len = strlen( $post->post_content );
    // change $numberposts based on length of $post->post_content
    if ( $len < 300 ){
      $numberposts = 8;
    } elseif ( $len < 500 ){
      $numberposts = 5;
    } elseif ( $len < 800 ){
      $numberposts = 3;
    } else {
      $numberposts = 1;
    }
}
$recent_posts = wp_get_recent_posts(array("numberposts"=>$numberposts));
global$post;
$numberposts=1;//默认员额数
如果(!空($post)){
$len=strlen($post->post\u内容);
//根据$post->post\u内容的长度更改$numberposts
如果($len<300){
$numberposts=8;
}elseif($len<500){
$numberposts=5;
}elseif($len<800){
$numberposts=3;
}否则{
$numberposts=1;
}
}
$recent_posts=wp_get_recent_posts(数组(“numberposts”=>$numberposts));

hmm。。它类似于
if(strlen($recents_posts>300){//dothis}
但你要么必须将
$recent\u posts
更改为
$the\u post
。可能在
foreach
循环中解析它。@yunodown我想你的意思是
全局$post
而不是
$the\u posts
是一个
数组,所以你不能
strlen()
在上面。啊,泰。我已经浏览了wordpress php,但还没有学会所有的变量名。因此,处理数组的foreach:p