首页 文章

搜索结果中的WordPress面包屑

提问于
浏览
5

在WordPress中我正在使用Yoast's SEO Plugin为我的页面和帖子显示breadcrumbs,这在访问特定页面时工作正常 .

这是我用来显示我的WordPress模板中的面包屑的函数:

<?php if ( function_exists('yoast_breadcrumb') ) {
    yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>

例如,当浏览 Team Members 这是 About Us 的孩子时,我得到:

Home > About Us > Team Members

但是,我希望能够在搜索结果循环中显示相同的面包屑(针对各个页面和帖子) .

到目前为止,搜索 Members 时显示的是:

您的搜索结果:团队成员
主页>搜索>会员

会员区
主页>搜索>会员

但我不想在搜索结果页面上使用面包屑,我希望它们用于搜索关键字时显示的各个页面和帖子 .

例如Imagine我再次搜索 Members 我想显示如下:

您的搜索结果:团队成员
主页>关于我们>团队成员

会员区
首页>会员专区

如果这个或者没有与SEO插件集成,我不会被打扰,但到目前为止,它是我发现在WordPress中显示面包屑的最佳解决方案!

另外,abody需要它,这是我的 search.php 文件:http://pastebin.com/0qjb2954

5 回答

  • 1

    试试这个 . 这是我自己的工作片段,在搜索循环中显示面包屑 .

    /*Begin Loop */
    <?php 
    echo '<div class="b-search_result_list__item_breadcrumbs breadcrumbs">';
    
    $current_type = get_post_type();
    if ($current_type == 'page') {
    
        $parents = get_post_ancestors(get_the_ID());
        if($parents){
    
            for($i=count($parents)-1;$i>=0;$i--){
                echo '<span typeof="v:Breadcrumb">';
                echo '<a rel="v:url" property="v:title" title="'.get_the_title($parents[$i]).'" href="'.get_permalink($parents[$i]).'">'.get_the_title($parents[$i]).'</a>';
                echo '</span>';
            }
        }else{
            echo '<span typeof="v:Breadcrumb">';
            echo '<a rel="v:url" property="v:title" title="'.get_bloginfo('name').'" href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a>';
            echo '</span>';
        }
        echo '<span typeof="v:Breadcrumb">';
        echo '<span property="v:title">'.get_the_title().'</span>';
        echo '</span>';
    }else{
        $current_obj = get_post_type_object($current_type);
    
            echo '<span typeof="v:Breadcrumb">';
            echo '<a rel="v:url" property="v:title" title="'.get_bloginfo('name').'" href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a>';
            echo '</span>';
            echo '<span typeof="v:Breadcrumb">';
            echo '<a rel="v:url" property="v:title" title="'.$current_obj->labels->name.'" href="'.get_post_type_archive_link( $current_type ).'">'.$current_obj->labels->name.'</a>';
            echo '</span>';
    
            $current_taxonomies = get_object_taxonomies($current_type);
    
            if($current_taxonomies){
                $current_terms = get_the_terms(get_the_ID(), $current_taxonomies[0]);
    
                if($current_terms){
                    $current_term = array_shift($current_terms);
    
                    echo '<span typeof="v:Breadcrumb">';
                        echo '<a rel="v:url" property="v:title" title="'.$current_term->name.'" href="'.get_term_link($current_term).'">'.$current_term->name.'</a>';
                    echo '</span>';
    
                    /*
                    var_dump($current_obj->labels->name); // Archive name
                    var_dump(get_post_type_archive_link( $current_type )); // Archive link
                    var_dump($current_term->name);  // Term name
                    var_dump(get_term_link($current_term)); // Term link
                    var_dump(get_permalink()); // Post link 
                    */
                }
            }
            echo '<span typeof="v:Breadcrumb">';
            echo '<span property="v:title">'.get_the_title().'</span>';
            echo '</span>';
    
    }    
    
    echo '</div>';
    ?>
    /*End Loop*/
    
  • 0

    使用插件生成面包屑并不是必需的 . 这是一个简单的PHP函数,可以添加到functions.php文件中:

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

    以及一些CSS来设计它,根据您的需要进行定制

    #breadcrumbs {
        list-style:none;
        margin:5px 0;
        overflow:hidden;
    }
    
    #breadcrumbs li{
        float:left;
    }
    #breadcrumbs li+li:before {
        content: '| ';
        padding:0 4px;
    }
    

    然后,您可以在任何您喜欢的页面上实现这些面包屑,包括您的searchpage.php文件或您使用此调用显示搜索结果的文件

    <?php breadcrumbs(); ?>
    
  • 1

    尝试在search.php文件中的yoast breadcrumb函数上方添加以下代码行:

    WPSEO_Breadcrumbs::$instance = NULL;
    

    这将是第22行我相信,并且还要确保使用你问题中的Yoast面包屑函数,而不是现在的新的breadcrumb()函数 .

    如果有效,请告诉我!

    Full explanation:

    Yoast插件面包屑功能基于页面加载,基于当前页面作为子项 . 要使其加载正确的子项和父项,您需要在运行该函数之前重置它 . 没有内置的重置功能,但是将静态$ instance设置为NULL应该会导致插件根据您循环时设置的当前全局post对象重新生成其数据 .

  • 1

    在Yavor的回答基础上,我找到了一条路 . 几个小时以来我一直在喋喋不休 . 您可以放置备份并恢复循环 . 这里是:

    global $wp_query;
    //backup
    $old_singular_value = $wp_query->is_singular;
    //change
    $wp_query->is_singular = true;
    //reset
    WPSEO_Breadcrumbs::$instance = NULL;
    //breadcrumbs
    if (function_exists('yoast_breadcrumb')){
        yoast_breadcrumb('<p id="breadcrumbs">','</p>');
    }
    //restore
    $wp_query->is_singular = $old_singular_value;
    

    它伪造查询以使其成为单数,因此新刷新的面包屑认为这不是搜索页面,而是单个帖子或页面或您显示为搜索结果的任何内容 .

  • 3

    搜索页面可以使用conditional function . 您始终可以将其应用于加载面包屑 . 这是一个例子:

    if ( ! is_search() ) {
        if ( function_exists('yoast_breadcrumb') ) {
            yoast_breadcrumb('<p id="breadcrumbs">','</p>');
        }
    }
    

    这取决于您加载面包屑的位置,但除非您的主题非常独特,否则这通常会起作用 .

相关问题