The page content is inserted into the page DOM’s “nav-wrapper” element, so the content is now also part of the page DOM, so you could do something like var myElement = document.getElementById("remoteContent"); This must be done as part of the success: callback since the element does not exist in the DOM until it’s inserted into nav-wrapper.
Alternately, use DOMParser().parseFromString() to convert the content string into a separate DOM object, of which you can then use its own .getElementById() method to get the element you want, without needing to insert the entire content into the page’s DOM.
Thread Starter
cwolff
(@cwolff)
Thank you bcworks…
How do I reference a div with an id=remote-content from theContent variable above?
var theContent = data;
Something like: theContent.content.#remote-content.rendered;
Thread Starter
cwolff
(@cwolff)
I think my issue is that the html outside of the page content (as in elements that are in the theme) are not returned in the “pages” method: https://mysite/wp-json/wp/v2/pages/
How would I get the contents of an element that is in a WP template code?
-
This reply was modified 6 years, 6 months ago by
cwolff.
You cannot. API data is “un-themed”, the calling app is expected to apply its own theme. However, if the theme content you are after is stored in the DB somewhere, you can probably still get at it through the API if you can determine where the data is stored. For example, if the theme template includes
?>
<div class="credit">Another awesome theme by Foobar</div>
<?php
you cannot get such content from the API. But if the template includes
<?php
echo get_post_meta( get_the_ID(), 'foo_credit', true );
?>
then you can use the API to get the saved value through the /posts API route as long as the meta field is registered to be served with post data.
Thread Starter
cwolff
(@cwolff)
Thank you again bcworkz,
Is it possible to register a route/endpoint to make my WP menuing/navigation consumable via wp_nav_menu()?
Sure! You define the endpoint callback, so anything possible in PHP whose result can be JSON encoded can be made into a custom API route.
https://developer.ww.wp.xz.cn/rest-api/extending-the-rest-api/adding-custom-endpoints/