WordPress的wp_list_pages
问题描述:
我用这个强调当前页面:WordPress的wp_list_pages
<ul>
<?php wp_list_pages("&post_type=projects&child_of=$parent_page&title_li="); ?>
</ul>
要获得:
<ul>
<li class="page_item page-item-588"><a href="#" title="One">One</a></li>
<li class="page_item page-item-592"><a href="#" title="Two">Two</a></li>
<li class="page_item page-item-599"><a href="#" title="Three">Three</a></li>
</ul>
首先代码应该显示子页面的列表。 一切都很好,但我面临一些问题。如果我使用自定义帖子类型(例如projects
),Wordpress 3.2.1不能将“当前”类添加到<LI>
,我无法突出显示随机打开的当前页面。
的functions.php
add_action('init', 'register_cpt_projects');
function register_cpt_projects() {
$labels = array(
'name' => _x('Проекты', 'projects'),
'singular_name' => _x('Проект', 'projects'),
'add_new' => _x('Добавить', 'projects'),
'add_new_item' => _x('Добавить проект', 'projects'),
'edit_item' => _x('Изменить', 'projects'),
'new_item' => _x('Новый Проект', 'projects'),
'view_item' => _x('Просмотреть', 'projects'),
'search_items' => _x('Поиск проектов', 'projects'),
'not_found' => _x('Ничего не найдено', 'projects'),
'not_found_in_trash' => _x('Ничего не найдень в корзине', 'projects'),
'parent_item_colon' => _x('Родительский Проект:', 'projects'),
'menu_name' => _x('Проекты', 'projects'),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'post-formats', 'page-attributes'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'page'
);
register_post_type('projects', $args);
};
变量 $ parent_page:
// get_top_parent_page_id
function get_top_parent_page_id($id) {
global $post;
if ($post->ancestors) {
return end($post->ancestors);
} else {
return $post->ID;
}
};
请帮助!
答
我认为最好的方法是考虑使用wp_nav_menu
而不是wp_list_pages
- 它是更灵活的解决方案,可以做你想做的。您必须将菜单与网页列表同步,但可以使用操作自动完成。
function get_id_outside_loop() {
global $wp_query;
$thePostID = $wp_query->post->ID;
return $thePostID;
}
在header.php中:
答
没有疼痛,只是身体类:)
在functions.php中解决这个问题
<body class="<?php echo get_id_outside_loop(); ?>">
在<head>
部分或页脚。 php:
<STYLE>
<!--
body.<?php echo get_id_outside_loop(); ?> .project_pages ul li.page-item-<?php echo get_id_outside_loop(); ?>
{
background: #0096f4; /* Highlight */
}
-->
</STYLE>
祝你有美好的一天!
答
在functions.php中添加此
add_filter('nav_menu_css_class', 'current_type_nav_class', 10, 2);
function current_type_nav_class($classes, $item) {
$post_type = get_post_type();
if ($item->attr_title != '' && $item->attr_title == $post_type) {
array_push($classes, 'current-menu-item');
};
return $classes;
}
答
您需要添加到您的functions.php:
function kct_page_css_class($css_class, $page, $depth, $args, $current_page) {
if (!isset($args['post_type']) || !is_singular($args['post_type']))
return $css_class;
global $post;
$current_page = $post->ID;
$_current_page = $post;
_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';
return $css_class;
}
add_filter('page_css_class', 'kct_page_css_class', 10, 5);
答
wp_list_pages( )会将当前页面与菜单项目进行比较,并将类别“ current_page_item“链接到当前页面的li链接。
所有你需要做的就是添加
li.current_page_item{
/*your specific mark up here */
}
在主题文件夹中的style.css文件。无需修改functions.php
来源:http://codex.wordpress.org/Template_Tags/wp_list_pages#Markup_and_styling_of_page_items