我想整合get_post_meta
进入wp_list_pages
混合,以便只有在帖子的“自定义字段”部分中存在关键字时,才将任何顶级父列表项替换为图像。我不能用特色图片b/c,它已经被用于帖子本身的另一个目的。
这是我在函数中的Walker\\u Page类。php:
/**
* Extend the default page walker class to append class names for pages that
* are parents.
* @uses Walker_Page
*
* http://wordpress.stackexchange.com/questions/11821/class-parent-for-wp-list-pages
*
*/
class My_Page_Walker extends Walker_Page
{
/**
* Filter in the classes for parents.
*/
function _filterClass( $class )
{
$class[] = \'parent\'; // change this to whatever classe(s) you require
return $class;
}
/**
* This is effectively a wrapper for the default method, dynamically adding
* and removing the class filter when the current item has children.
*/
function start_el( &$output, $page, $depth, $args, $current_page )
{
if ( !empty($args[\'has_children\']) )
add_filter( \'page_css_class\', array( &$this, \'_filterClass\') );
parent::start_el( $output, $page, $depth, $args, $current_page );
if ( !empty($args[\'has_children\']) )
remove_filter( \'page_css_class\', array( &$this, \'_filterClass\') );
}
}
因此,我目前正在使用
wp_list_pages
要显示自定义帖子类型的帖子,请执行以下操作:
<ul class="items"><?php
$args = array(
\'walker\'=>new My_Page_Walker,
\'post_type\' => \'region\',
\'menu_class\' => \'nav-menus\',
\'title_li\' => \'\'
);
wp_list_pages($args);
?>
</ul>
它会输出以下代码:
<ul class="items">
<li class="page_item page-item-123 parent"><a href="/wordpress/region/city/" title="city">City</a>
<ul class=\'children\'>
<li class="page_item page-item-124 parent"><a href="/wordpress/region/city/suburb/" title="Suburb">Suburb</a>
<ul class=\'children\'>
<li class="page_item page-item-125"><a href="/wordpress/region/city/suburb/complex/" title="Complex">Complex</a></li>
</ul>
</li>
</ul>
</li>
</ul>
我想要以下内容:
<ul class="items">
<li class="page_item page-item-123 parent"><a href="/wordpress/region/city/" title="city"><img src="city-name.jpg" alt="City" /></a>
<ul class=\'children\'>
<li class="page_item page-item-124 parent"><a href="/wordpress/region/city/suburb/" title="Suburb">Suburb</a>
<ul class=\'children\'>
<li class="page_item page-item-125"><a href="/wordpress/region/city/suburb/complex/" title="Complex">Complex</a></li>
</ul>
</li>
</ul>
</li>
</ul>
在哪里
city-name.jpg
在“自定义字段”部分中定义。它应该只显示具有自定义字段的帖子标题。
这是一个非常复杂的代码量,我不能使用wp_list_pages
? 老实说,我在网上找不到答案。
最合适的回答,由SO网友:micah 整理而成
我去了#wordpress irc频道,得到了Marko Heijnen. 他从/wp includes/post模板中获取了Walker代码。php并在其中添加了几行。
/**
* Extend the default page walker class to append class names for pages that
* are parents.
* @uses Walker_Page
*
* http://wordpress.stackexchange.com/questions/11821/class-parent-for-wp-list-pages
*
*/
class My_Page_Walker extends Walker_Page
{
/**
* Filter in the classes for parents.
*/
function _filterClass( $class )
{
$class[] = \'parent\'; // change this to whatever classe(s) you require
return $class;
}
/**
* This is effectively a wrapper for the default method, dynamically adding
* and removing the class filter when the current item has children.
*/
function start_el( &$output, $page, $depth, $args, $current_page )
{
if ( !empty($args[\'has_children\']) )
add_filter( \'page_css_class\', array( &$this, \'_filterClass\') );
if ( $depth )
$indent = str_repeat("\\t", $depth);
else
$indent = \'\';
extract($args, EXTR_SKIP);
$css_class = array(\'page_item\', \'page-item-\'.$page->ID);
if ( !empty($current_page) ) {
$_current_page = get_page( $current_page );
_get_post_ancestors($_current_page);
if ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) )
$css_class[] = \'current_page_ancestor\';
if ( $page->ID == $current_page )
$css_class[] = \'current_page_item\';
elseif ( $_current_page && $page->ID == $_current_page->post_parent )
$css_class[] = \'current_page_parent\';
} elseif ( $page->ID == get_option(\'page_for_posts\') ) {
$css_class[] = \'current_page_parent\';
}
$css_class = implode(\' \', apply_filters(\'page_css_class\', $css_class, $page));
$output .= $indent . \'<li class="\' . $css_class . \'"><a href="\' . get_permalink($page->ID) . \'">\' . $link_before;
$image = get_post_meta($page->ID, \'title_image\', true);
if ( isset($image) && !empty($image) ) {
$output .= \'<img src="\'. $image .\'" alt="\'. apply_filters( \'the_title\', $page->post_title, $page->ID ) .\'" />\';
}
else {
$output .= apply_filters( \'the_title\', $page->post_title, $page->ID );
}
$output .= $link_after . \'</a>\';
if ( !empty($show_date) ) {
if ( \'modified\' == $show_date )
$time = $page->post_modified;
else
$time = $page->post_date;
$output .= " " . mysql2date($date_format, $time);
}
if ( !empty($args[\'has_children\']) )
remove_filter( \'page_css_class\', array( &$this, \'_filterClass\') );
}
}