目前為止我們只定義了一種格式的帖子害晦, 我們還可以提供諸如圖庫设拟,鏈接萍诱,圖像和報價狀態(tài)更新等內(nèi)容三圆,我們可以通過不同方式格式化這些不同類型的帖子狞换。接下來我們來學(xué)習(xí)如何定制不同格式的帖子。
精簡index文件代碼
打開index.php舟肉,仔細(xì)觀察主循環(huán)部分的每一行代碼:
<?php while(have_posts()) : the_post(); ?>
<article class="post">
<h2><?php the_title(); ?></h2>
<p class="meta">
發(fā)布于 <?php the_time('F j, Y g:i a'); ?> by
<a href="<?php echo get_author_posts_url( get_the_author_meta('ID')); ?>">
<?php the_author(); ?>
</a> | 類別:
<?php
$categories = get_the_category();
$separator = ", "; $output = '';
if($categories){
foreach($categories as $category){
$output .= '<a href="'.get_category_link($category->term_id).'">'. $category->cat_name.'</a>'.$separator;
}
}
echo trim($output, $separator);
?>
</p>
<?php if(has_post_thumbnail()) : ?>
<div class="post-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<?php the_excerpt(); ?>
<a class="button" href="<?php the_permalink(); ?>"> Read More</a>
</article>
<?php endwhile; ?>
創(chuàng)建content.php
從article標(biāo)簽開始到 article結(jié)束標(biāo)簽的所有內(nèi)容修噪。 我們將其剪切并粘貼到一個新文件中,并將其保存為content.php路媚。然后回到index文件黄琼,在之前的 article 代碼塊位置添加以下代碼:
<?php get_template_part('content'); ?>
保存代碼到前端刷新,頁面沒有變化整慎。
精簡archive和search.php代碼
我們再來查看archive.php脏款,你可以看到相同的內(nèi)容,我們的想法就是把while循環(huán)中的所有內(nèi)容放入content.php文件中裤园,我們剪切以下代碼:
<div class="archive-post">
<h4>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h4>
<p>Posted On: <?php the_time('F j, Y g:i a'); ?></p>
</div>
梳理content.php文件邏輯關(guān)系
跳轉(zhuǎn)到content.php中撤师,現(xiàn)在這里需要的代碼與我們常規(guī)查看博客文章內(nèi)容的代碼略有不同,所以我們可以使用if條件語句檢查是否獲得存檔或搜索結(jié)果頁面拧揽。代碼如下:
<?php if(is_search() || is_archive()) : ?>
<div class="archive-post">
<h4>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h4>
<p>Posted On: <?php the_time('F j, Y g:i a'); ?></p>
</div>
<?php else : ?>
<?php endif; ?>
這段代碼的意思是:如果當(dāng)前頁是搜索結(jié)果頁或者分類存檔頁剃盾,那么就調(diào)用下面這段從archive文件中剪切過來的代碼,來顯示相應(yīng)的內(nèi)容淤袜。
接下來痒谴,我們將獲取article標(biāo)簽中的所有代碼并將其粘貼到else部分中,content文件的全部代碼如下所示:
<?php if(is_search() || is_archive()) : ?>
<div class="archive-post">
<h4>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h4>
<p>Posted On: <?php the_time('F j, Y g:i a'); ?></p>
</div>
<?php else : ?>
//else部分代碼
<article class="post">
<h2><?php the_title(); ?></h2>
<p class="meta">
發(fā)布于 <?php the_time('F j, Y g:i a'); ?> by <a href="<?php echo get_author_posts_url( get_the_author_meta('ID')); ?>"><?php the_author(); ?>
</a> | 類別:
<?php
$categories = get_the_category();
$separator = ", "; $output = '';
if($categories){
foreach($categories as $category){
$output .= '<a href="'.get_category_link($category->term_id).'">'. $category->cat_name.'</a>'.$separator;
}
}
echo trim($output, $separator);
?>
</p>
<?php if(has_post_thumbnail()) : ?>
<div class="post-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<?php the_excerpt(); ?>
<a class="button" href="<?php the_permalink(); ?>"> Read More</a>
</article>
<?php endif; ?>
保存代碼铡羡,跳到archive.php文件中积蔚,在剛才剪掉代碼的位置也粘貼上這句代碼:
<?php get_template_part('content'); ?>
同樣在search.php文件中用上面這句代碼替換while內(nèi)部的所有代碼。
精簡single.php代碼
我們也可以在single.php文件中實(shí)現(xiàn)content.php文件烦周,因?yàn)閟ingle.php文件和index.php文件中有很多相同的東西尽爆。都有一個article標(biāo)簽;不同的地方是index中使用了the_excerpt()摘要读慎,而single中用的是the_content教翩;index中多了一個Read More。進(jìn)入single文件贪壳,我們刪除從article開始標(biāo)簽到結(jié)束article標(biāo)簽這段代碼饱亿,然后在這個位置粘貼一個get _template_part(),然后保存。再進(jìn)入content.php文件彪笼,添加一些條件钻注,覆蓋之前的老代碼。
<?php if(is_single()) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<a class="button" href="<?php the_permalink(); ?>">Read More</a>
<?php endif; ?>
定制帖子格式
現(xiàn)在archive.php的while循環(huán)中只有一行代碼:
<?php get_template_part('content'); ?>
同樣的事也發(fā)生在search配猫,single 和 index頁幅恋,接下來我們開始定制帖子格式,先來添加一個圖庫類型泵肄。
首先捆交,我們轉(zhuǎn)到functions.php文件。 啟用我們想要使用的這些不同格式腐巢。
在register_nav_menus()函數(shù)下方插入如下代碼:
register_nav_menus(array(
'primary' => __('Primary Menu'),
'footer' => __('Footer Menu')
));
//Post Format Support
add_theme_support('post-formats', array('aside', 'gallery', 'link'));
現(xiàn)在我們到后臺打開寫文章選項(xiàng)品追,會看到在右邊多了一個形式選項(xiàng)欄,如下圖:
現(xiàn)在也看不出它能做什么不同的事情冯丙。 所以讓我們做一些與眾不同的事情肉瓦。
新建一篇博客,標(biāo)題叫做“普通日志”胃惜,分類:IT泞莉,形式:日志,然后粘貼一些內(nèi)容船殉,最后點(diǎn)發(fā)布鲫趁。
現(xiàn)在回到前端刷新,發(fā)現(xiàn)我們多了一篇文章利虫,它并沒有什么特別的地方
但是這樣做的目的是讓它以不同的方式顯示出來挨厚。
接下來我們轉(zhuǎn)到index.php文件中,在get_template_part()中傳入第二個參數(shù) get_post_format()列吼,代碼如下:
<?php get_template_part('content', get_post_format()); ?>
這是一個函數(shù)幽崩,它將允許查看文章是什么類型的帖子格式苦始。我們轉(zhuǎn)到archive.php寞钥,search.php和single.php,把第二個參數(shù)插到所有g(shù)et_template_part()中陌选。
定制日志格式
接下來我們?yōu)槊恳环N帖子格式都創(chuàng)建一個模板文件理郑,先建一個content-aside.php,然后再創(chuàng)建content-gallery.php 和 content-link.php咨油。
現(xiàn)在您炉,通過這些不同的文件,我們可以使我們的格式看起來不同役电,從content-aside.php文件開始赚爵,在文件中添加以下代碼:
<article class="post post-aside">
<div class="well">
<small>
<?php the_author(); ?>@<?php the_date(); ?>
</small>
<?php the_content(); ?>
</div>
</article>
保存代碼到前端窗口刷新,我們看到文章樣式變了
我們?yōu)槿罩靖袷教砑右恍邮剑屗雌饋砀烙^:
article.post-aside small {
font-weight: bold;
}
article.post-aside .well {
background: #e0eefc;
padding: 10px;
}
保存代碼冀膝,到前端刷新頁面:
定制鏈接格式
接下來定制content-link.php唁奢,找到文件打開添加以下代碼:
<article class="post post-link">
<div class="well">
<a href="<?php echo get_the_content(); ?>">
<?php echo the_title(); ?>
</a>
</div>
</article>
接下來我們回到后臺在寫一篇文章,帖子格式設(shè)置為鏈接窝剖;標(biāo)題:“WordPress課程簡書同步更新”麻掸;內(nèi)容只放一個鏈接,然后發(fā)布赐纱。然后回到首頁刷新會看到一個鏈接到http://www.reibang.com/u/199fee2e0fd9的超鏈接標(biāo)題脊奋。
然后再給鏈接格式添加一點(diǎn)樣式:
article.post-link .well {
background: #f4f4f4;
padding: 10px;
}
定制相冊格式
最后來搞定相冊格式,新建一個文件保存為 content-gallery.php疙描,并添加以下代碼:
<article class="post post-gallery">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</article>
再去后臺創(chuàng)建一篇相冊形式的帖子诚隙,名字:美圖相冊,分類:相冊淫痰,我們在內(nèi)容中插入幾張圖片然后點(diǎn)擊發(fā)布最楷,回到前端刷新:
再來為相冊格式添加點(diǎn)樣式:
article.post-gallery {
background: #333;
color: #fff;
padding: 5px 10px;
margin-top: 5px;
}
保存代碼,去前端刷新頁面:
格式化完成待错。