0

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.

5
  • Why not just put the Contact page's content into the editor, and load it like a normal WordPress page?
    – Pat J
    Commented Jan 7, 2014 at 14:01
  • What of there is a contact form code that needs to be part of a template for example? In addition, my contact info such as email, phone, fax, etc are currently part of a widget and I am planning to move them to a settings panel. If I am missing something or doing things out of the normal wordpress way please let me know. thx
    – blaise
    Commented Jan 7, 2014 at 14:07
  • I would suggest that you are doing things in a strange way, at least it seems that way. It is not clear exactly what you want and I think you have an XY problem. You are asking how to do Y even if it is a bad way to get X, which is what you actually want. Try to rewrite the question so that your goal is clearly explained.
    – s_ha_dum
    Commented Jan 7, 2014 at 15:20
  • I provided more details. I hope it is clear now, Thx
    – blaise
    Commented Jan 7, 2014 at 15:47
  • The wording of the title is going to throw many people off. Pages don't get their content from a template file; rather, the template file used is determined by the queried page object, and what is output based on the queried object is determined by the query-determined template. Perhaps a re-phrase will help make your question more clear. Commented Jan 7, 2014 at 15:49

1 Answer 1

0

You can get part of the way there by querying the page template using get_page_template_slug() inside your loop. For example, you can do something for non-default page templates:

if ( '' != get_page_template_slug( $page->ID ) ) {
    // This page uses a custom page template;
    // do something
}

Or, you could look for your contact-form page template specifically:

if ( 'template-contact-form.php' == get_page_template_slug( $page->ID ) ) {
    // This page uses the contact form custom page template;
    // do something
}

The problem you'll encounter is the do something part, because you've hard-coded your contact form into the template.

To do what you're wanting to do, you'll probably need to convert your contact-form code into a shortcode or Widget, so that you can parse/output it in your custom loop, such as:

if ( 'template-contact-form.php' == get_page_template_slug( $page->ID ) ) {
    // This page uses the contact form custom page template;
    // parse the shortcode
    echo do_shortcode( '[contact-form]' );
}

...or:

if ( 'template-contact-form.php' == get_page_template_slug( $page->ID ) ) {
    // This page uses the contact form custom page template;
    // output the widget
    the_widget( 'my-contact-form' );
}

(Note: shorthand code for do_shortcode() and the_widget(); refer to Codex for proper usage.)

1
  • Thx Chip for the explanation you provided in the comment above. I will look at your suggestion and try to make it work for what I need to achieve. However, I am interested in re-phrasing the title for 2 reasons: bring more people to help and make this thread helpful for others in the future. So what do you think it is best as a title?
    – blaise
    Commented Jan 7, 2014 at 22:19

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.