Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query only the pages used in the menu. WordPress

  • SOLVED

Hello!
I am working on a one page WordPress theme. What I want to do is query the pages only used in the header wp_nav_menu so I can output their contents on the page.

So with that list of page ID's, I can do something like this:



<?php query_posts('post_type=page&page_id='.$pages_from_nav.''); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php the_title(); ?>
<?php the_content(); ?>

<?php endwhile; ?>
<?php endif; ?>



Thanks!

Answers (2)

2011-10-16

Julio Potier answers:

Hello You can do this :

In your functions.php in your theme, add this Class :

class pages_from_nav extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args)
{
$item_output .= $item->object_id;
$item_output .= ',';

$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}


Then in your code you can grad the ids like this :
<?php $ids = wp_nav_menu( array( 'theme_location' => 'primary', 'walker' => new pages_from_nav(), 'echo' => false ) ); ?>

THese Ids are from this menu code :
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>

See you soon !


Mike McAlister comments:

Hi Julio,
Thanks for your answer. How do I work that into a query, because it doesn't seem to work the way I'm doing it.


<?php $ids = wp_nav_menu( array( 'theme_location' => 'main', 'walker' => new pages_from_nav(), 'echo' => false ) ); ?>

<?php query_posts('post_type=page&page_id='.$ids.''); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php the_title(); ?>
<?php the_content(); ?>

<?php endwhile; ?>
<?php endif; ?>


Julio Potier comments:

Ok, add this code under the first $ids= ...
Code :
$ids = explode( ',', $ids );
for( $i = 0; $i<count($ids)-1; $i++){
$ids[$i] = preg_replace( '[^0-9]', '', strip_tags($ids[$i]) );
}


Full code :
<?php
$ids = wp_nav_menu( array( 'theme_location' => 'primary', 'walker' => new pages_from_nav(), 'echo' => false ) );
$ids = explode( ',', $ids );
for( $i = 0; $i<count($ids)-1; $i++){
$ids[$i] = preg_replace( '[^0-9]', '', strip_tags($ids[$i]) );
}
query_posts('post_type=page&page_id='.$ids.''); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php the_title(); ?>

<?php the_content(); ?>

<?php endwhile; ?>

<?php endif; ?>


Julio Potier comments:

edit :

Replace
$ids[$i] = preg_replace( '[^0-9]', '', strip_tags($ids[$i]) );
by
$ids[$i] = absint(preg_replace('[^0-9]', '', strip_tags(trim($ids[$i]))))


Julio Potier comments:

@Romel Apuya : Because <strong>wp_get_nav_menu_items</strong> requires the slug menu, and Mike is using <strong>wp_nav_menu</strong>


Mike McAlister comments:

Thanks again, Julio. The code brings in pages, but it doesn't seem to be bringing in just the pages on the menu though, keeps bringing in all pages.

The last edit you posted gave me a 500. I think it just needed a ; at the end of the line, but the code didn't work when adding that anyway.

Mike


Julio Potier comments:

ok so replace this :
query_posts('post_type=page&page_id='.$ids.'');
by this :
query_posts('post_type=page&post__in='.$ids.'');


Mike McAlister comments:

Nope, that actually makes the entire query stop working. I feel like you're close but I don't know what to suggest to make your code work.


Julio Potier comments:

Can i have an admin access or ftp acces to edit/test my code in real time ?
This will be faster, i don't have your theme, your pages, your settings ...
Thank you inadvance !

2011-10-16

Romel Apuya answers:

why not use <strong>wp_get_nav_menu_items</strong>

[[LINK href="http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items"]]http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items[[/LINK]]