自动链接页脚中的每个页面 PHP / jQuery 中的下一页和上一页导航

问题描述 投票:0回答:1

我有 100 个 php 页面,而且全都是静态页面。我希望在每个页脚中都有下一页/上一页链接,该链接应该自动工作,而不是手动设置。所有页面都将具有“组件/按钮”、“组件/工具提示”、“设计/令牌”和“开始使用”等 URL。

与页脚中的

碳设计系统相同。

我希望有一种更简单的方法来自动链接每个组件页面。

我尝试了一些 jQuery 但没有得到所需的结果。

php jquery url navigation anchor
1个回答
0
投票
这是我整理的一个非常简单的解决方案。您只需将要包含的每个页面添加到 PHP 数组中,然后在每个页面上包含脚本即可。页面根据其在数组中的位置进行排序。

页脚导航.php:

<?php // Array of pages in the order they should appear $pages = [ '/test.php', '/files/file1.php', '/files/file2.php' ]; $currentUrl = $_SERVER['REQUEST_URI']; $currentIndex = array_search($currentUrl, $pages); $prevIndex = $currentIndex > 0 ? $currentIndex - 1 : null; $nextIndex = $currentIndex < count($pages) - 1 ? $currentIndex + 1 : null; $prevUrl = $prevIndex !== null ? $pages[$prevIndex] : '#'; $nextUrl = $nextIndex !== null ? $pages[$nextIndex] : '#'; ?> <footer> <?php if($prevIndex !== null): ?> <a href="<?php echo $prevUrl; ?>">Previous</a> <?php endif; ?> <?php if($nextIndex !== null): ?> <a href="<?php echo $nextUrl; ?>">Next</a> <?php endif; ?> </footer>
要将导航添加到每个页面,请将以下内容放在页脚中:

<?php include 'footer-navigation.php'; ?>
确保与 

footer-navigation.php 不在同一目录中的页面路径正确。

© www.soinside.com 2019 - 2024. All rights reserved.