我有一个定制助行器,我想get_the_content()
(直至阅读更多)或get_the_excerpt()
在这个助行器中使用,但我不知道如何让它工作-它要么什么也不返回,要么如果我把$item->object_id
在其中(认为它可能需要帖子/页面ID),然后它只返回该ID,而不是实际的帖子/页面文本。
我试着用谷歌搜索
这是我的代码:
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
最合适的回答,由SO网友:gillespieza 整理而成
对于任何可能正在寻找相同东西的人,我最终的做法是:
我放弃了Custom Walker的想法wp_get_nav_menu_items
要从我的自定义菜单中获取帖子ID列表,请从本教程的Digging into WordPress: http://digwp.com/2011/11/html-formatting-custom-menus/
在我的函数文件中,我创建了以下函数:
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 ;
}
然后在我的主题模板文件中,我通过了
$post_list
到新查询,如下所示:
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();