簡體   English   中英

通過下一個/上一個鏈接瀏覽頁面兄弟

[英]Browsing page siblings through next/previous links

我正在使用WordPress作為我正在開發的網站的CMS。 當我瀏覽帖子時,我可以使用下一個/上一個鏈接在帖子之間走動。 我想在頁面上有相同的東西。

  • 頁面A.
  • 第B頁
  • 第C頁

頁面A應鏈接到下一個兄弟頁面B.頁面B應鏈接到上一個兄弟頁面A和下一個兄弟頁面C.頁面C應鏈接到上一個兄弟頁面B.

是否有任何可以推薦的插件可以生成這些鏈接? 我知道有一些插件可以做到這一點,但我特別想要一個自動掛鈎到我當前主題的插件。 我知道如何編輯主題,但只要主題更新可用,就會阻止我的網站。

我正在使用LightWord WordPress主題。

將以下代碼彈出到活動主題目錄中的functions.php文件中:

function siblings($link) {
    global $post;
    $siblings = get_pages('child_of='.$post->post_parent.'&parent='.$post->post_parent);
    foreach ($siblings as $key=>$sibling){
        if ($post->ID == $sibling->ID){
            $ID = $key;
        }
    }
    $closest = array('before'=>get_permalink($siblings[$ID-1]->ID),'after'=>get_permalink($siblings[$ID+1]->ID));

    if ($link == 'before' || $link == 'after') { echo $closest[$link]; } else { return $closest; }
}

要調用該函數:

<?php siblings('before'); ?>

要么

<?php siblings('after'); ?>

它將回顯前一頁或下一頁的鏈接。

如果你想要兩個,你可以將函數留空,它將返回一個包含兩個鏈接的數組。

來自wordpress Codex

<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}

$current = array_search(get_the_ID(), $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>

<div class="navigation">
<?php if (!empty($prevID)) { ?>
<div class="alignleft">
<a href="<?php echo get_permalink($prevID); ?>"
title="<?php echo get_the_title($prevID); ?>">Previous</a>
</div>
<?php }
if (!empty($nextID)) { ?>
<div class="alignright">
<a href="<?php echo get_permalink($nextID); ?>" 
 title="<?php echo get_the_title($nextID); ?>">Next</a>
 </div>
 <?php } ?>
</div><!-- .navigation -->

從我在這里得到的答案開始的粗略片段(獲得真實的鏈接和行動中的帖子標題) - 可能對某人有幫助!

$closest = array('before'=>get_permalink($siblings[$ID-1]->ID),'after'=>get_permalink($siblings[$ID+1]->ID));
$name =  array('before'=>get_the_title($siblings[$ID-1]->ID),'after'=>get_the_title($siblings[$ID+1]->ID));

if ($link == 'before' || $link == 'after') { 
echo '<a href="' . $closest[$link] . '">';
echo '<span>' . $name[$link] . '</span></a>';
}
else { return $closest; }

這里的另一個功能基於jackreichert的答案。 當你位於兄弟姐妹列表的末尾時,它會附加到最后/第一個兄弟的附加功能,所以你可以無休止地循環。 它還使用菜單順序。

function get_sibling_link($link) {
    global $post;

    $siblings = get_pages('sort_column=menu_order&child_of='.$post->post_parent.'&parent='.$post->post_parent);

    foreach ($siblings as $key => $sibling){
        if ($post->ID == $sibling->ID){
            $current_id = $key;
        }
    }

    $closest = [ 
        'before' => get_permalink( $siblings[$current_id-1]->ID ),
        'after'  => get_permalink( $siblings[$current_id+1]->ID )
    ];

    if($siblings[$current_id-1]->ID == '' ){
        $closest['before'] = get_permalink( $siblings[count($siblings)-1]->ID );
    }

    if($siblings[$current_id+1]->ID == '' ){
        $closest['after'] = get_permalink( $siblings[0]->ID );
    }

    if ($link == 'before' || $link == 'after') { 
        echo $closest[$link]; 
    } else { 
        return $closest; 
    }
}

在嘗試了幾個解決方案之后,盡量不安裝另一個插件,最終唯一有效的是免費插件: 下一頁,不是下一篇文章

(我在這里給出的解決方案存在問題,我想也許是因為我在Wordpress Admin的Pages部分手動訂購頁面。你可以手動選擇頁面順序。所以層次結構中的第一頁可能有100的ID,並且第二頁的ID可能為10.頁面ID不是我的頁面訂購方式。)

 function siblings()
    {
        global $post;
        $args = array(
                        'sort_order'    => 'ASC',
                        'sort_column'   => 'menu_order',
                        'hierarchical'  => 1,
                        'child_of'      => $post->post_parent,
                        'parent'        => $post->post_parent,
                        'exclude_tree'  => '',
                        'offset'        => -1,
                        'post_type'     => 'page',
                        'post_status'   => 'publish'
            );
        $siblings = get_pages($args);

        foreach ($siblings as $key=>$sibling)
        {
            if ($post->ID == $sibling->ID)
            {
                $ID = $key;
            }
        }
        $closest = array(
                            'back'  => array('perm_link' => get_permalink(isset($siblings[$ID -1]->ID) ?  $siblings[$ID -1]->ID : $siblings[$ID]->ID), 'title' => get_the_title(isset($siblings[$ID -1]->ID) ?  $siblings[$ID -1]->ID : $siblings[$ID]->ID)),
                            'next'  => array('perm_link' => get_permalink(isset($siblings[$ID +1]->ID) ?  $siblings[$ID +1]->ID : $siblings[$ID]->ID) , 'title' => get_the_title(isset($siblings[$ID +1]->ID) ?  $siblings[$ID +1]->ID : $siblings[$ID]->ID))
                        );

        return $closest;

    }

然后使用如下:

    <?php
$newrenderViews = new renderViews() ;
$back_next      = $newrenderViews->siblings();
$next           = $back_next['next'] ;
$next_title     = $next['title'] ;
$next_link      = $next['perm_link'] ;
$back           = $back_next['back'] ;
$back_title     = $back['title'] ;
$back_link      = $back['perm_link'] ;
?>

<div  class="section" >
    <div class="content_wrapper " style="background-color: #8A8C8F; width: 970px;">
    <p class="navbar-text pull-right">
        <a href="<? echo $next_link; ?>" class="navbar-link" style="color: #ffffff;">Next - <?php echo $next_title ;  ?></a>
    </p>
    <p class="navbar-text pull-left">
        <a href="<? echo $back_link; ?>" class="navbar-link" style="color: #ffffff;margin-left: 340px;">Go back - <?php  echo $back_title ;   ?></a>
    </p>
</div>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM