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 an additional menu that appears on some single.php pages. To this menu I would like to add an li with a link to a PDF which is uploaded to the post by a custom field (advanced custom fields helps with this).

I found a snippet on how to add todays date to my menu, and thought I could maybe use this as a starting point. However I´m not good enough coder to know where to go from here. I would appreciate some help with it!

This is the snippet I´m starting with: http://pastebin.com/xHHxuHdQ

I need to make it display this instead:

<a href="<?php the_field('mcf_uppsagning-pdf'); ?>">Uppsägning</a>

I cant just replace it though and I dont know the syntax enough to know how to modify it. Please help :)

Thanks

share|improve this question
1  
You have 8 questions, 0 of which are resolved, you should really consider trying to get away from that 0% accept rate, either by submitting your own answers or by accepting the (correct) answers others provide you with. On the issue of your question: What is the_field()? More importantly, where is that .pdf stored? –  m0r7if3r Mar 5 '12 at 16:11
    
I had a similar requirement so I just ended up writing a custom walker - works a treat, and is fairly easy to do. –  David Gard Mar 5 '12 at 16:22
    
@JohanDahl Please go through your solved Questions and mark answers as solved. –  kaiser Mar 5 '12 at 23:08
    
the_field() is a function provided by the custom fields plugin mentioned and I thought the below approach was easiest to use as writing a custom walker isn't really that easy for beginners! –  sanchothefat Mar 5 '12 at 23:51

1 Answer 1

up vote 1 down vote accepted

Try this:

// Filter wp_nav_menu() to add additional links and other output
add_filter('wp_nav_menu_items','add_pdf_to_menu', 10, 2);
function add_pdf_to_menu( $items, $args ) {
    if( $args->theme_location == 'extra-menu')  {

        $pdf = get_post_meta( get_the_ID(), 'mcf_uppsagning-pdf', true );
        if ( $pdf )
            $items .=  '<li><a href="' . wp_get_attachment_url( $pdf ) .  '">Uppsägning</a></li>';

    }
    return $items;
}

Essentially because you're in the context of the single page get_the_ID() will return the id for the post/page you're on. If that doesn't work try using get_queried_object_id() in place of get_the_ID().

EDIT: changed the above code to return the attachment permalink.

share|improve this answer
    
Hmm, sadly that doesn´t display anything for me. –  Johan Dahl Mar 5 '12 at 16:16
    
That comment was not finished, sry for accidently posting it. It almost works now! This did add a link with the correct title, but the link is only /145. That is the ID of the attachment. I need to get the URL, but it should only be a slight change should it not? Also, if I might add another question. Is there a way to decide where in the menu this new li will be inserted? Thanks you. –  Johan Dahl Mar 5 '12 at 16:27
1  
@JohanDahl I updated the code example - you can pass any attachment ID into wp_get_attachment_url() and it'll give you the URL –  sanchothefat Mar 5 '12 at 16:59
1  
@JohanDahl to change the order of the links you'd need to do some clever stuff with the $items string. Is it a simple case like putting it at the top or do you need in the middle somewhere? –  sanchothefat Mar 5 '12 at 17:03
1  
@Johan Dahl: You have a 0% answer acceptance rate; you should go back to your old questions and accept correct answers or submit your own answers. That's the way this site works. –  songdogtech Mar 5 '12 at 20:37

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.