【问题标题】:Loops through an array of pages in wordpress循环遍历 wordpress 中的页面数组
【发布时间】:2012-12-18 01:42:36
【问题描述】:

我在 wordpress 中遇到了这个功能。它会显示一个用于浏览页面的链接。

如果页面是A,那么下一页是B

如果页面是B,那么下一页是C,上一页是A

如果页面是C,那么上一页是B

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

但是,当它到达C时,“下一个”链接仍然是C。当页面是A时,“上一个”链接仍然是A。

如何修改,当它是A时,“上一个”链接是C,当它是C时,“下一个”链接是A?

谢谢。

【问题讨论】:

    标签: php arrays wordpress loops


    【解决方案1】:

    我想用以下方式重写它,它应该做你想要的:

    $last_item = end( $siblings );
    
    $before = ( $ID-1 > 0 ) ? get_permalink( $siblings[$ID-1]->ID ) : get_permalink( $last_item->ID );
    $after = ( $ID+1 < count( $siblings ) )  ? get_permalink( $siblings[$ID+1]->ID ) : $siblings[0]->ID;
    
    $closest = array( 'before' => $before, 'after' => $after );
    

    在哪里?是ternary operator

    【讨论】:

    • $closest = ...(仔细检查应该就能知道!)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 2016-08-11
    • 1970-01-01
    • 2014-06-04
    相关资源
    最近更新 更多