I am building a menu and representing it to the front-end in Accordion style using the foundation front-end framework. The Wordpress admin drags and drops menu items (pages, posts, custom posts, categories, whatever) into the menu from Appearance -> Menus as normal. My code loops through every menu item in the menu and displays it along with its content. For example, if the loop encounters an about page in the menu, it displays About as a list item label (clickable) and its content (hidden) beneath it. Site visitors clicks on About to reveal the about page content in accordion fashion. What is the best approach to get this done? I mean the loop and content retrieval. I made an attempt as of the code below.
About about page content (text/ starts as hidden) Contact contact page content (text/ starts as hidden) Solutions HEALTHCARE (label) Healthcare solution 1 (link) Healthcare solution 2 (link) PROFESSIONAL SERVICES (label) Prof service 1 (link) prof service 2 (link)
I wrote the following code and I partially succeeded:
<?php
$menu_name = 'primary';
if( ($locations = get_nav_menu_locations()) && isset($locations[$menu_name]) ) {
$menu = wp_get_nav_menu_object( $locations[$menu_name] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
?>
<dl class="accordion" data-accordion>
<?php foreach( (array) $menu_items as $key => $menu_item ) { ?>
<?php if ($menu_item->object == 'page'): ?>
<?php
$id = get_post_meta($menu_item->ID, '_menu_item_object_id', true);
$page = get_page($id);
//$template_name = get_post_meta($id, '_wp_page_template', true);?>
<dd>
<a href="#panel<?php echo $menu_item->ID;?>"><?php echo $menu_item->title; ?></a>
<div id="panel<?php echo $menu_item->ID;?>" class="content">
<?php
if($page->post_title == 'Contact'): /* DIRTY:PROBLEM IS HERE */
get_template_part('contact'); /* NOT GOOD, very had coded */
else: echo apply_filters('the_content', $page->post_content); endif; /* I LIKE THIS MORE */?>
</div>
</dd>
<?php elseif($menu_item->object == 'solutions_category'): /* CUSTOM POST TYPE */?>
<?php /* REST OF THE CODE ... */ ?>
<?php endif;?>
<?php } ?>
</dl> <!-- .accordion -->
It works fine that way, but the problem lies in the 'Contact' page because it only uses a template page for its content (the page text editor is empty). As you can see I am explicitly checking if post_title is Content in order to use get_template_part. This is definitely bad and not desired.
Is there a generic solution capable of retrieving page content despite if content is from template, settings page or the text editor itself?
Thanks in advance.