Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom walker and I would like to either get_the_content() (up to the Read More) or get_the_excerpt() to use in this walker, but I can't figure out how to get it to work - it either returns nothing, or if I put an $item->object_id in it (thinking maybe it needed the post/page ID), then it just returns that ID and not the actual post/page text.

I tried googling

Here is my code:

class Salamander_AdventMore_Walker extends Walker_page {
function start_el(&$output, $item, $depth, $args) {
    if ( $depth ) {
        $indent = str_repeat("\t", $depth);
    } else {
        $indent = '';
    }

    $advent_slug = get_post_meta($item->object_id, 'advent-slug', true);
    $advent_small_title = get_post_meta($item->object_id, 'advent-title', true);
    $advent_title = ( !empty($advent_small_title) ? $advent_small_title : $advent_slug);

    $advent_content = apply_filters('the_content', $item->object_id);
    $advent_content = str_replace(']]>', ']]>', $advent_content);

    // this doesn't work
    // $advent_content = get_the_content( $item->object_id, 'Read more ...' ) ;

    $output .= $indent . '
    <li>
      <section>
        <h1>' . $advent_title . '</h1>
        <div id="day'. $advent_slug .'" class="daydetail-content">
          <div class="right">
            <hgroup>
              <h2>' . $advent_title . '</h2>
            </hgroup>
            '. $advent_content .'
          </div><!-- ends right -->
          <div class="left">
            <!--<a href="#" target="_blank"><img src="http://www.oia.co.za/wp-content/uploads/currency_by_abcdz2000_Flickr-356x703.jpg" width="356" alt=""/></a>-->
          </div><!-- ends left -->
        </div><!-- ends daydatail-content -->
      </section>
    ';
} // ends function
} // ends class
share|improve this question
    
Alternatively, I'd be happy with how to do a regular query, but with the selection of posts from a particular menu –  Amanda Feb 15 '12 at 6:23
add comment

1 Answer

up vote 2 down vote accepted

For anyone who might be looking for the same thing, this is what I did in the end:

I abandoned the Custom Walker idea and used wp_get_nav_menu_items to get a list of post IDs from my custom menu, from this tutorial on Digging into WordPress: http://digwp.com/2011/11/html-formatting-custom-menus/

In my functions file, I created this function:

function salamander_fetch_advent_posts() {
 global $post_list;
 $menu_name = 'advent-calendar'; // specify custom menu slug
 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);

    $post_list = array();
    foreach ((array) $menu_items as $key => $menu_item) {
        $post_id = $menu_item->object_id;
        $post_list[] = $post_id;
    }
  }
  return $post_list ;
}

And then in my theme template file, I passed $post_list to a new query, like so:

global $post_list;
if (function_exists( salamander_fetch_advent_posts() )) {
    salamander_fetch_advent_posts();
}

// The Query
$the_query = new WP_Query( array(
    'post__in' => $post_list,
    'post_type' => 'any',
    'posts_per_page' => -1
    ) ) ;

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.