drl37530 2012-11-11 14:39
浏览 18
已采纳

如果类别为空则返回消息

i have this code to get some elements from wordpress

global $post;

$i=0;

$args = array( 'numberposts' => 5, 'category' =>5,'order'=>'DESC','orderby'=>'post_date','suppress_filters' => 0 );

$myposts = get_posts( $args );
$has_posts = true;

foreach( $myposts as $post ) :  setup_postdata($post); ?>


<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>

<li>

<?php if($image){ ?>


<div class="news_left"><a href="<?php the_permalink(); ?>"><img src="<?php echo $image[0]; ?>" alt="" width="191" height="132" /></a></div>

<?php } ?>

<?php
    $content = apply_filters('the_content', get_the_content());
    $content = explode("</p>", $content);
?>

<div class="news_right">

    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

    <span class="date">Date: <?php the_time('j/m/Y') ?></span>

    <?php echo $content[1] . "</p>";//echo String::content_limit(200,'<p>'); ?>

    <a href="<?php the_permalink(); ?>">Read More</a>

</div> 

<div class="clear"></div>   

</li>

<?php $i++; endforeach; ?>

i need to put a condition to return text message if category is empty.like no posts to display , please note that the post use WPML as the language switcher

  • 写回答

2条回答 默认 最新

  • dragon321723 2012-11-11 14:46
    关注
    <?php
    $myposts = get_posts( $args );
    if($myposts){
        //found posts
    }else{
        //no posts
    }
    ?>
    

    UPDATE: PLEASE check if the code works probably and THEN compare it with your code, i have commented on the changes i made, so its your chance to learn:

    $i=0;
    
    $args = array( 'numberposts' => 5, 'category' =>5,'order'=>'DESC','orderby'=>'post_date','suppress_filters' => 0 );
    
    $myposts = get_posts( $args );
    
    //check if $myposts
    if(!$myposts){
        //the $myposts has no posts, print the error message
        echo "<li>";
        echo "This category has zero posts";
        echo "</li>";
    }else{
        //the category has one more or more posts
        $has_posts = true;
        foreach( $myposts as $post ) :  setup_postdata($post); ?>
    
    
        <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
    
        <li>
    
        <?php if($image){ ?>
    
    
        <div class="news_left"><a href="<?php the_permalink(); ?>"><img src="<?php echo $image[0]; ?>" alt="" width="191" height="132" /></a></div>
    
        <?php } ?>
    
        <?php
            $content = apply_filters('the_content', get_the_content());
            $content = explode("</p>", $content);
        ?>
    
        <div class="news_right">
    
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
            <span class="date">Date: <?php the_time('j/m/Y') ?></span>
    
            <?php echo $content[1] . "</p>";//echo String::content_limit(200,'<p>'); ?>
    
            <a href="<?php the_permalink(); ?>">Read More</a>
    
        </div> 
    
        <div class="clear"></div>   
    
        </li>
    
    <?php
        $i++; endforeach; 
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法