0

I extended my Walker_Nav_Menu that converting hyperlink to anchor as I'm making one page website. Here is the code:

class description_walker extends Walker_Nav_Menu{
  function start_el(&$output, $item, $depth= 0 , $args = array(), $id = 0){
       global $wp_query;
       $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
       $class_names = $value = '';
       $classes = empty( $item->classes ) ? array() : (array) $item->classes;
       $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
       $class_names = ' class="'. esc_attr( $class_names ) . '"';
       $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
       $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
       $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
       $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
       if($item->object == 'page')

       {
            $varpost = get_post($item->object_id);
            if(is_home()){
              $attributes .= ' href="#' . $varpost->post_name . '"';
            }else{
              $attributes .= ' href="'.home_url().'/#' . $varpost->post_name . '"';
            }
       }
       else
            $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID );
        $item_output .= $args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
 }
}

Now I have a meta box in Page that ask is this page a section(options Yes or No) if Yes than convert that page to anchor other wise keep it hyperlink . How would I achieve it please help.. Thanks in advance..

2
  • Have you considered modifying the permalinks via filters instead?
    – Tom J Nowell
    Commented Apr 11, 2015 at 15:53
  • Hi Tom, Thanks for your answer .. Nope actually I don't have any idea about modifying the permalinks via filter. I'm new to Wordpress it just took me 4-5 day to extend the walker_nav_menu. Can you explain it little more if it is what I'm looking for.. or any other way to get what I want. thanks in advance.
    – Hussy571
    Commented Apr 11, 2015 at 16:10

1 Answer 1

0

First, throw away your custom walker.

Post URLs are called permalinks, and as with most things in WordPress, you can filter them! There is a filter called post_link that lets you modify the URL of the post before it's returned in get_permalink(). We can use this, along with the posts slug, and the home_url function to get something similar to this:

function wpse183928_anchor_urls( $url, $post, $leavename ) {
    $url = home_url()."#".$post->post_name;
    return $url;
}
add_filter( 'post_link', 'wpse183928_anchor_urls', 10, 3 );

Secondly, your metabox and post meta you're using for sections is the wrong way to do it. If you want to categorise things, you use a custom taxonomy, e.g. section category, you don't repurpose pages and post meta to implement a custom taxonomy.

So register a custom taxonomy, call it sections, and assign it to pages. Now you can add 'sections' to your nav menu, WordPress will give you a user interface for free, your data will support import/export, and you'll have an admin page dedicated to creating/editing/deleting sections made for you

Some helpful pieces of information:

  • A lot of people use a page template to implement a homepage. There's no need, just use the home.php template, or index.php
  • When I say posts, I'm talking about a post of type post. Pages are posts too ( of type page ). Menu nav items are posts of type nav_menu_item, and images are referenced by post ID, where each image has an associated post of type attachment with post meta connected to it that stores EXIF data, descriptions, etc
  • WP_Query. People will suggest querying using query_posts. These people are not your friends, treat everything they say with suspicion, and never use query_posts
  • If you need to modify which posts WordPress retrieves in the main loop, don't throw away the main query and create your own, use the pre_get_posts filter instead, it's significantly faster
5
  • Hi Tom, Thanks for your answer . I use the code u wrote but its not taking to that section instead taking me to the individual page :( .. May be I'm doing something wrong. Is there any way to do with my code walker_nav with meta box???? thanks
    – Hussy571
    Commented Apr 11, 2015 at 20:55
  • My code is unrelated to your custom nav walker, I hadn't spotted the meta requirement though which suggest you're going about this all wrong
    – Tom J Nowell
    Commented Apr 12, 2015 at 0:21
  • I've made modifications to my answer, for information on custom taxonomies you should look elsewhere on this site. Other people have asked about it and good answers were given, better to look at those, but you want the register_taxonomy function
    – Tom J Nowell
    Commented Apr 12, 2015 at 0:25
  • I threw away my walker and paste the filter add_filter('post_link', wpse183....); but it still taking me to another page not changing the permalink for eg: I want. localhost/wordpress/#services but its generating the permalink like localhost/wordpress/?page_id=458 I got what you are saying. Explained very well. I made the custom taxonomy and its showing the interface... Any help is going to be appreciated. :) thanks for your answer.
    – Hussy571
    Commented Apr 12, 2015 at 7:39
  • Oooh, you don't have pretty permalinks turned on? This explains a lot, anything involving rewrites or changing permalinks will need permalinks turned on
    – Tom J Nowell
    Commented Apr 13, 2015 at 18:42

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.