简体   繁体   中英

How to modify a child page link with php on Wordpress

Hi stackoverflow community,

Well the think is that I have my custom menu already set for all the pages (no problem with that) the options are being getting by:

$links = wp_kses(wp_nav_menu($parameters), $allowed_tags); (I just allow anchor tags and its attributes)

and then:

<nav class="menu-navigation">
 <?php
 echo $links;
 ?>
</nav>

Everything cool so far, however some options on the menu are sections of some pages, like:

Home#contact

Evidently on other pages like the blog page if a don't have the contact section but I click that option it should redirection the user to Home#contact otherwise do not reload the page and just go to the #contact section.

So I want to detect with php which options are child and modify its href depending on the current page. I don't know php enough though so please any ideas and clear walkthrough to get this done?

Thanks in advance.

PD: I would like to avoid use JS if it is possible


Update:

Following the approach of several nav menus

This is my current header-nav code: https://gist.github.com/duranmla/e70cc6073dfb2d79e19f and I make the navigations as suggested. Finally here my dashboard https://www.evernote.com/shard/s243/sh/9896863b-c10b-4e32-bad9-5bbc4341cfd9/d373ab95d0c3c3f5d72f455c03dea7d4/deep/0/Fullscreen-8-15-14,-11-35-AM.png

First off, wrapping wp_nav_menu() with wp_kses() seems unnecessary.

To answer your question, one easy way without much coding would be to create two Menus - one for display on the pages that have a contact section, one for use everywhere else.

In your theme package, you'll need to be sure there are two menu locations registered.

register_nav_menus(
  array(
    'home_navigation' => __('Home Navigation'),
    'primary_navigation' => __('Primary Navigation')
  )
);

See http://codex.wordpress.org/Function_Reference/register_nav_menus

Then create your menu items in the WordPress admin, under Appearance > Menus. In the first menu, make your link " #contact ", and in the second, " /#contact ".

Then in your theme's header.php template, you'll need to indicate which nav menu to use. Here's what it would look like if your home page is the only page which requires the alternate menu:

if(is_front_page()){

  wp_nav_menu(array('theme_location' => 'home_navigation'));

} else {

  wp_nav_menu(array('theme_location' => 'primary_navigation'));

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM