2012-11-11 53 views
0

我有這個代碼從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; ?> 

一些內容我需要把狀態返回文本消息,如果類是empty.like沒有文章顯示,請注意後使用WPML的語言切換器

回答

2
<?php 
$myposts = get_posts($args); 
if($myposts){ 
    //found posts 
}else{ 
    //no posts 
} 
?> 

UPDATE:請檢查代碼的工作大概然後將它與你的代碼進行比較,我都談到我所做的更改,所以這是你的機會來學習:

$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; 
} 
?> 
+0

很抱歉,但我是新來的,更多地解釋:d – user1714777

+0

@ user1714777我已經更新了代碼 –

+0

THX這麼多的工作就像一個魅力:) – user1714777

0

太感謝你了!這解決了我的問題.. 裁判:howlingwolfmedia.com/site3/classes/class-categories

參見 '個性化訓練'

我的代碼在這裏: `

<?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> 
     '; 
    } 
?> 

`