3

I would like to add a new section in the custom menu panel that would allow predetermined URLs to be added to a custom menu.

For example, I would like a custom post type archive page. I know that they can be added manually by specifying the URL in the custom link section, but then if the site URL changes then the custom menu needs to be changed as well.

Can someone please point me in the correct direction for this?

1 Answer 1

1

I have made a custom menu for the nav menu page to add post type archives just like posts/pages etc. I don't think it's possible to make it change the url on siteurl change. You could add the url as /slug/ instead of http://url.com/slug/, but that won't work with WordPress in a folder.

function add_post_types_nav_menu() {
    add_meta_box('post_types_meta_box', __( 'Post type archives' ), 'render_add_post_types_nav_menu', 'nav-menus', 'side', 'default' );
}
add_action('admin_init', 'add_post_types_nav_menu');

function render_add_post_types_nav_menu() {
    $post_types = get_post_types(array('public' => true, '_builtin' => false), 'objects', 'and');
    ?>
    <div id="posttype-archive" class="posttypediv">
        <ul class="posttype-tabs add-menu-item-tabs">
            <li class="tabs"><?php _e( 'Post types' ); ?></li>
        </ul>
        <div class="tabs-panel tabs-panel-active">
            <ul class="categorychecklist form-no-clear">
            <?php $i = 0; foreach ($post_types  as $post_type ) : $i++; ?>
                <li>
                    <label class="menu-item-title"><input type="checkbox" class="menu-item-checkbox" name="menu-item[-<?php echo $i; ?>][menu-item-object-id]" value="<?php echo $post_type->name; ?>"> <?php echo $post_type->labels->name; ?></label>
                    <input type="hidden" class="menu-item-title" name="menu-item[-<?php echo $i; ?>][menu-item-title]" value="<?php echo $post_type->labels->name; ?>">
                    <input type="hidden" class="menu-item-url" name="menu-item[-<?php echo $i; ?>][menu-item-url]" value="<?php echo get_post_type_archive_link($post_type->name); ?>">
                    <input type="hidden" value="custom" name="menu-item[-<?php echo $i; ?>][menu-item-type]">
                </li>
            <?php endforeach; ?>
            </ul>
        </div>
        <p class="button-controls">
            <span class="list-controls">
                <a href="/wp-admin/nav-menus.php?page-tab=all&amp;selectall=1#posttype-archive" class="select-all">Select All</a>
            </span>

            <span class="add-to-menu">
                <input type="submit" class="button-secondary submit-add-to-menu right" value="Add to Menu" name="add-post-type-menu-item" id="submit-posttype-archive">
                <span class="spinner"></span>
            </span>
        </p>
    </div>
    <?php
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.