Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created a metabox that takes a name that the user can give as an additional name of their post. I am wondering how I can automatically create a menu item based on the name the user input into the metabox, and set the url to #PostName?

Even if someone could guide me in the right direction on creating the menu item from a metabox on the post screen I could figure out the rest.

EDIT: I have added some extra info since I understand it seems a little confusing to someone who has know idea of why I want this.

I am creating a custom theme with the html5 boilerplate. It is a one page theme that users add sections to the page by adding posts. The theory is the user adds a custom name to the post into a meta box on the page and it will add the menu item with that name. Since the meta box I created already adds an ID in the html of each post with an additional ID tag the url for the menu will be #PostName. This is so when the user clicks the menu item it will automatically scroll right to the section.

In most themes the Nav menu brings you to a different page. Since my theme is only a one page theme the nav links will bring the user to the section on the page. All I am trying to accomplish is to add some automation in the theme so the users don't need to add the menu items manually.

share|improve this question
    
I do not understand this question. –  s_ha_dum Apr 21 '13 at 4:26
    
I want to programmatically add menu items from the post page. –  user1632018 Apr 21 '13 at 4:36
    
A menu item for what? What menu? –  s_ha_dum Apr 21 '13 at 14:12
    
Navigational menu. I am creating a custom theme with the html bootstrap. It is a one page theme that users add sections to the page by adding posts. The theory is the user adds a custom name to the post from a meta box for the menu and it will add the menu item with that name. Since it already adds an ID in the html the url for the menu will be #PostName. This is so when the user clicks the menu item it will automatically scroll right to the section. –  user1632018 Apr 21 '13 at 20:19
    
@s_ha_dum I have added some addition information into my original post. –  user1632018 Apr 21 '13 at 20:29
add comment

1 Answer

Here's how I understand this task: get posts (you can shape this any way you want by adding extra parameters to the query), for each post get its meta-box value (replace the key with yours), if value found add it to the menu's HTML.

Note: code is not tested, it is more of an outline.

$html = '';
$domain = 'your-domain'; // http://codex.wordpress.org/Function_Reference/_e
$meta_key = 'your-meta-key'; // TODO replace value with yours

$post_menu_items = get_posts( array(
    'post_type'     =>  'post',
    'post_status'   =>  array( 'publish' ),
    'numberposts'   =>  -1
) );

if($post_menu_items){
    $html .= '<ul>';
    foreach ($post_menu_items as $key => $menu_item) {
        $meta_title = get_post_meta($menu_item->ID, $meta_key, true);
        if(!empty($current_meta)){
            $html .='<li><a href="#' . $meta_title . '">' . $menu_item->post_title . '</a></li>';
        }
    }
    $html = '</ul>';
} else {
    _e('No posts found.', $domain);
}
echo $html;

Thinking about it, you could even avoid the get_post_meta call by supplying a meta_key parameter into your get_posts. You're welcome to experiment with that.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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