• Hello everybody.

    Using the **Toolset Types** plugin, I’ve created some custom post types with their relatives custom fields. I’d like to display a nav menu on my site with the list of the “pages” I’ve created under a certain **Custom Post**.

    By registering a new menu in my functions.php theme file, I managed to do that, no problem till that point. Now I’d need to**display a custom field** as an <img></img> tag inside the <li></li>
    outputted by the WordPress core nav.

    I’m not an expert when it comes to php, but I can hack my way around it.
    Any help will be much appreciated.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Sounds like you just want to loop your post type instead try this code:

    $myquery = array('post_type' => 'YOUR POST TYPE', 'posts_per_page' => -1);
    $mydata = get_posts($myquery);
    
    echo('<ul>');
    foreach($mydata as $dk => $dv){
    $meta = get_post_meta($dv->ID);
    //print_r($meta);
    echo('<li><a href="' . get_permalink($dv->ID) . '">' . $dv->post_title . '</a></li>');
    }
    echo('</ul>');

    This should output your post type with links.

    If you noticed the //print_r if you take out the // it will show all the custom field data for that post.

    So you could add the image in as:

    echo('<li><img src="'.$meta['myimg'].'" /><a href="' . get_permalink($dv->ID) . '">' . $dv->post_title . '</a></li>');

    If might be your image field is a number in which case you want this:

    echo('<li><img src="'.wp_get_attachment_url($meta['myimg']).'" /><a href="' . get_permalink($dv->ID) . '">' . $dv->post_title . '</a></li>');

    Thread Starter baglio

    (@baglio)

    Hello. First of all, thank you for taking your time to help me.

    Sounds like you just want to loop your post type instead try this code:…

    This was, indeed, the first approach I took, and it worked like a charm.
    The problem is that all of a sudden, my customer has decided that he wanted the “menu” entries to be sortable in the back-end.

    This is why I switched to the core wp_nav instead, so that he could arrange the entries whatever he wanted.

    Hmmm well you could always add an ‘order’ custom field in the post type but i guess its not as elegant.

    You could use a hybrid approach maybe. Let him have the menu then do the above in a more roundabout way:

    $menu = wp_get_nav_menu_items('YOUR MENU');

    print_r that and it gives you an array of post objects you can then just loop over them:

    echo('<ul>');
    foreach($menu as $mk => $mv){
    $meta = get_post_meta($mv->ID);
    echo('<li><a href="' . get_permalink($mv->ID) . '">' . $mv->post_title . '</a></li>');
    }
    echo('</ul>');
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display "custom field" to menu items’ is closed to new replies.