【问题标题】:Display different text on page depending on number of published posts根据发布的帖子数量在页面上显示不同的文本
【发布时间】:2022-01-14 09:19:31
【问题描述】:

我想在所选类别中有三个帖子时显示不同的文本,当我添加更多帖子时显示不同的文本...... 类似于“如果帖子数 >= 3 则显示 TEXT1 否则 TEXT2”。 这不起作用:

<?php
$args = array( 'post_type' => 'upoutavka','category__in' => '12, 13',); 
$postslist = get_posts( $args ); 
if(count($posts) >= 3)
{
  echo "TEXT2";
}
else
{ 
  echo "TEXT1"
}
?>

谢谢!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    使用这个:

    $args = array(
        'post_type'      => 'upoutavka', // Change this to any custom post type.
        'posts_per_page' => -1,          // Show all posts.
        'fields'         => 'ids',       // Return only post id we don't need all post data for post count.
        'category__in'   => array( 3 )   // Change this to current category id.
    );
    
    $posts = get_posts( $args );
    
    if ( count( $posts ) >= 3 ) {
        echo __( 'Text no. 2', 'text-domain' );
    } else {
        echo __( 'Text no. 1', 'text-domain' );
    }
    

    【讨论】:

    • 谢谢!当我还添加 count( $posts )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多