當(dāng)前位置:首頁>WordPress建站>WordPress開發(fā)>在 WordPress 中創(chuàng)建上下文相關(guān)的側(cè)欄頁面導(dǎo)航

在 WordPress 中創(chuàng)建上下文相關(guān)的側(cè)欄頁面導(dǎo)航

有時(shí)我得到一項(xiàng)業(yè)務(wù),我的客戶要求建立一個完全由眾多網(wǎng)頁構(gòu)成的網(wǎng)站——沒有花哨的數(shù)據(jù)庫查詢,也沒有額外的模板文件,只有在分層結(jié)構(gòu)中的一大堆網(wǎng)頁。

對用戶來講,一個非常龐大且內(nèi)嵌有眾多網(wǎng)頁的網(wǎng)站,如果導(dǎo)航?jīng)]有做好的話,就會變得非常混亂而難以操作。因而,在每一個網(wǎng)頁頁面層次的當(dāng)前分支中,提供一個包含所有頁面的導(dǎo)航列表就顯得很有幫助了。

例如,如果你為一個機(jī)構(gòu)建立一個網(wǎng)站,這個機(jī)構(gòu)的每個功能展示都要有一個頂級頁面和各個部門相關(guān)的子頁面來完成,然后你想要列出所有的部門以及一個指向頂級功能頁面的鏈接,并且無論在哪一個子頁面和哪一個部門的頁面上,它們都會和頂級頁面同時(shí)存在。

為了做到這一點(diǎn),你是無法通過查詢當(dāng)前文章類型(post type)或分類項(xiàng)目(taxonomy term)來將相關(guān)內(nèi)容的列表全部列出的。你需要去確定當(dāng)前頁面在網(wǎng)站結(jié)構(gòu)中的位置,從而能顯示一組相應(yīng)的鏈接列表。

在這里,我將向你展示如何做到這一點(diǎn):通過建立一個函數(shù),添加到側(cè)邊欄文件中,或添加到你模板文件原有的內(nèi)容之上(或者如果你的主題使用了這些模板的話,也可以通過“鉤子”激活)。

這個過程包含兩個階段:

1.確定當(dāng)前頁面在網(wǎng)站結(jié)構(gòu)中的位置

2.輸出頁面列表

你需要做的是

完成本教程,你需要

  • 安裝WordPress
  • 一個文本編輯器

創(chuàng)建你的插件

我打算在一個插件中建立這個函數(shù),從而保證其主題的獨(dú)立性。因此,第一步便是建立一個插件文件,我命名其為 tutsplus-list-subpages.php。

打開你的插件文件,添加以下代碼:

<?php 
/*Plugin Name: List Subpages
Description: This plugin checks if the current page has parent or child pages and if so, outputs a list of the highest ancestor page and its descendants. It creates a function called tutsplus_list_subpages() which you insert into your theme or activate via a hook to work.
Version: 1.0
Author: Rachel McCollin
Author URI: http://rachelmccollin.com
License: GPLv2
*/
?>

很明顯,你的代碼會因?yàn)椴寮髡吆途W(wǎng)址(URL)的不同而需要做出相應(yīng)的改變,你也許已經(jīng)迫不及待地想要修改這些說明了吧。

在代碼說明中包含函數(shù)名稱這一點(diǎn)是非常有用的,因?yàn)樗馕吨?dāng)你在一個網(wǎng)站上安裝你的插件的時(shí)候,你無須去檢查代碼以提醒你自己如何去使用它。

確認(rèn)當(dāng)前頁面在網(wǎng)站層次結(jié)構(gòu)中的位置

為了找到當(dāng)前頁面在網(wǎng)站層次結(jié)構(gòu)中的位置,你需要完成以下4件事情:

  1. 檢查當(dāng)前是否真的是一個頁面
  2. 檢查當(dāng)前頁面是否有母頁面
  3. 如果沒有,那么你便可以確認(rèn)當(dāng)前所在位置是網(wǎng)站層次中的頂級初始頁面部分
  4. 如果有,你需要用get_post_ancestors()來確認(rèn)頂級初始頁面

那么就讓我們來試一試吧!

首先使用一個條件標(biāo)簽來建立一個新的函數(shù),用來檢查當(dāng)前頁面:

<?php
function tutsplus_check_for_page_tree() {
 
    //start by checking if we're on a page
    if( is_page() ) {
         
    }
 
}
?>

