You have two options :
Option 1: Tweak your existing code
Consider caching the menu item ID at the begin of your
foreach()loop and rather of using functions like
the_content()use the equivalent functions that accept a page/post ID as an argument. Something like :
$post_id = $item->object_id;should work, then you can use
get_the_title($post_id);. If each individual page is meant to be visible on its own then this is your best count. If not, keep reading…
Option 2: Create a more intuitive one page structure
It ’ south been my experience that this structure isn ’ metric ton always intuitive for clients to manage. They ’ ra bounced back and forth between multiple pages and even some rather understanding clients have trouble efficaciously managing the page structure using WP ’ s menus. It besides adds overhead because you need to setup at least one
template_redirect to ensure visitors can not view each page individually.
My solution to this has been to use Advanced Custom Fields to plainly create multiple sections within one WordPress page. If each incision contains more than a few customs fields then it looks much uninfected with ACF ’ s built in tab key fields :
It eliminates any potentially surly rewrites and gives clients the ability to manage a single page worth of content precisely where they expect it to be : on the Home/Front/Index Page, rather of bouncing between multiple pages and seafaring menus.
The downside to option 2 is that it lacks some tractability, but you could easily compensate for that by creating a repeating firearm field with the check value or a custom field within each tab called “ Sort Order ” which would function much like WordPress ’ s native Sort order when you run your loop to output the tab key ’ content .
This leaves one defect : the customer won ’ triiodothyronine be able to add a modern section on their own. In my opinion, this is plainly the cost of doing business and commissioning a custom-made site—that is, if they want to add extra sections they will need to hire you again ( or another dev ). So it ’ s not 100 % harass unblock for them, but it ’ s not the goal of the worldly concern for you since it could mean future work down the road .
There are besides some SEO considerations with your method because it could be seen as a electric potential sitemap hack. I.e., by creating multiple pages you ’ re tricking search engines into thinking your site is larger than it actually is. sophisticated crawlers like Google will notice that when they crawl the individual pages and are redirected .