2012-12-21 45 views
2

我想从特定页面的菜单中删除链接。如何从wordpress中的主菜单中删除链接

function wp_list_pages($args = '') { 
$defaults = array(
    'depth' => 0, 'show_date' => '', 
    'date_format' => get_option('date_format'), 
    'child_of' => 0, 'exclude' => '', 
    'title_li' => __('Pages'), 'echo' => 1, 
    'authors' => '', 'sort_column' => 'menu_order, post_title', 
    'link_before' => '', 'link_after' => '', 'walker' => '', 
); 

$r = wp_parse_args($args, $defaults); 
extract($r, EXTR_SKIP); 

$output = ''; 
$current_page = 0; 

// sanitize, mostly to keep spaces out 
$r['exclude'] = preg_replace('/[^0-9,]/', '', $r['exclude']); 

// Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array) 
$exclude_array = ($r['exclude']) ? explode(',', $r['exclude']) : array(); 
$r['exclude'] = implode(',', apply_filters('wp_list_pages_excludes', $exclude_array)); 

// Query pages. 
$r['hierarchical'] = 0; 
$pages = get_pages($r); 

if (!empty($pages)) { 
    if ($r['title_li']) 
     $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>'; 

    global $wp_query; 
    if (is_page() || is_attachment() || $wp_query->is_posts_page) 
     $current_page = $wp_query->get_queried_object_id(); 
    $output .= walk_page_tree($pages, $r['depth'], $current_page, $r); 

    if ($r['title_li']) 
     $output .= '</ul></li>'; 
} 

$output = apply_filters('wp_list_pages', $output, $r); 

if ($r['echo']) 
    echo $output; 
else 
    return $output; 

}

请告诉我,我能做些什么。我已经搜索了所有关心此问题的所有问题,但仍无法解决问题。提前致谢 。

+0

'wp_list_pages()'有一个'exclude'选项,不是? –

回答

2

只需将其放置在主题的functions.php文件中即可。

function removeParentLinks() { 
$pages = wp_list_pages('echo=0&amp;title_li='); 
$pages = explode("</li>", $pages); 
$count = 0; 
foreach($pages as $page) { 
    if(strstr($page,"<ul>")) { 
     $page = explode('<ul>', $page); 
     $page[0] = str_replace('</a>','',$page[0]); 
     $page[0] = preg_replace('/\<a(.*)\>/','',$page[0]); 
     if(count($page) == 3) { 
      $page[1] = str_replace('</a>','',$page[1]); 
      $page[1] = preg_replace('/\<a(.*)\>/','',$page[1]); 
     } 
     $page = implode('<ul>', $page); 
    } 
    $pages[$count] = $page; 
    $count++; 
} 
$pages = implode('</li>',$pages); 
echo $pages; 

}

现在只需更换您的wp_list_pages();函数removeParentLinks();并离开你去。

+0

感谢您的回复。是工作。 –

相关问题