我正在使用 wp_list_pages
设置侧边导航菜单,我想将子链接从 URL 转换为 anchor 链接,即 #link 而不是当前的 https://example.com/link由 wp_list_pages
生成。
我正在尝试使用以下代码:
<?php
$my_pages = wp_list_pages('echo=0&title_li=&child_of=5&depth=1');
$pieces = explode('"', $my_pages);
$i = 5;
$j = 3;
$limit = count($pieces);
for (;$i<$limit;) {
$tmp1 = '#'.$pieces[$j];
$pieces[$i] = $tmp1;
$i = $i+6;
$j = $j+6;
}
$tmp2 = implode('"',$pieces);
echo $tmp2;
?>
但它似乎很旧,我无法思考如何将它正确地实现到我当前的结构中。也许这段代码对我正在尝试做的事情毫无用处,但我找不到任何有用的东西。
这是我目前拥有的:
<div class="hero-container">
<?php
global $children;
global $post;
if ( $post->post_parent ) {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->post_parent,
'echo' => 0
) );
} else {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->ID,
'echo' => 0
) );
}
if ( $children ) : ?>
<?php echo '<div class="hero-side-menu">', '<h1>', get_the_title(), '</h1>', '<ul>', $children, '</ul>', '</div>' ?>
<?php endif; ?>
</div>
任何建议将不胜感激,因为我已经尝试解决这个问题几天了,但一无所获...也将不胜感激,因为我正在努力了解我哪里出错了!
我需要将父页面的子页面修改为具有#anchor 链接而不是 URLS,因为我已将页面压缩到它们的父页面中,但仍希望将它们作为侧边菜单中的选项。
澄清:我有一个包含子页面的页面,我将其称为父页面,但它们确实是子页面。我想保留子页面的 URLS,然后让子页面的子页面成为#links。
最佳答案
这有点取决于您希望 anchor 文本是什么。无论是 ID 还是标题等。但您可以根据需要修改它。
您可能想改用 get_pages(),这样您可以更好地控制输出。类似于以下内容:
<div class="hero-container">
<?php
global $post;
$current_page_title = $post->post_title; // current page title
$child_pages =
get_pages(
array(
'child_of' => $post->ID
),
);
if ($child_pages) : ?>
<div class="hero-side-menu">
<h1><?php echo $current_page_title; ?></h1>
<ul>
<?php foreach ($child_pages as $page) : ?>
<li><a href="<?php echo '#' . $page->post_title; ?>"><?php echo $page->post_title; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</div>
关于php - WordPress wp_list_pages - 将父项的子项从 URLS 更改为#anchor 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58312105/