一、循環(huán)的基本結(jié)構(gòu):
<?php if( have_post() ) : ?>
? ? ?<?php while( have_post() ) : the_post(); ?>
? ? ? ? ? <?php the_title(); ?>
? ? ? <?php endwhile; ?>
<?php endif; ?>
說明:1.have_post()函數(shù):判斷當(dāng)前頁面是否還有要顯示的文章
2.the_post()函數(shù):準(zhǔn)備好要顯示的文章內(nèi)容
3.the_title()函數(shù):顯示文章的標(biāo)題忽妒,還有類似的函數(shù)详炬,也叫模版標(biāo)簽 ?
注:你可以在循環(huán)里添加任何你想顯示的東西,文章的正文晰绎,特色圖像寓落,發(fā)布日期,作者等等荞下。找到合適的模版標(biāo)簽伶选,放在循環(huán)里,再加上點自己的設(shè)計尖昏。
相關(guān)資源:
http://codex.wordpress.org/Template_Tags
http://codex.wordpress.org/Function_Reference/have_posts
http://codex.wordpress.org/Function_Reference/the_post
http://codex.wordpress.org/The_Loop
二仰税、條件判斷
if...else ? ?<?php if() : ?> ... <?php else: ?> ... <?php endif; ?>
in_category(id) ?以文章分類作為判斷條件
三、自定義查詢
? ? ? 前面我們在循環(huán)里使用了 WordPress 默認(rèn)的查詢來顯示相關(guān)的內(nèi)容抽诉,默認(rèn)的查詢會使用當(dāng)前頁面的路徑作為查詢的參數(shù)陨簇,然后到數(shù)據(jù)庫里幫我們找到相應(yīng)的內(nèi)容。也就是在首頁上會顯示所有首頁上的東西迹淌,在分類頁面會顯示屬于這個分類的文章河绽,在文章頁面只會顯示當(dāng)前這個文章的內(nèi)容己单。
WP_Query() ?http://codex.wordpress.org/Class_Reference/WP_Query
<?php if ( have_posts() ): ?>
<?php while ( have_posts() ) : the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; ?>
<?php endif; ?>
<?php
$myqueryargs = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'ASC',
'category__in' => array( 8,15 ),
);
?>
<?php $myquery = new WP_Query( $myqueryargs ); ?>
<?php if ( $myquery -> have_posts() ): ?>
<ol>
<?php while ( $myquery -> have_posts() ) : $myquery -> the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
</ol>
<?php endif; ?>
說明:$myquery -> 表示我們自己新建的這個查詢里還有要顯示的內(nèi)容嗎,在have_post(),the_post()前面加上$myquery ->葵姥,意思是去準(zhǔn)備好在我們的查詢里的內(nèi)容荷鼠,最后使用wp_reset_postdata(),恢復(fù)一下查詢榔幸。
自定義查詢的參數(shù)設(shè)置:
'post_type' => 設(shè)置查詢的內(nèi)容類型(post/page...)
'posts_per_page' =>設(shè)置顯示內(nèi)容數(shù)量
'orderby' =>設(shè)置內(nèi)容的排序方法允乐,比如按照文章修改的日期,按照文章評論的數(shù)量等等削咆,默認(rèn)查詢會按照文章的發(fā)布日期牍疏,降序排列。
'order' =>升序或降序
'category_in' =>id 拨齐,只顯示某個分類的內(nèi)容鳞陨,若指定多個分類,需將id放在一個數(shù)組里瞻惋,array(8, 15)