Wordpress wp_列表_页面突出显示当前页面
我用这个:Wordpress wp_列表_页面突出显示当前页面,wordpress,highlight,custom-post-type,Wordpress,Highlight,Custom Post Type,我用这个: <ul> <?php wp_list_pages("&post_type=projects&child_of=$parent_page&title_li="); ?> </ul> 变数 $parent_页面: // get_top_parent_page_id function get_top_parent_page_id($id) { global $post; if
<ul>
<?php wp_list_pages("&post_type=projects&child_of=$parent_page&title_li="); ?>
</ul>
变数
$parent_页面:
// 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;
}
};
请帮忙 > P>我认为最好的方法是考虑使用<代码> WpnNavaMutux而不是<代码> WpListListPox——它是更灵活的解决方案,可以做你想做的事情。你必须同步菜单与你的页面列表,但是它可以使用动作自动完成。 < P>我认为最好的方法是考虑使用<代码> WpNavaMult菜单而不是<代码> WpListListPox< /Cord>——它是更灵活的解决方案,可以做你想做的事。您必须将菜单与页面列表同步,但可以使用操作自动完成此操作。轻松解决此问题,只需body类:) 在functions.php中:
function get_id_outside_loop() {
global $wp_query;
$thePostID = $wp_query->post->ID;
return $thePostID;
}
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 );
在header.php中:
<body class="<?php echo get_id_outside_loop(); ?>">
轻松解决它,只需身体类:)
在functions.php中:
function get_id_outside_loop() {
global $wp_query;
$thePostID = $wp_query->post->ID;
return $thePostID;
}
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 );
在header.php中:
<body class="<?php echo get_id_outside_loop(); ?>">
在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中添加此
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;
}
来源:您需要将其添加到函数中。php:
function get_id_outside_loop() {
global $wp_query;
$thePostID = $wp_query->post->ID;
return $thePostID;
}
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 );
通过您需要将其添加到函数中。php:
function get_id_outside_loop() {
global $wp_query;
$thePostID = $wp_query->post->ID;
return $thePostID;
}
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
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 );
};
来源:wp\u list\u pages()将当前页面与菜单项进行比较,并将类“current\u page\u item”添加到包含当前页面链接的li中
您只需添加
li.current_page_item{
/*your specific mark up here */
}
到主题文件夹中的style.css文件。无需修改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 );
};
资料来源: