首页 文章

如何使用没有插件的wordpress导航菜单创建面包屑?

提问于
浏览
2

试图显示面包屑,但我被卡住了 .

ABOUT US 
 - TEAM
 - WHAT WE DO

我不明白如何在面包屑中动态显示所有菜单,如果我目前在我们的页面上,那么如何在面包屑中显示请帮助我

我试试这个

<?php $menuID = $post->ID;
query_posts( 'post_type=page&post_parent=$menuID' );
while ( have_posts() ) : the_post();
the_title();
endwhile;
?>

此代码只显示每个页面的子代,但如何找到单击哪一个页面

1 回答

  • 10

    将此代码放在 custom_functions.php:

    function the_breadcrumb() {
        global $post;
        echo '<ul id="breadcrumbs">';
        if (!is_home()) {
            echo '<li><a href="';
            echo get_option('home');
            echo '">';
            echo 'Home';
            echo '</a></li><li class="separator"> / </li>';
            if (is_category() || is_single()) {
                echo '<li>';
                the_category(' </li><li class="separator"> / </li><li> ');
                if (is_single()) {
                    echo '</li><li class="separator"> / </li><li>';
                    the_title();
                    echo '</li>';
                }
            } elseif (is_page()) {
                if($post->post_parent){
                    $anc = get_post_ancestors( $post->ID );
                    $title = get_the_title();
                    foreach ( $anc as $ancestor ) {
                        $output = '<li><a href="'.get_permalink($ancestor).'" title="'.get_the_title($ancestor).'">'.get_the_title($ancestor).'</a></li> <li class="separator">/</li>';
                    }
                    echo $output;
                    echo '<strong title="'.$title.'"> '.$title.'</strong>';
                } else {
                    echo '<li><strong> '.get_the_title().'</strong></li>';
                }
            }
        }
        elseif (is_tag()) {single_tag_title();}
        elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';}
        elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';}
        elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';}
        elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';}
        elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';}
        elseif (is_search()) {echo"<li>Search Results"; echo'</li>';}
        echo '</ul>';
    }
    

    转到 single.php 页面,在要显示面包屑的任何位置添加以下代码 .

    <?php the_breadcrumb(); ?>
    

相关问题