dtvpl739577
2014-07-25 14:56
浏览 30
已采纳

向Wordpress菜单项显示自定义发布元数据

this is my first post so first of all : hello everybody, glad to be a part of the community. Secondly please escuse-me in advance for all the English mistakes, i'm french ...

Now this is the problem i have with Wordpress as i'm not a good developer for now :

I've created a custom post type based on the page model named 'Projects' for a more friendly display in the admin :

add_action( 'init', 'create_post_type' );

function create_post_type() {
    register_post_type( 'Projects', array(
        'labels' => array(
            'name' => __( 'Menu Projects' ),
            'singular_name' => __( 'Project' )
        ),
        'hierarchical' => true,
        'capability_type' => 'page',
        'supports' => array('title','editor','thumbnail','custom-fields'),
        'public' => true,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-art'
    ));
}

Then i've add some custom fields i need :

add_action('wp_insert_post', 'wpc_champs_personnalises_defaut');

function wpc_champs_personnalises_defaut($post_id){
if( $_GET['post_type'] = 'Projects' ){
    add_post_meta(1, 'company', '', true);
    add_post_meta(2, 'année', '', true);
}
return true;
};

And now what I want is to display those post meta values in my menu items , right next to the name of the page !

I've tried different things unsucessfully, so if a Wordpress master could help me, that would be great ! Thanks in advance , have a good day !

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanduan1993 2014-07-25 15:07
    已采纳

    When you add the post meta, you should supply the $post_id as the first argument.

    add_post_meta( $post_id, 'company', 'value here' );
    

    and when you want to the post meta you can use get_post_meta function

    get_post_meta( $post_id, 'company', true );
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsgixl5195 2014-07-31 10:42

    This is finaly how a manage to display those metas from custom fields next to each menu item. In my exemple Company and Year :

    /* DISPLAY CUSTOM MENU */
        wp_nav_menu( array(
            'theme_location'    => 'secondary'
        ) );
    

    Get the menu and the items in arrays

    $menu = wp_get_nav_menu_object('Projects Menu');
        var_dump($menu);
    
        $items = wp_get_nav_menu_items( $menu->term_id);
        var_dump($items);
    

    Get all the meta in 1 array, then 1 array for each meta

    /* GET IDs-COMPANIES-YEAR OF EACH ITEM*/
    foreach ( (array) $items as $key => $menu_item ) {
        $id = $menu_item->object_id;
    
        $meta_id = $menu_item->ID;
    
        $meta_company = get_post_meta( $id, 'company', true );
    
        $meta_year = get_post_meta( $id, 'année', true );
    
        $meta_array[] = array($meta_id, $meta_company, $meta_year);
    }
    var_dump($meta_array);
    
    /* GET THE IDs OF THE ITEM */
    foreach ($meta_array as $key => $value) {
        $meta_array_id[] = $meta_array[$key][0];
    }
    var_dump($meta_array_id);
    
    /* GET THE COMPANIES NAMES OF THE ITEM */
    foreach ($meta_array as $key => $value) {
        $meta_array_company[] = $meta_array[$key][1];
    }
    var_dump($meta_array_company);
    
    /* GET THE YEARS OF THE ITEM */
    foreach ($meta_array as $key => $value) {
        $meta_array_year[] = $meta_array[$key][2];
    }
    var_dump($meta_array_year);
    
    }
    

    Then convert to javascript arrays to display:

    /* GET STRINGIFY IDs IN A JS ARRAY*/
            var js_meta_array_id = [<?php echo '"'.implode('","', $meta_array_id).'"' ?>];
            console.log(js_meta_array_id);
            console.log(js_meta_array_id[0]);
    
            /* GET COMPANIES NAMES IN A JS ARRAY*/
            var js_meta_array_company = [<?php echo '"'.implode('","', $meta_array_company).'"'   ?>];
            console.log(js_meta_array_company);
    
            /* GET YEARS IN A JS ARRAY*/
            var js_meta_array_year = [<?php echo '"'.implode('","', $meta_array_year).'"' ?>];
            console.log(js_meta_array_year);
    

    Target the items

    /* GET MENU PROJECTS MENU */
            var menu_project_id = $('#menu-projects-menu');
            console.log(menu_project_id);
    
            /*GET MENU PROJECTS ITEMS ID*/
            var item = menu_project_id.children('.menu-item');
            console.log(item);
    

    And Finally add the meta to items :

    var length = item.length,
            element = null;
    
            for (var i = 0; i < length; i++) {
            element = item[i];
                var item_id = (element.id);
    
                $( "#"+item_id ).append( "<span class='company'>" +js_meta_array_company[i]+ "</span><span class='year'>" +js_meta_array_year[i]+ "</span>" );
            }
    

    This is maybe quite heavy but it works fine for me !

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题