WordPress 菜单需要 wp post 元素

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

尝试将页面元素添加到菜单会导致 WordPress 挂起 - 在 debug.log 中,我看到类似的错误,就像它期待

WP_Post Element
(在这种情况下没有任何意义)。如何限制 WordPress 只接受
wp_post
元素作为菜单元素?或者反过来问 - 我必须在哪里挖掘才能禁用此功能? 非常感谢任何帮助。

[03-Nov-2024 10:09:51 UTC] PHP Fatal error: Uncaught TypeError: 
Elementor\TemplateLibrary\Source_Local::on_save_post(): Argument #2 ($post) 
must be of type WP_Post, null given, called in 
/home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-includes/class-wp-hook.php 
on line 326 and defined in 
/home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-content/plugins/elementor/includes/template-library/sources/local.php:1112 

Stack trace:
#0 /home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-includes/class-wp-hook.php(326): 
   Elementor\TemplateLibrary\Source_Local->on_save_post()
#1 /home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-includes/class-wp-hook.php(348): 
   WP_Hook->apply_filters()
#2 /home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-includes/plugin.php(517): 
   WP_Hook->do_action()
#3 /home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-includes/post.php(4891): 
   do_action()
#4 /home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-includes/nav-menu.php(541): 
   wp_insert_post()
#5 /home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-admin/includes/nav-menu.php(1184): 
   wp_update_nav_menu_item()
#6 /home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-admin/includes/ajax-actions.php(1567): 
   wp_save_nav_menu_items()
#7 /home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-includes/class-wp-hook.php(324): 
   wp_ajax_add_menu_item()
#8 /home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-includes/class-wp-hook.php(348): 
   WP_Hook->apply_filters()
#9 /home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-includes/plugin.php(517): 
   WP_Hook->do_action()
#10 /home/u628460773/domains/abcacademy.com/public_html/abcdev/wp-admin/admin-ajax.php(192): 
    do_action()
#11 {main}
php wordpress menu themes
1个回答
0
投票

您需要自定义 WordPress 处理菜单项的方式。

按照以下步骤进行操作,使其只接受 wp_post:

  1. 使用自定义 CSS 或 JavaScript 隐藏不需要的菜单项类型 您可以通过将其添加到主题的functions.php文件或自定义插件中,使用CSS或JavaScript隐藏菜单界面中的选项。此方法在视觉上隐藏了元素,但并没有完全删除它们的功能。

  2. 过滤可用的菜单项 您可以使用 wp_nav_menu_items 过滤器来限制导航菜单中显示的项目。以下是如何仅添加帖子作为菜单项选项的示例:

    add_filter('wp_nav_menu_objects', function($items) {
    foreach ($items as $key => $item) {
        // Check if the menu item is not a post
        if ($item->object !== 'post') {
            unset($items[$key]); // Remove the item from the menu
        }
    }
    return $items;
    

    });

此过滤器在 WordPress 获取要显示的菜单项时运行,因此只有帖子出现在前端。

3.  Customize the WordPress Admin UI with JavaScript

您可以在 WordPress 管理员中使用 JavaScript 来隐藏其他类型菜单项的选项。将此代码添加到functions.php文件中:

function restrict_menu_items() {
?>
<script type="text/javascript">
    jQuery(document).ready(function($) {
        // Hide pages, custom links, categories, etc.
        $('#posttype-page, #custom, #category').hide();
    });
</script>
<?php

} add_action('admin_head-nav-menus.php', 'restrict_menu_items');

  1. 使用 wp_get_nav_menu_items 仅限制帖子 修改 wp_get_nav_menu_items 过滤器以限制菜单查询仅包含帖子类型:

    add_filter('wp_get_nav_menu_items', 'restrict_menu_to_posts', 10, 3); 函数restrict_menu_to_posts($items, $menu, $

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