這段代碼允許我向連接的用戶顯示他所寫的文章的串列。然而,我想選擇在沒有文章被寫入時(當串列為空時)出現的訊息。
我知道我需要一個IF陳述句,但我不知道該把它放在哪里,用什么資料。
我想讓沒有文章的用戶看到寫著 "沒有文章"。
謝謝你,
以下是我的代碼:
add_action( 'woocommerce_account_dashboard' , 'recent_posts', 3 ) 。
function recent_posts() /span>{
if ( is_user_logged_in() ) 。
global $current_user;
wp_get_current_user()。
$author_query = array('post_per_page' => '-1','author' => $current_user-> ID)。)
$author_posts = new WP_Query($author_query) 。
?><div id=" recentposts">
<ul class="liststylenone" >
<?php?
while($author_posts->have_posts()) : $author_posts->the_post() 。
?>。
<li><a href="< ? php the_permalink(); ? >" title="<? php the_title_attribute(); ?> "><? php the_title(); ?></a> </li>
<?php?
endwhile;
?></ul><?php?
else :
echo "not logged in" 。
endif;
?">。
uj5u.com熱心網友回復:
當你使用wp_query時,它有一個屬性叫做found_posts。就在您的ul標簽之前,我們將檢查您的查詢中是否有任何帖子。如果沒有找到帖子,那么我們將回顯一個帶有資訊的p標簽。像這樣:
add_action('woocommerce_account_dashboard', 'recent_posts', 3) 。
function recent_posts()
{
if (is_user_logged_in() ) :
global $current_user;
$author_query = array('post_per_page' => '-1', 'author' => $current_user-> ID)。)
$author_posts = new WP_Query($author_query) 。
? >
<div id="recentposts">
<?php?
if ($author_posts-> found_posts) {
? >
<ul class="liststylenone" >
<?php?
while ($author_posts->have_posts()) : $author_posts-> the_post();
?>。
<li><a href="<? php the_permalink(); ? >" title="<? php the_title_attribute(); ?> "><? php the_title(); ?></a> </li>
<?php?
endwhile;
?">。
</ul>
<?php?
} else{
echo '<p class="author-no-post-yet">No Articles</p> ';
}
else :
echo "not logged in"。
endif;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/324417.html
標籤:
上一篇:Sql:如何獲取某一年的月總收支