現(xiàn)在,在 is_page()條件標(biāo)簽里,開始定義 $post全局變量:

<?php
function tutsplus_check_for_page_tree() {
 
    // start by checking if we're on a page
    if( is_page() ) {
     
        global $post;
     
    }
 
}
?>

接下來你需要確認(rèn)的是當(dāng)前頁面是否有母頁面,你需要用到 if ( $post->post_parent ) :

<?php
function tutsplus_check_for_page_tree() {
 
    // start by checking if we're on a page
    if ( is_page() ) {
     
        global $post;
     
        // next check if the page has parents
        if ( $post->post_parent ) {
 
        }
         
    }
 
}
?>

如果當(dāng)前頁面有母頁面的話,你需要用get_post_ancestors()來確定頂級初始頁面:

<?php
function tutsplus_check_for_page_tree() {
 
    //start by checking if we're on a page
    if( is_page() ) {
     
        global $post;
     
        // next check if the page has parents
        if ( $post->post_parent ){
         
            // fetch the list of ancestors
            $parents = array_reverse( get_post_ancestors( $post->ID ) );
             
            // get the top level ancestor
            return $parents[0];
             
        }
    }
 
}
?>

以上定義了 $parents一個新的變量,這個變量的值就是在當(dāng)前網(wǎng)站層次分支中頂級頁面的ID。return $parents[0];這一行輸出了這個值,因此你可以在以后的函數(shù)當(dāng)中使用它。

最后,你需要定義如果當(dāng)前頁面沒有母頁面,即它本身就是頂級初始頁面的情況。在此情況下,你想要輸出當(dāng)前頁面的ID,就要將下列代碼添加到你的函數(shù)中:

return $post->ID;

整個函數(shù)現(xiàn)在看起來是這樣的:

<?php
function tutsplus_check_for_page_tree() {
 
    //start by checking if we're on a page
    if( is_page() ) {
     
        global $post;
     
        // next check if the page has parents
        if ( $post->post_parent ){
         
            // fetch the list of ancestors
            $parents = array_reverse( get_post_ancestors( $post->ID ) );
             
            // get the top level ancestor
            return $parents[0];
             
        }
         
        // return the id  - this will be the topmost ancestor if there is one, or the current page if not
        return $post->ID;
         
    }
 
}
?>

輸出一組子頁面列表

既然你已經(jīng)知道了當(dāng)前網(wǎng)站層次分支中頂級頁面的ID,輸出一組子頁面列表相對來說就很簡單了。你需要用 get_pages()來確認(rèn)頂級頁面的子頁面,頂級頁面的ID之前已經(jīng)確認(rèn)過了。此外,你還需要在列表的最前面輸出初始頁面的鏈接。

使用list_pages()確認(rèn)子頁面

首先建立一個新的函數(shù),用來檢查當(dāng)前頁面:

<?php
function tutsplus_list_subpages() {
 
    // don't run on the main blog page
    if ( is_page() ) {
 
    }
 
}
?>

請注意,如果你要將這個函數(shù)添加到你的page.php模板中,你可以不用進(jìn)行這項(xiàng)檢查。

在條件標(biāo)簽中你首先要做的就是將已經(jīng)確認(rèn)的頂級頁面ID添加到tutsplus_check_for_page_tree()函數(shù)中,實(shí)現(xiàn)代碼如下:

$ancestor = tutsplus_check_for_page_tree();

接著,我們來定義 get_pages() 函數(shù)的參數(shù):

$args = array(
    'child_of' => $ancestor,
    'depth' => '-1',
    'title_li' => '',
);

讓我們來快速回顧一下我使用的參數(shù):

  • ‘child_of’ => $ancestor 確認(rèn)哪些是$ancestor頁面的子頁面
  • ‘depth’ => ‘-1’ 描述函數(shù)運(yùn)用于網(wǎng)站層次結(jié)構(gòu)中的層次數(shù)目。如果你只是想展示一個或兩個層次的話你可以改變其賦值。
  • ‘title_li’ => ” 確保所輸出的不會被包裹在任何HTML標(biāo)簽內(nèi),我會在之后添加這些內(nèi)容。

下一步,你需要運(yùn)行 list_pages()函數(shù):

$list_pages = get_pages( $args );

輸出頁面列表

既然你已經(jīng)建立好了你的網(wǎng)頁,那么接下來你需要輸出它們的鏈接。為了做到這一點(diǎn),首先你要檢查list_pages()是否返回一個空數(shù)組:

