本文小編為大家詳細介紹“WordPress如何制作index.php”,內容詳細,步驟清晰,細節處理妥當,希望這篇“WordPress如何制作index.php”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
修改后的代碼是這樣的:
<?php get_header(); ?>
<!-- Column 1 /Content -->
<div class="grid_8">
<!-- Blog Post -->
<div class="post">
<!-- Post Title -->
<h4 class="title"><a href="single.html">Loreum ipsium massa cras phasellus</a></h4>
<!-- Post Data -->
<p class="sub"><a href="#">News</a>, <a href="#">Products</a> ? 31st Sep, 09 ? <a href="#">1 Comment</a></p>
<div class="hr dotted clearfix"> </div>
<!-- Post Image -->
<img class="thumb" alt="" src="<?php bloginfo('template_url'); ?>/images/610x150.gif" />
<!-- Post Content -->
<!-- Read More Button -->
<p class="clearfix"><a href="single.html" class="button right"> Read More...</a></p>
</div>
<div class="hr clearfix"> </div>
<!-- Blog Navigation -->
<p class="clearfix"> <a href="#" class="button float"><< Previous Posts</a> <a href="#" class="button float right">Newer Posts >></a> </p>
</div>
<?php get_sidebar(); ?><?php get_footer(); ?>
undefined<div class="post">
<!-- Post Title -->
<h4 class="title"><a href="single.html">文章標題</a></h4>
<!-- Post Data -->
<p class="sub"><a href="#">標簽1</a>, <a href="#">標簽12</a> ? 發布時間 ? <a href="#">評論數</a></p>
<div class="hr dotted clearfix"> </div>
<!-- Post Image 文章的縮略圖 -->
<img class="thumb" alt="" src="<?php bloginfo('template_url'); ?>/images/610x150.gif" />
<!-- Post Content -->
文章內容
<!-- Read More Button -->
<p class="clearfix"><a href="single.html" class="button right"> 閱讀全文按鈕</a></p>
</div>
<div class="hr clearfix"> </div>
不同主題的主題的文章html骨架是不一樣的,如果你熟悉html,可以很快地分辨出文章骨架,以上是我們這個主題的骨架,我們將以此為基礎給index.php加上動態內容:
1、添加文章標題
找到:
<h4 class="title"><a href="single.html">Loreum ipsium massa cras phasellus</a></h4>
改成:
<h4 class="title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h4>
這里解釋一下這幾個php函數:
<?php the_permalink(); ?> 輸出文章的URL鏈接
<?php the_title(); ?> 輸出文章的標題
2、添加文章標簽
我們很多人在寫文章的時候都喜歡添加一些標簽,況且側邊欄我們也加入了"標簽云",我們的主題應該支持標簽。找到:
<a href="#">News</a>, <a href="#">Products</a>
改成:
<?php the_tags('標簽:', ', ', ''); ?>
函數參考:the_tags
3、添加日期
找到:31st Sep, 09
改成:
<?php the_time('Y年n月j日') ?>
函數參考:the_time
關于該函數中 Y n j 獲取的日期格式,你可以參考文檔(中文),選擇你喜歡的時間格式:zh-cn:自定義時間和日期
可能你看了以上提供的時間和日期文檔,還是一頭霧水,下面提供幾個示例,你就差不多能夠依樣畫葫蘆,指定自己喜歡的時間日期格式:
PHP代碼 | 輸出內容 |
<?php the_time('Y年n月j日') ?> | 1999年5月1日 |
<?php the_time('Y年m月d日') ?> | 1999年05月01日 |
<?php the_time('Y-m-d') ?> | 1999-05-01 |
<?php the_time('y-m-d H:i:s') ?> | 99-05-01 02:09:08 |
4、顯示評論數
現在我們來添加評論數代碼,查找代碼:
<a href="#">1 Comment</a>
改成:
<?php comments_popup_link('0 條評論', '1 條評論', '% 條評論', '', '評論已關閉'); ?>
該函數會根據文章的評論數量顯示不同的文字鏈接,0 條評論、1 條評論等等,當然能你可以根據自己的愛好定制文字內容。該鏈接會直接打開對應的文章,并移動到文章的評論區.
函數參考:comments_popup_link
5、添加編輯按鈕
如果文章作者已登錄,我們將允許他在首頁點擊對應文章的編輯按鈕,就可以直接修改文章,這個功能是可選的,你可以不添加。接上面的評論按鈕,我們在其后面添加相應代碼:
<?php comments_popup_link('0 條評論', '1 條評論', '% 條評論', '', '評論已關閉'); ?><?php edit_post_link('編輯', ' ? ', ''); ?>
函數參考:edit_post_link
6、添加文章內容
可能有些人喜歡在首頁輸出全文,而有些人喜歡在首頁輸出文章摘要,這里提供兩種方案,任君選擇。查找:<!-- Post Content -->
將其改成:
<!-- Post Content -->
<?php the_excerpt(); ?>
只要在寫文章的時候在"摘要"框內填寫摘要,在首頁顯示的就是摘要,如果不填就輸出全文!以下是方案二,用于輸出全文,除非你在文章中使用了<!-- more -->,代碼修改:
<!-- Post Content -->
<?php the_content('閱讀全文...'); ?>
函數參考:
the_excerpt
the_content
7、閱讀全文
這里給添加閱讀全文鏈接,如果在6、添加文章內容中你選擇了輸出全文,本步驟可以忽略,查找代碼:
<a href="single.html" class="button right"> Read More...</a>
改成:
<a href="<?php the_permalink(); ?>" class="button right">閱讀全文</a>
8、添加文章循環
到目前為止,你的首頁還只能顯示一篇文章,要想輸出所有文章,需要我們之前提到的循環。查找代碼:
<!-- Blog Post -->
改成:
<!-- Blog Post --><?php if (have_posts()) : while (have_posts()) : the_post(); ?>
再查找:
<div class="hr clearfix"> </div>
改成:
<div class="hr clearfix"> </div><?php endwhile; ?>
再次查找:
</div><?php get_sidebar(); ?>
改成:
<?php else : ?>
<h4 class="title"><a href="#" rel="bookmark">未找到</a></h4>
<p>沒有找到任何文章!</p>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
好了,現在查看你的主頁,是不是可以顯示多篇文章了呢?文章數量取決于你在后臺設置每頁可顯示的文章數量。以上的循環可以簡化為以下內容,這樣看起來應該比較容易理解了,在endwhile之前不斷地輸出每篇文章,直至文章數量達到每頁顯示的最大文章數量;如果你的博客上一篇文章都沒有,就會輸入無文章提示。
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
文章html骨架
<?php endwhile; ?>
<?php else : ?>
輸出找不到文章提示
<?php endif; ?>
9、添加文章分頁
上面你已經看到,每頁只能顯示部分文章,要想看下一頁,就得添加分頁?,F在查找代碼:
<p class="clearfix"> <a href="#" class="button float"><< Previous Posts</a> <a href="#" class="button float right">Newer Posts >></a> </p>
改成:
<p class="clearfix"><?php previous_posts_link('<< 查看新文章', 0); ?> <span class="float right"><?php next_posts_link('查看舊文章 >>', 0); ?></span></p>
參考函數:
previous_posts_link
next_posts_link
10、文章縮略圖
對于大部分人來說,不太需要文章縮略圖的功能,而且有很多插件可以實現這個功能。這里我們將首頁的文章縮略圖代碼刪除:
<!-- Post Image --><img class="thumb" alt="" src="<?php bloginfo('template_url'); ?>/images/610x150.gif" />
另外,還有個存檔頁面的模板archive.php,跟index.php的制作過程完全一樣,只不過需要在functions.php里添加一個函數,這里就不再羅嗦,下載自己看吧,注意:functions.php中的代碼已經修改,要想讓你的分類、標簽等存檔頁能夠正常顯示,請下載最新的functions.php覆蓋原來的。另外,標簽頁和分類頁支持在該頁面頂部顯示介紹,前提是你在后臺文章標簽和分類處要填上了描述。
讀到這里,這篇“WordPress如何制作index.php”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。