• Resolved StrawJam

    (@strawjam)


    Hello,
    First, sorry for my bad english.
    I’m trying to link a menu, to an iframe in that page, but I can’t, because I don’t know where can I define the name of the target.

    I know this: “On /wp-admin/nav-menus.php , click on the link for Screen Options in the top right of your screen. Hidden away in the dropdown there, below the usual Show on screen checkboxes, is another line of Show advanced menu properties checkboxes. Just check the box for Link Target and then all your Menu Items will have the additional option of Link Target : “Same window or tab” or “New window or tab”
    But this only works fot target=”_blank”, it’s just a checkbox, and I need to define another one.
    What can I do?

    [Moderator Note: No bumping, thank you.]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Just a guess, but I think what you want is an ‘anchor link’. Here is an article that may help: https://help.typepad.com/anchor-tags.html

    Thread Starter StrawJam

    (@strawjam)

    No, I don’t want to do an anchor link (that is easy), I want to link to an iframe.

    I think you can use a Walker to do what you want. You would code the URL like this:

    https://yourdomain.com/optionalpage/target/iframename

    Add the walker to wp_nav_menu() similar to this:

    wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary', 'walker' => new iframe_walker() ) );

    And define the walker in functions.php:

    /* Convert "https://mydomain.com/pagename/target/iframename" to
     * href="https://mydomain.com/pagename/" target="iframename"
     *
     * The walker must be added to wp_nav_menu() in header.php
     * */
    class iframe_walker extends Walker_Nav_Menu
    {
          function start_el(&$output, $item, $depth, $args)
          {
                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 .'>';
    
                if ( preg_match( '/^(.+?)\/target\/(.+)$/', $item->url, $matches ) ) {
                   $item->url = $matches[1] . '/';
                   $item->target = $matches[2];
                }
                $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        ) .'"' : '';
                $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
    
                $item_output = $args->before;
                $item_output .= '<a'. $attributes .'>';
                $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
                //$item_output .= $description.$args->link_after;
                $item_output .= '</a>';
                $item_output .= $args->after;
    
                $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
                }
    }
    Thread Starter StrawJam

    (@strawjam)

    Thank you!
    It almost works! but I have a problem: My link is this way> https://mydomain.com/pagename.html, not this way: https://mydomain.com/pagename/
    Can I change something to make it works?

    Try changing this line:

    $item->url = $matches[1] . '/';

    to this:

    $item->url = $matches[1];

    Thread Starter StrawJam

    (@strawjam)

    Yeah!!!!!!!!! ??
    Now it works perfect!
    Thank you very much for your help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Menu with target option (to link an iframe)’ is closed to new replies.