if ( $list_pages ) {
 
}

在檢查中,第一個鏈接指向頂級頁面:

<ul class="page-tree">
    <?php // list ancestor page ?>
    <li class="ancestor">
        <a href="<?php echo get_permalink( $ancestor ); ?>"><?php echo get_the_title( $ancestor ); ?></a>
    </li>
</ul>

然后接下來,第一個<li>元素仍然在<ul>中,使用wp_list_pages()函數(shù)輸出包裹在超鏈接中的頁面列表:

wp_list_pages( $args );

這將顯示頁面標(biāo)題的鏈接列表。

完整的tutsplus_list_subpages()函數(shù)全文應(yīng)當(dāng)如下:

<?php
function tutsplus_list_subpages() {
 
    // don't run on the main blog page
    if ( is_page() ) {
     
        // run the tutsplus_check_for_page_tree function to fetch top level page
        $ancestor = tutsplus_check_for_page_tree();
     
        // set the arguments for children of the ancestor page
        $args = array(
            'child_of' => $ancestor,
            'depth'    => '-1',
            'title_li' => '',
        );
         
        // set a value for get_pages to check if it's empty
        $list_pages = get_pages( $args );
         
        // check if $list_pages has values
        if ( $list_pages ) {
     
            // open a list with the ancestor page at the top
            ?>
            <ul class="page-tree">
                <?php // list ancestor page ?>
                <li class="ancestor">
                    <a href="<?php echo get_permalink( $ancestor ); ?>"><?php echo get_the_title( $ancestor ); ?></a>
                </li>
                 
                <?php
                // use wp_list_pages to list subpages of ancestor or current page
                wp_list_pages( $args );;
             
     
            // close the page-tree list
            ?>
            </ul>
     
        <?php
        }
    }
     
}
?>

激活tutsplus_list_subpages()函數(shù)

你可以采取下面兩種方式中任意一種來激活tutsplus_list_subpages()函數(shù):

  • 在你的一個主題模板文件中調(diào)用tutsplus_list_subpages(),比如sidebar.php文件。
  • 把它附加到你主題中的一個鉤子上。

例如,如果你的主題在sidebar.php文件中有一個tutsplus_sidebar鉤子,你可以將以下代碼添加到你的 functions.php文件中:

<?php add_action( 'tutsplus_sidebar', 'tutsplus_list_subpages' ); ?>

小結(jié)

我演示的這些代碼可以讓你在你的網(wǎng)站層次結(jié)構(gòu)中的任何位置自動添加相關(guān)網(wǎng)頁的列表。

如果你是在你的客戶網(wǎng)站上使用這些代碼,你需要確保客戶了解如何分層創(chuàng)建頁面,然而這并不意味著客戶需要做全面仔細(xì)的考慮。

如果你想要這些代碼對客戶來說變得更加人性化的話,你可以創(chuàng)建一個WordPress小工具(或者也許一段簡碼就夠了)來輸出頁面列表,而這又是另一個話題了。

原文出自:http://code.tutsplus.com/tutorials/creating-context-sensitive-sidebar-navigation-in-wordpress–cms-22359

由  stonetan@WordPress大學(xué) 原創(chuàng)翻譯,未經(jīng)允許,禁止轉(zhuǎn)載和采用本譯文。

聲明:本站所有文章,如無特殊說明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時(shí),禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號 WPDAXUE
WordPress開發(fā)

在正確的工具區(qū)域內(nèi)顯示你創(chuàng)建的WordPress小工具

2015-1-25 21:36:15

WordPress開發(fā)

WordPress 主題框架是如何工作的

2015-2-4 18:07:29

0 條回復(fù) A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

大石桥市| 嘉祥县| 黄大仙区| 山东| 阳山县| 靖州| 朝阳县| 安阳市| 道真| 松溪县| 保亭| 松滋市| 衡阳市| 新竹县| 益阳市| 社会| 佛教| 饶河县| 台北市| 龙南县| 灵丘县| 绥阳县| 保德县| 龙山县| 凤翔县| 资中县| 双鸭山市| 大方县| 荣昌县| 桃江县| 洮南市| 锡林浩特市| 客服| 汝州市| 巫山县| 平果县| 嘉义县| 吴堡县| 鄂伦春自治旗| 柘荣县| 乐都县|