gpt4 book ai didi

php - Wordpress:将事件类添加到 $parent_title

转载 作者:太空宇宙 更新时间:2023-11-04 13:29:04 24 4
gpt4 key购买 nike

我正在显示一个列出父页面和子页面的边栏菜单。

  • 家长
  • 子页一
  • 子页面二
  • 子页三

我的代码将类 .current_page_item 应用到事件的子页面,而不是事件的父页面。我希望父级在该页面上也有 .current_page_item 类。

<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) {
$parent_title = get_the_title($post->post_parent);?>
<ul id="sub">
<li><a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $parent_title;?></a></li>
<?php echo $children; ?>
</ul>
<?php } ?>

这里是输出的概念:

<ul id="sub">
<li><a href="#">Parent</a></li>
<li class="page_item page-item-19 current_page_item"><a href="#">Child page</a></li>
<li class="page_item page-item-21"><a href="#">Child page</a></li>
<li class="page_item page-item-23"><a href="#">Child page</a></li>
</ul>

最佳答案

您应该使用 get_pageswp_get_nav_menu_items。任何返回帖子对象数组的 WordPress 函数。

$children = wp_get_nav_menu_items( 'Main Menu' ); // dump to make sure this is an array of objects
echo "<ul id='sub'>";
foreach($children as $item) {
$class = '';
if ( $item->object_id == $post->post_parent ){
$class = 'current_page_ancestor';
} else if ( $item->object_id == $post->ID ){
$class = 'current_page_item';
}
echo "<li " . $class . "><a href='" . $item->url . "'>" . $item->title . "</a></li>";
}
echo "</ul>";

这会获取一组 WordPress 帖子,然后循环遍历它们。

如果当前迭代中的帖子 ID 与您正在查看的帖子的父级相同,请添加“current_page_ancestor”类。

如果当前迭代中的帖子 ID 与您正在查看的帖子相同,请添加“current_page_item”类。

关于php - Wordpress:将事件类添加到 $parent_title,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23494502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com