0

我在 wordpress 上有一些虚拟页面,当页面创建或标题更改时,页面存储在瞬态数据中。我正在尝试在 nav-menus.php 的元框上添加这些页面。几乎一切都按预期工作,但后端缺少 url,而前端的链接带有href="" 虚拟页面 代码:

<?php
class MP_MenuVirtualPages {
    public function __construct() {
        add_action('admin_init', array( $this, 'virtualpages_register_menu_metabox' ) ) ;
    }

    public function virtualpages_register_menu_metabox() {
        add_meta_box( 
            'virtualpages-menu-test-metabox', 
            __( 'Virtual pages', MP_TEXTDOMAIN ), 
            array( $this, 'render_manu_virtualpages_metabox' ), 
            'nav-menus', 
            'side', 
            'default'
        );
    }
    static public function render_manu_virtualpages_metabox( $object, $args ) {
        global $nav_menu_selected_id;
        global $wpdb;
        $sql = "SELECT `option_name` AS `name`, `option_value` AS `value`
        FROM  $wpdb->options
        WHERE `option_name` LIKE '%transient_gm_virtualpage%'
        ORDER BY `option_name`";

        $results = $wpdb->get_results( $sql );
        $virtualpages_items = array();
        foreach ( $results as $id => $result )
        {
            $page = unserialize( $result->value );
            $virtualpages_items[] = (object) array(
                'ID' => $id,
                'db_id' => 0,
                'menu_item_parent' => 0,
                'object_id' => 1,
                'post_parent' => 0,             
                'type' => 'virtualpages-custom-type',
                'object' => 'virtualpages-object-slug',
                'type_label' => '',
                'title' => $page['title'],
                'url' => home_url( $page['url'] ),
                'target' => '',
                'attr_title' => '',
                'description' => '',
                'classes' => array(),
                'xfn' => '',
            );
        }

        $walker = new Walker_Nav_Menu_Checklist( );

        ?>
        <div id="virtualpages-plugin-div">
            <div id="tabs-panel-virtualpages-plugin-all" class="tabs-panel tabs-panel-active">
                <ul id="virtualpages-plugin-checklist-pop" class="categorychecklist form-no-clear" >
                    <?php 
                    echo walk_nav_menu_tree( 
                        array_map( 'wp_setup_nav_menu_item', $virtualpages_items ), 0, (object) array( 'walker' => $walker )
                    ); 
                    ?>
                </ul>

                <p class="button-controls">
                    <span class="list-controls">
                        <a href="<?php
                        echo esc_url(add_query_arg(
                            array(
                                'virtualpages-plugin-all' => 'all',
                                'selectall' => 1,
                            )
                        ));
                        ?>#virtualpages-menu-test-metabox" class="select-all"><?php _e( 'Select All' ); ?></a>
                    </span>

                    <span class="add-to-menu">
                        <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-virtualpages-plugin-menu-item" id="submit-virtualpages-plugin-div" />
                        <span class="spinner"></span>
                    </span>
                </p>
            </div>
        </div>
        <?php
    }
}
new MP_MenuVirtualPages();

知道我缺少什么吗?

4

1 回答 1

1

解决方案:

typeon$virtualpages_items[]必须是而'custom'不是'type' => 'virtualpages-custom-type',

于 2018-04-15T20:12:38.253 回答