4

I am trying for hours and I managed to get full slug for parent of sub-page when I am on 3rd level sub-page I can not get slug of the top parent page. Example domain.com/top_page/sub_page/sub_sub_page

I need to get full slug 'top_page/sub_page/sub_sub_page'

I am doing this for language sites that are on sub-domains (individuals sites). These pages have same slug across sub-domains. So I want to link language site that when you are on some given page link to language site will take you to that page in that language (not home page of that language).

example of hyperlink to English language sub-domain:

echo '<a href="en.domain.com/'.get_FULL_SLUG).'">'English'</a>';

Is it possible to get slug of top parent when you are on third level page, sub-sub-page

Hope somebody can help

2 Answers 2

4

This would replace domain.com/top_page/sub_page/sub_sub_page with /top_page/sub_page/sub_sub_page

$path = str_replace(home_url(),'',get_permalink());
8
  • If I understand...you are suggesting to take current permalink and get full slug from there exclude current sub-domain from permalink and after create dynamic link on language flags. Short example on how it would be of great help...Thanks again!
    – a.shaki
    Apr 2, 2012 at 11:37
  • If your home url for the english site is en.domain.com/ then removing the home_url, and then adding in say fr.domain.com to the start of the permalink, you'd have the new URL. Eitherway your question is not about 'full slugs' as that would be 'sub_sub_page' which isn't what you stated you want. This question is about permalinks.
    – Tom J Nowell
    Apr 2, 2012 at 12:02
  • I guess you are right...there are two questions: how to get full slug of the current page and how to replace domain so flags will link to related pages...Example: Contact page when you click on any language flag it would take you to contact page in that language, I will make sure that contact pages in all languages have same slug...Not sure which way would be the best to achieve it.
    – a.shaki
    Apr 3, 2012 at 7:45
  • I've already answered that though, for en.domain.com/contact $path == '/contact', so your french link is "fr.domain.com".$path, your german "de.domain.com".$path etc etc etc
    – Tom J Nowell
    Apr 3, 2012 at 9:59
  • 1
    Actually this has worked...thank you Tom. For other users this is what I have done: 1. insert in header.php following <?php $full_slug = str_replace(home_url(),'',get_permalink()); ?> 2. Linked language flags using following code: <?php Echo "<a href=pt.domain.com$full_slug>" ?><img src="<?php bloginfo('template_url'); ?>/assets/images/flags/pt.jpg" alt="" />Português</a>
    – a.shaki
    Apr 3, 2012 at 11:34
8

Directly using get_page_uri:

$path = get_page_uri($post_or_id);

Result is e.g. top-level-page/sub-page/current-page. This function is used internally by get_page_link etc.

1
  • To get the current page you can pass null as parameter. echo "Full slug: ", get_page_uri(null);
    – Fred
    Nov 21, 2014 at 15:15

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.