Prev / Next menu item

时间:2019-11-15 作者:Peter Morris

我的网站右侧有一个菜单https://blazor-university.com/

在菜单上方,我需要上一个菜单项和下一个菜单项的链接。我找不到任何能这样做的东西。怎么做到的?

1 个回复
最合适的回答,由SO网友:Peter Morris 整理而成

我写了这个短代码插件。要使用它,请将以下短代码添加到任何页面或短代码小部件中。

[menu_navigator menu=\'The name of your menu\']

menu 如果未指定,将默认为“目录”。

要安装,只需保存到名为shortcode-blazoruniversity-menu-navigator.php, 将其压缩,然后作为插件上传。

<?php

/**
 * Plugin Name: Menu navigator
 * Description: Previous/Next links based on a menu
 * Version: 1.0.0
 * Author: Peter Morris
 * Author URI: https://blazoruniversity.com
 * License: GPLv2 or later
 * Text Domain: blazoruniversity
 * @package blazoruniversity_menu_navigator
 * @author Peter Morris
 */

// If accessed directly, exit
if (!defined(\'ABSPATH\')) {
    exit;
}

if (!class_exists(\'BlazorUniversity_Menu_Navigator\')) {
    class BlazorUniversity_Menu_Navigator
    {
        public function __construct() {
            add_shortcode(\'menu_navigator\', array($this, \'render\'));
        }

        public function render($atts) {
            $result = \'\';

            $atts = array_change_key_case((array) $atts, CASE_LOWER);

            $atts = shortcode_atts(array(\'menu\' => \'Table of contents\'), $atts);

            $menu = $atts[\'menu\'];

            $menu_items = wp_get_nav_menu_items($menu);
            if (!$menu_items) {
                return \'Menu not found: \' . $menu;
            }

            if (count($menu_items) == 0) {
                return \'Menu is empty: \' . $menu;
            }

            global $post;
            $this_id = $post->ID;
            $found_index = -1;
            for ($i = 0; $i < count($menu_items); $i++) {
                $menu_item = $menu_items[$i];
                if ($menu_item->object_id == $this_id) {
                    $found_index = $i;
                    break;
                }
            }

            if ($found_index == -1) {
                $found_index = 0;
            }

            $previous_url = NULL;
            $previous_title = NULL;
            $next_url = NULL;
            $next_title = NULL;

            if ($found_index > 0) {
                $previous_menu_item = $menu_items[$found_index - 1];
                $previous_id = $previous_menu_item->object_id;
                $previous_url = get_permalink($previous_id);
                $previous_title = get_the_title($previous_id);
            }

            if ($found_index < count($menu_items) - 1) {
                $next_menu_item = $menu_items[$found_index + 1];
                $next_id = $next_menu_item->object_id;
                $next_url = get_permalink($next_id);
                $next_title = get_the_title($next_id);
            }

            if ($previous_url != NULL || $next_url != NULL) {
                $result .= \'<div class="menu-navigator">\';
                if ($previous_url != NULL) {
                    $result .= \'<a href="\' . $previous_url . \'">\';
                    $result .= \'<div class="previous">\';
                    $result .= $previous_title;
                    $result .= \'</div>\';
                    $result .= \'</a>\';
                }

                if ($next_url != NULL) {
                    $result .= \'<a href="\' . $next_url . \'">\';
                    $result .= \'<div class="next">\';
                    $result .= $next_title;
                    $result .= \'</div>\';
                    $result .= \'</a>\';
                }
                $result .= \'</div>\';
            }

            return $result;
        }
    }
}

new BlazorUniversity_Menu_Navigator();

?>