0

This is a rather simple question, so I'll keep it short.

Update1: In this context {page-structure} means just that: page1/page1-1/page1-1-1/{...}.

Update2: For further clarification, I want to do about the same the ACF Post Object field is doing, except I want to do it dynamically based on the URL structure as explained below.

I'm making a custom multilingual site, with a dual page structure (with and without /en). Any page at mysite.com/en/{page-structure} are getting its locale changed to en_US and the content (ACF) is fetched from the corresponding page at mysite.com/{page-structure}. Based on the locale different content is displayed.

How can I on mysite.com/en/{page-structure} automatically get the $post Object from mysite.com/{page-structure} (i.e. without en/)?

(Currently I'm using ACF to fetch fields from the corresponding page by rewriting the field requests, but it I could change the $post Object on the fly, it would make everything a little simpler.)

3
  • 1
    What's adding the /en? Is it a plugin? A parent page? A different WordPress install? Aug 23, 2017 at 11:17
  • without knowing the contents of {page-structure} it's going to be very difficult to know how to answer this question, especially with the /en/ thing, is that a multilingual plugin? A parent page? How will you know which page it is you want? slug? ID? Please edit your question with more context/details
    – Tom J Nowell
    Aug 23, 2017 at 11:22
  • Question is updated. Aug 23, 2017 at 11:29

3 Answers 3

2

This is how you can get the post object of a page with a matching path, minus the /en part:

$page_path = get_page_uri( get_queried_object_id() ); // en/page/child-page
$target_page_path = str_replace( 'en/', '', $page_path ); // page/child-page
$target_page = get_page_by_path( $target_page_path ); // WP_Post of target page.
1
  • 1
    I think @anton-lukin also had a correct answer, but this is simpler – marked as accepted answer. Aug 23, 2017 at 11:51
1

If I understand correctly the question, the right answer would be to use url_to_postid function.

Then you can setup post id this way:

global $post; 
$post = get_post($post_id, OBJECT);
setup_postdata($post);

// your code

wp_reset_postdata();
2
  • Ok, with this method I think I can retrieve the ID from the page I want. Now that I have the ID, how do redefine the $post Object to display the data from the page? Aug 23, 2017 at 11:40
  • Updated answer. Aug 23, 2017 at 11:44
0

Use the $global in before your $post

For example:

global $post;
echo $post->ID;
1
  • I think you may have misunderstood the question. I've updated it and tried to clarify. Aug 23, 2017 at 11:35

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.