• Resolved hakelos

    (@hakelos)


    Hi,
    my site has 2 post formats, standard and links. Links format post are blank and they are basicly a placeholder for thumbnail and a custom field with link address. I managed to change url for those format of post to that custom field in submenuWalker.php

    if (get_post_format( $p->ID ) == "link" ) {
    					$p->url = apply_filters( 'jcs/item_url', get_post_meta($p->ID, 'link', true), $p->ID, $post_type );
    				} else {
    					$p->url = apply_filters( 'jcs/item_url', get_permalink( $p->ID), $p->ID, $post_type );
    				}
    				$p->url = apply_filters( 'jcs/post_item_url', $p->url, $p->ID );

    Is there is a way to add atribute target=”_blank” to those links?

    https://www.ads-software.com/plugins/jc-submenu/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author ImportWP

    (@jcollings)

    Hi,

    when working with wordpress i always try not to edit wordpress core or any plugin files so they can still be updated and not break any custom code i have written, instead you can use actions and filters to modify instead.

    for example to add the blank target to your links you could hook into either of these filters by adding this code to your themes function file.

    the filter nav_menu_link_attributes is built into the core wordpress nav menu walker and jcs/menu_item_args filter is part of JC Submenu.

    add_filter('nav_menu_link_attributes', 'jcs_nav_menu_link_attributes', 10, 4);
    function jcs_nav_menu_link_attributes($atts, $item, $args, $depth){
    
    	if (get_post_format( $item->ID ) == "link" ) {
    		$atts['target'] = '_blank';
    	}
    	return $atts;
    }
    
    add_filter( 'jcs/menu_item_args', 'jcs_menu_item_args', 10, 2 );
    function jcs_menu_item_args($args, $item){
    
    	if (get_post_format( $item->ID ) == "link" ) {
    		$item->target = '_blank';
    	}
    	return $args;
    }

    Hope this helps

    James

    Thread Starter hakelos

    (@hakelos)

    I’m still kinda noob to WordPress and I don’t know much about working with filters, anyway U directed me what to add and its working.
    THX ??

    Plugin Author ImportWP

    (@jcollings)

    glad i could help.

    Have fun working with wordpress

    James

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘target="_blank" in link’ is closed to new replies.