通过下一个/上一个链接浏览页面兄弟

时间:2010-12-26 09:52:21

标签: wordpress wordpress-plugin

我正在使用WordPress作为我正在开发的网站的CMS。当我浏览帖子时,我可以使用下一个/上一个链接在帖子之间走动。我希望在网页上有相同的内容。

  • Page A
  • Page B
  • Page C

页面A应链接到下一个兄弟页面B.页面B应链接到上一个兄弟页面A和下一个兄弟页面C.页面C应该链接到上一个兄弟页面B.

您是否可以推荐生成这些链接的插件?我知道有some插件可以做到这一点,但我特别想要一个自动挂钩到我当前主题的插件。我知道如何编辑主题,但只要主题更新可用,这就会阻塞我的网站。

我正在使用LightWord WordPress主题。

6 个答案:

答案 0 :(得分:18)

将以下代码弹出到您的活动主题目录中的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'); ?>

它将回显上一页或下一页的链接。

如果你想要两个,你可以将函数留空,它将返回一个包含两个链接的数组。

答案 1 :(得分:7)

来自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 -->

答案 2 :(得分:2)

我从这里的答案开始的粗略片段(为了获得真实的链接和帖子标题) - 可能对某人有帮助!

$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; }

答案 3 :(得分:2)

这是另一个基于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; 
    }
}

答案 4 :(得分:1)

在尝试了几个解决方案后,尽量不安装另一个插件,最终唯一有效的是免费插件:Next Page, Not Next Post

(我在这里给出的解决方案存在问题,我想也许是因为我在Wordpress Admin的Pages部分手动命令页面。你可以手动选择页面顺序。所以我的第一页在层次结构的ID可能为100,第二个页面的ID可能为10.页面ID不是我的页面的排序方式。)

答案 5 :(得分:-1)

 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>
相关问题