首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果类别为空,则返回一条消息

如果类别为空,则返回一条消息
EN

Stack Overflow用户
提问于 2012-11-11 22:39:35
回答 2查看 3.8K关注 0票数 0

我有这段代码来从wordpress获取一些元素

代码语言:javascript
复制
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; ?>

我需要把一个条件来返回文本消息,如果类别是empty.like没有帖子显示,请注意,帖子使用WPML作为语言切换器

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-11 22:46:56

代码语言:javascript
复制
<?php
$myposts = get_posts( $args );
if($myposts){
    //found posts
}else{
    //no posts
}
?>

更新:请检查代码是否正常工作,然后将其与您的代码进行比较,我已经对我所做的更改进行了评论,所以这是您学习的机会:

代码语言:javascript
复制
$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; 
}
?>
票数 2
EN

Stack Overflow用户

发布于 2013-09-17 22:28:37

非常感谢!这解决了我的问题..参考: howlingwolfmedia.com/site3/classes/class-categories

参见“个性化培训”

我的代码如下:`

代码语言:javascript
复制
<?php 
    $args=array(
      'child_of' => 4,
      'orderby' => 'name',
      'order' => 'ASC',
      'hide_empty' => '0'
      );
    $categories=get_categories($args);
      foreach($categories as $category) {
        echo '<h3>' . $category->name . '</h3>
        <div>';
            global $post;
            $args = array( 'posts_per_page' => -1, 'category' => $category->term_id, 'orderby' => 'name', 'order' => 'ASC' );
            //alternatively this also works:  'nopaging' => true
            $myposts = get_posts( $args );

            if (!$myposts) {
                echo 'Sorry there are currently no classes in this category';
                }
            else {
            foreach( $myposts as $post ) :  setup_postdata($post);
            echo  '<p><a href="' . get_permalink($post->ID) . ' " style="color:#7c7c7c; font-size: 1em; text-decoration:none !important; line-height:80%;">' . get_the_title($post->ID) . '</a></p>';
            endforeach;
            }
        echo  '</div>
       ';
    }
?>

`

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13332128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档