0

I have been searching and reading for 3 hours and still couldn't find anything that come close to what I needed and I am still trying understand how wp add_filter, rewrite, etc works. Just started wordpress coding.

How do I use function.php to dynamically alter all the page links that are generated in the menu?

the pages in menu shows this :

"About" -> "www.mysite.com/about-us"

"Services" -> "ww.mysite.com/our-services"

My problem now is how change all the links in the website to :

"About" -> "ww.mysite.com/somethingaddedfromfunctionphp-about-us"

"Services" -> "ww.mysite.com/somethingaddedfromfunctionphp-our-services"

I have tried many codes from the search like :

add_filter( 'post_type_link', 'filter_the_post_type_link', 1, 4 );
function filter_the_post_type_link( $post_link, $post, $leavename, $sample ) {

function reset_permalinks() {
add_action( 'init', 'reset_permalinks' );

function edit_the_permalink($html){
add_filter('the_permalink', 'edit_the_permalink');

But all of them doesn't change the url in the menu.

Thanks


Ok I tried this wordpress.stackexchange.com/questions/33551/… modified it to :

add_filter('post_type_link', 'change_menu_url', 1, 3); 
function menu( $link, $post = 0 ){ return 'japan-' . $post->post_name ); } I 

I would expect the url generated in the menu to change, but when it is still pointing to 'our-product' and not 'japan-our-product'

  • Why do you want to do such thing? Do you want to change those URLs only in the menu or every where? What are those links, links to pages, posts, anything else? The sample code you have posted seems incomplete, can you post the completed code you have tried? – cybmeta Oct 2 '15 at 7:12
  • We have 2 duplicated sites for 2 countries and all contents are the same except, for SEO purpose, the most of the links have the country name "/japan-our-product-name", "/korea-our-product-name – Joe Oct 2 '15 at 12:50
  • When you create the pages/posts/menu on each site, jut set the slug you want to use. I'm still don't understand the purpose of modifying the links on the fly. – cybmeta Oct 2 '15 at 17:04
  • But the reason we are combining two sites into one is to create and maintain only one page to serve both country – Joe Oct 2 '15 at 17:27
  • Nobody understands what I am trying to do? – Joe Oct 9 '15 at 15:14
0

You aren't showing enough of the code to determine where you are going wrong.

Are you looking for specific post types or pages? How are you adding or deleting menu items?

Example code to add a menu item

add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 ); function your_custom_menu_item ( $items, $args ) { if (is_single() && $args->theme_location == 'primary') { $items .= '<li>Show whatever</li>'; } return $items; }

As you can see, you can interrogate the post type and args if needed, but the items are primarily html line items so to delete them you need to search for them and replace them in the $items variable.

| improve this answer | |
  • Hi I am not showing enough of the code is because I was just trying out whatever examples to try to change the url of the menu. I add menu from the admin appearance - menu. The slug of the page is 'our-products'. On the website when you hover the mouse on the menu it will show 'our-products' in the browser status at the bottom, I want it to show 'japan-our-products' even though the actual page slug is 'pir – Joe Oct 2 '15 at 17:30
  • Hi I am not showing enough of the code is because I was just trying out whatever examples to try to change the url of the menu. I add menu from the admin appearance - menu. The slug of the page is 'our-products'. On the website when you hover the mouse on the menu it will show 'our-products' in the browser status at the bottom, I want it to show 'japan-our-products' even though the actual page slug is 'our-products'. The menu URL shud be altered for display purposes because there isn't any real page with the slug 'japan-our-products' – Joe Oct 2 '15 at 17:47
  • Yes, but without the code no one can help you. What you will learn as you use stackoverflow is that the more you put into the question, the more you get back... – brianlmerritt Oct 2 '15 at 18:02
  • Ok I tried this wordpress.stackexchange.com/questions/33551/… modified it to : add_filter('post_type_link', 'change_menu_url', 1, 3); function menu( $link, $post = 0 ){ return 'japan-' . $post->post_name ); } I would expect the url generated in the menu to change, but when it is still pointing to 'our-product' and not 'japan-our-product' – Joe Oct 3 '15 at 17:59
  • trying to put quote in a box..sorry code add_filter('post_type_link', 'change_menu_url', 1, 3); function menu( $link, $post = 0 ){ return 'japan-' . $post->post_name ); } I code – Joe Oct 4 '15 at 3:58
0

You are probably looking for wp_nav_menu_objects hook.

See this answer: Add items to a menu dynamically

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.