WordpressCMS主題開發08-CMS文章內容頁面的製作

huangbangqing12發表於2018-07-08

這節課,我們來製作內容頁面

製作內容頁面single.php

首先,我們來看看靜態的內容頁面,下面,我們就開始製作這樣的內容頁面,開啟主題資料夾,複製article.html,然後貼上,重新命名為single.php,

首先修改頭部呼叫為wordpress的標籤:

<?php get_header();?>
<!--搜尋欄End--><!--主體下Begin-->
    <div class="page_row">

緊接著,文章頁面的底部呼叫footer.php:並把一些多餘的程式碼刪除掉:

    <!--主體下End--><!--友情連結Begin--><!--友情連結End--><!--頁尾Begin-->
    <?php get_footer();?>
 <!--頁尾End-->
  </body>
</html>

然後,把這個檔案的編碼轉為utf-8:

儲存,來到網站前臺檢視下:

You must be logged in to view the hidden contents.

我們依次往下修改,接下來,修改麵包屑導航,找到麵包屑導航的位置,替換為上節課的函式呼叫:

    <div class="list_bar">
        <?php wheatv_breadcrumbs(); ?>
    </div>

接著,依次來修改相關的呼叫:

首先替換標題為模板標籤的呼叫:

<div class="ctitle ctitle1"><?php the_title_attribute(); ?></div>

下面是作者-釋出日期-檢視次數(用到外掛wp-postviews):

一一用模板替換:

    <div class="ctitleinfo">
    作者:<?php the_author_posts_link(); ?>;釋出日期:<?php the_date_xml(); ?>;檢視次數:<?php the_views();?> 次
    </div>

在文章和摘要的中間,多出了一個div容器,這裡不需要,所以把下面這段程式碼刪除:

<pre class="pure-highlightjs" style="margin: 0px; padding: 0px; font-weight: 400; font-family: inherit; background-color: transparent; border: none; color: rgb(69, 69, 69); font-size: 18px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><div style="text-align: center"><a target="_blank" href="../download/index.html"><img alt="" style="width: 672px; height: 83px" src="../res_base/jeecms_com_www/default/article/img/1111111.jpg" /></a></div></pre>

接著是摘要,同樣,我們使用模板來呼叫(自動呼叫文章的前200個字元):

    <div class="intr"><span class="intr_front">摘 要:</span>
        <?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 200,"……"); ?>
    </div>

然後是文章內容的呼叫(文章內容在p標籤裡就可以,不需要font標籤的限定):

對文章內容進行迴圈呼叫即可:

    <div class="pbox">

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <p>
        <?php the_content("Read More..."); ?>
    </p>
    <?php endwhile; ?>
    <?php else : ?>
    <?php endif; ?>

再到首頁重新整理一下效果,這樣就正常的出現了文章的內容。

再往下面看看,這裡有版權的資訊,直接刪除:

<p><font size="3"><font face="宋體"><span lang="EN-US">&nbsp;&nbsp;</span></font></font><span lang="EN-US" style="font-family: 宋體; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋體; mso-bidi-font-weight: bold"><o:p><font size="3">&nbsp;</font></o:p></span></p>
<p class="MsoNormal" style="background: white; margin: 0cm 0cm 0pt; text-indent: 21pt"><span lang="EN-US"><o:p><font face="Times New Roman" size="3">&nbsp;</font></o:p></span></p>
<p class="MsoNormal" style="background: white; margin: 0cm 0cm 0pt"><span style="font-size: 9pt; color: #42515a; font-family: 宋體; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">本文地址:</span><span lang="EN-US" style="font-size: 9pt; color: #42515a; font-family: Verdana">http://www.xuhss.com/theme-dev/cms/01-cms-css<o:p></o:p></span></p>
<p class="MsoNormal" style="background: white; margin: 0cm 0cm 0pt"><span style="font-size: 9pt; color: #42515a; font-family: 宋體; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">相關文章:</span><span lang="EN-US" style="font-size: 9pt; color: #42515a; font-family: Verdana"><o:p></o:p></span></p>
<p class="MsoNormal" align="left" style="margin: 0cm 0cm 0pt; text-align: left"><span lang="EN-US" style="font-size: 9pt; color: #42515a; font-family: Verdana"><a href="125.htm"><span lang="EN-US" style="font-family: 宋體; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana"><span lang="EN-US"><font color="#800080">百度收錄問題</font></span></span></a><o:p></o:p></span></p>
<p class="MsoNormal" style="background: white; margin: 0cm 0cm 0pt"><span lang="EN-US" style="font-size: 9pt; color: #42515a; font-family: Verdana"><a href="78.htm"><span lang="EN-US" style="font-family: 宋體; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana"><span lang="EN-US"><font color="#800080">網站內容優化</font></span></span></a><o:p></o:p></span></p>
<p class="MsoNormal" align="left" style="margin: 0cm 0cm 0pt; text-align: left"><span lang="EN-US" style="font-size: 9pt; color: #42515a; font-family: Verdana"><a href="66.htm"><font color="#800080">SEO<span lang="EN-US" style="font-family: 宋體; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana"><span lang="EN-US">發展前景</span></span></font></a><o:p></o:p></span></p>
<p class="MsoNormal" align="left" style="background: white; margin: 0cm 0cm 0pt; text-align: left; mso-pagination: widow-orphan"><font face="Times New Roman"><span lang="EN-US" style="font-size: 12pt; mso-font-kerning: 0pt">&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;</span><span lang="EN-US" style="font-size: 12pt; font-family: 宋體; mso-font-kerning: 0pt; mso-bidi-font-family: 宋體"><o:p></o:p></span></font></p>
<p class="MsoNormal" align="left" style="background: white; margin: 0cm 0cm 0pt; line-height: 16.5pt; text-align: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"><font size="3"><u><span style="color: purple; font-family: 宋體; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: Arial">快樂網賺</span></u><span lang="EN-US" style="color: #42515a; font-family: 宋體; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: Arial">-</span><span lang="EN-US" style="color: #42515a; font-family: Verdana; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: Arial"><a href="../index.html"><span lang="EN-US" style="color: purple; font-family: 宋體; mso-bidi-font-size: 12.0pt"><span lang="EN-US">網上如何賺錢</span></span></a></span><span lang="EN-US" style="color: #42515a; font-family: 宋體; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: Arial">-</span><span lang="EN-US" style="color: #42515a; font-family: Verdana; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: Arial"><a href="../index.html"><span lang="EN-US" style="color: purple; font-family: 宋體; mso-bidi-font-size: 12.0pt"><span lang="EN-US">網路兼職賺錢</span></span></a></span><span lang="EN-US" style="color: #42515a; font-family: 宋體; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: Arial"> </span><span style="color: #42515a; font-family: 宋體; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: Arial">編輯<span lang="EN-US">,</span></span><span style="color: #42515a; font-family: 宋體; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: Arial; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">如本網文章稿涉及版權等問題</span><span lang="EN-US" style="color: #42515a; font-family: Verdana; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: Arial">,</span><span style="color: #42515a; font-family: 宋體; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: Arial">請作者在兩週內速與本網站聯絡。</span><span lang="EN-US" style="font-size: 9pt; font-family: Arial; mso-font-kerning: 0pt"><o:p></o:p></span></font></p>
<p class="MsoNormal" align="left" style="background: white; margin: 0cm 0cm 0pt; line-height: 16.5pt; text-align: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"><span lang="EN-US" style="color: #42515a; font-family: 宋體; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: Arial"><font size="3">&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;</font></span><span lang="EN-US" style="font-size: 9pt; font-family: Arial; mso-font-kerning: 0pt"><o:p></o:p></span></p>

再往下面看看,這裡有對文章的分頁:

我們不需要分頁,直接刪除即可:

<pre class="pure-highlightjs" style="margin: 0px; padding: 0px; font-weight: 400; font-family: inherit; background-color: transparent; border: none; color: rgb(69, 69, 69); font-size: 18px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> <div class="page_no"><div class="pg-3"> <span class="total">共1頁</span> <span class="disabled"> |< 首頁</span><span class="disabled"> < 上一頁</span> <span class="current">1</span> <span class="disabled"> 下一頁 ></span><span class="disabled"> 尾頁 >|</span> </div></div></pre>

上下一篇功能

替換上下一篇為模板標籤:

    <div class="arti_ref">【上一篇】:  <?php previous_post_link('%link'); ?>
 &nbsp;&nbsp; 【下一篇】: <?php next_post_link('%link'); ?>
    </div>

測試點選下上一篇。

新增相關文章

再往下,我們實際上可以做一個相關文章的呼叫:

可以參考“摘要”的div的形式,做一個相關文章,我們複製摘要所在的div,貼上到對應的“下一篇”下面的位置:

        <div class="intr"><span class="intr_front">摘 要:</span>
        <?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 200,"……"); ?>
        </div>

然後,修改為“相關文章”的呼叫程式碼:

</div>
        <div class="intr">
        相關文章:
            <?php
                $cats = wp_get_post_categories($post->ID);
                if ($cats) {
                $cat = get_category( $cats[0] );
                $first_cat = $cat->cat_ID;

                $args = array(
                'category__in' => array($first_cat),
                'post__not_in' => array($post->ID),
                'showposts' => 5,
                'caller_get_posts' => 1
                );
                query_posts($args);
                if (have_posts()) :
                while (have_posts()) : the_post(); update_post_caches($posts); ?>
            <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
            <?php endwhile; else : ?>
            <li>* 暫無相關文章</li>
            <?php endif; wp_reset_query(); } ?>
        </div>

來到前臺重新整理下:

You must be logged in to view the hidden contents.

評論

通常,文章都是可以評論的,所以我們想刪除靜態的評論程式碼(table/form/script),改成wordpress的評論呼叫標籤:

    <div class="comment_list">
        <?php comments_template(); ?>
    </div>

You must be logged in to view the hidden contents.

為了讓樣式好看一點,我也把它放在和“相關文章”一樣的div容器裡面:

    <div class="comment_list">
    <div class="intr">
        <?php comments_template(); ?>
    </div>
    </div>

這樣的內容頁面就比較漂亮了。

相關文章