0

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

3
  • 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
    Commented Mar 5, 2012 at 16:22
  • @JohanDahl Please go through your solved Questions and mark answers as solved.
    – kaiser
    Commented Mar 5, 2012 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! Commented Mar 5, 2012 at 23:51

1 Answer 1

1

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.

7
  • Hmm, sadly that doesn´t display anything for me.
    – Johan Dahl
    Commented Mar 5, 2012 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
    Commented Mar 5, 2012 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 Commented Mar 5, 2012 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? Commented Mar 5, 2012 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. Commented Mar 5, 2012 at 20:37

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.