wp_nav_menu查找下一个,上一个链接 [英] wp_nav_menu find next, previous links

查看:56
本文介绍了wp_nav_menu查找下一个,上一个链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用wp_nav_menu设置一些分页,所以我需要访问菜单中的上一个,当前和下一个项目.有什么想法吗?

I need to set up some pagination using wp_nav_menu, so I need to access the previous, current and next items in a menu. Any ideas?

推荐答案

好的,这就是我想出的-它可以正常工作.

Ok, here's what I came up with - and it works.

<?php
$menu_name = 'main-menu';
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );

$i=-1;
foreach ( $menuitems as $item ):

    $i++;

  $id = get_post_meta( $item->ID, '_menu_item_object_id', true );
  $page = get_page( $id );
  $link = get_page_link( $id );

    $linkarray.=$id.",";
    $urlarray.=$link.",";

  if ($id==$post->ID){
    $previd=$i-1;
    $nextid=$i+1;
  }
endforeach;

$linkarray=explode(',',$linkarray);
$urlarray=explode(',',$urlarray);

$nextid=$urlarray[$nextid];
if (empty($nextid)){
    $nextid=$urlarray[0];
}
$previd=$urlarray[$previd];
if (empty($previd)){
    $previd=$urlarray[$i];
}
?>

<a href="<?php echo $nextid; ?>">Next Item</a>
<a href="<?php echo $previd; ?>">Previous Item</a>

这篇关于wp_nav_menu查找下一个,上一个链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