2011-10-02 74 views
0

我用這個強調當前頁面: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; 
     } 
    }; 

請幫助!

回答

2

我認爲最好的方法是考慮使用wp_nav_menu而不是wp_list_pages - 它是更靈活的解決方案,可以做你想做的。您必須將菜單與網頁列表同步,但可以使用操作自動完成。

function get_id_outside_loop() { 
    global $wp_query; 
    $thePostID = $wp_query->post->ID; 
    return $thePostID; 
} 

在header.php中:

0

沒有疼痛,只是身體類:)

在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> 

祝你有美好的一天!

1

在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; 
} 

來源:https://wordpress.stackexchange.com/a/12292/11610

0

您需要添加到您的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); 

通過http://kucrut.org/wp_list_pages-for-custom-post-types/