php - 获取子菜单项和回显
问题描述
我正在尝试打开活动页面的子菜单项,并使用 wordpress 查询将它们回显。我已经想出为菜单的帖子类型执行此操作,我有点挣扎。这是我正在使用的帖子类型版本。
<div class="container">
<div class="grid mb-10 mt-10 w-full text-white sm:grid-cols-3 gap-4">
<?php
// WP_Query arguments
wp_nav_menu( array(
'menu' => 'Menu Name',
'sub_menu' => true,
'show_parent' => true
) );
// The Query
$services = new WP_Query( $args );
// The Loop
if ( $services->have_posts() ) {
while ( $services->have_posts() ) {
$services->the_post();
?>
<div class="p-5 bg-gray-300">
<h3 class="pt-5 pb-0 mb-0 capitalize"><a>THIS IS WHERE I NEED TO ECHO THE TITLE</h3></a>
<button class="btn btn-blue mt-5 mb-5"><a href="<?php the_permalink();?> ">read more</a></button>
</div>
<?php wp_reset_postdata(); ?>
<?php }}?>
</div>
解决方案
在你的functions.php文件中放这个函数:
function display_related_menu_pages() {
// Get all menu items
// replace "Menu Name" by your menu name or id
$menu_items = wp_get_nav_menu_items('Menu Name');
// Get the current item (current page)
$current_item = false;
foreach($menu_items as $item) {
if((int)$item->object_id === get_the_id()) {
$current_item = $item;
}
}
// Get the parent ID
$parent_id = false;
if($current_item->menu_item_parent != 0) {
$parent_id = $current_item->menu_item_parent;
}else {
$parent_id = $current_item->ID;
}
// Prepare items to display
$items_to_display = [];
// Get only items that have the parent_id or the item that is the parent_id
foreach($menu_items as $item) {
if(($parent_id == $item->ID || $parent_id == $item->menu_item_parent) && $item->ID !== $current_item->ID ) {
$items_to_display[] = $item;
}
}
// Display items
foreach($items_to_display as $item) :
?>
<div class="p-5 bg-gray-300">
<h3 class="pt-5 pb-0 mb-0 capitalize"><a><?php echo $item->title; ?></h3></a>
<button class="btn btn-blue mt-5 mb-5"><a href="<?php echo $item->url; ?>"><?php _e('read more', 'your-theme-text-domain'); ?></a></button>
</div>
<?php
endforeach;
}
然后在你的模板中调用你希望它显示的函数
display_related_menu_pages();
您还可以使用仅返回菜单项的功能,如下所示:
function get_related_menu_pages() {
// Get all menu items
// replace "Menu Name" by your menu name or id
$menu_items = wp_get_nav_menu_items('Menu Name');
// Get the current item (current page)
$current_item = false;
foreach($menu_items as $item) {
if((int)$item->object_id === get_the_id()) {
$current_item = $item;
}
}
// Get the parent ID
$parent_id = false;
if($current_item->menu_item_parent != 0) {
$parent_id = $current_item->menu_item_parent;
}else {
$parent_id = $current_item->ID;
}
// Prepare items to display
$items_to_display = [];
// Get only items that have the parent_id or the item that is the parent_id
foreach($menu_items as $item) {
if(($parent_id == $item->ID || $parent_id == $item->menu_item_parent) && $item->ID !== $current_item->ID ) {
$items_to_display[] = $item;
}
}
return $items_to_display;
}
并处理模板中的显示:
$related_pages = get_related_menu_pages();
// Display items
foreach($related_pages as $item) :
?>
<div class="p-5 bg-gray-300">
<h3 class="pt-5 pb-0 mb-0 capitalize"><a><?php echo $item->title; ?></h3></a>
<button class="btn btn-blue mt-5 mb-5"><a href="<?php echo $item->url; ?>"><?php _e('read more', 'your-theme-text-domain'); ?></a></button>
</div>
<?php
endforeach;
推荐阅读
- loops - 使用“rvest”进行多链接网页抓取。错误参数意味着不同的行数
- xpath - 如何根据之前 HTML 类中的文本抓取内容
- python - python中的简单蒙特卡罗模拟
- angular - 如何为 Angular“@ckeditor/ckeditor5-angular”添加插件到 CKEditor?
- javascript - 使用 select 在特定 URL 上显示 2 个不同的 div
- python - 在python中搜索所有数据集值
- php - 在 Woocommerce 3 中检查产品库存可用性
- reactjs - 反应数字缓动
- php - 主键错误(自动增量 int)在 php 中调用存储过程
- playframework - 无法创建 PlayFramework@2.7.x 应用程序