Php 搜索结果中的WordPress面包屑

Php 搜索结果中的WordPress面包屑,php,wordpress,breadcrumbs,Php,Wordpress,Breadcrumbs,在WordPress中,我目前使用它来显示我的页面和帖子,这在访问特定页面时可以正常工作 下面是我用来显示WordPress模板中面包屑的函数: <?php if ( function_exists('yoast_breadcrumb') ) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } ?> 但是,我希望能够在搜索结果循环中显示相同的面包屑(用于各个页面和帖子) 到目前为止,搜索成员时显示

在WordPress中,我目前使用它来显示我的页面和帖子,这在访问特定页面时可以正常工作

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

<?php if ( function_exists('yoast_breadcrumb') ) {
    yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
但是,我希望能够在搜索结果循环中显示相同的面包屑(用于各个页面和帖子)

到目前为止,搜索
成员时显示的内容是:

您的搜索结果:

Team Members 
Home > Search > Members

Members Area 
Home > Search > Members
Team Members 
Home > About Us > Team Members

Members Area 
Home > Members Area
但是我不想在搜索结果页面中使用面包屑,我希望它们用于搜索关键字后显示的各个页面和帖子

例如,假设我再次搜索
成员
,我希望显示以下内容:

您的搜索结果:

Team Members 
Home > Search > Members

Members Area 
Home > Search > Members
Team Members 
Home > About Us > Team Members

Members Area 
Home > Members Area
我不担心这是否与SEO插件集成,但到目前为止,这是我发现的在WordPress中显示面包屑的最佳解决方案


另外,如果abody需要,这是我的
search.php
文件:

使用插件生成面包屑并不是真的必要。下面是一个简单的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>";
}
WPSEO_Breadcrumbs::$instance = NULL;
然后,您可以在您喜欢的任何页面上实现这些面包屑,包括您的searchpage.php文件或用于显示此调用的搜索结果的任何文件

<?php breadcrumbs(); ?>

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

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>";
}
WPSEO_Breadcrumbs::$instance = NULL;
我相信这是第22行,还要确保使用您问题中的Yoast breadcrumb函数,而不是现在的新breadcrumb()函数

请让我知道这是否有效

完整解释:

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

搜索页面有一个可使用的。你可以将其应用于加载面包屑。以下是一个例子:

if ( ! is_search() ) {
    if ( function_exists('yoast_breadcrumb') ) {
        yoast_breadcrumb('<p id="breadcrumbs">','</p>');
    }
}
如果(!is_search()){
如果(函数_存在('yoast_breadcrumb')){
yoast_面包屑(“

”,“

”); } }

这也取决于你在哪里加载面包屑,但除非你的主题非常独特,否则这通常是可行的

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

/*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*/
/*开始循环*/
/*端环*/

基于亚弗的回答,我找到了一个方法。几个小时来我一直在为这事绞尽脑汁。不过,您可以将备份和恢复放在循环的最前面。这是:

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;
global$wp\u查询;
//备份
$old\u singular\u value=$wp\u query->is\u singular;
//改变
$wp\u query->is\u singular=true;
//重置
WPSEO_Breadcrumbs::$instance=NULL;
//面包屑
如果(函数_存在('yoast_breadcrumb')){
yoast_面包屑(“

”,“

”); } //恢复 $wp\u query->is\u singular=$old\u singular\u value;

它伪造查询使其单数化,因此新刷新的面包屑认为这不是搜索页面,而是一篇文章或页面,或者任何显示为搜索结果的内容。

抱歉,刚刚发现了几个语法错误,但现在应该都正常了。已经有两周没有接触过php了,所以让我知道它是如何运行的,应该可以很好地工作。搜索结果总是比首页高一级,不是吗?当然!但是我不需要搜索结果页面的面包屑,我需要搜索关键字时找到的页面的面包屑。例如,请看我的问题,我希望页面的面包屑与标题一起出现,以及php代码的摘录.modified搜索部分。。不知道你到底在找什么。不过我还是去睡觉了。希望你能找到答案。为你的帮助干杯!恐怕不是这样。我将很快重新编辑我的问题,让其他人有更好的机会理解我,啊哈。@Giles你测试过了吗?看起来这是不可能的。即使在重置实例之后,插件也会使用is_singular()来确定当前页面是否为帖子,并且此函数无法重写。因此,尽管它在重置实例后加载了正确的帖子,但它在5年后根据当前页面的类型(帖子/存档/类别/等等)选择了错误类型的面包屑来显示,仍然很有魅力。谢谢你,亚弗!