带有不带插件的自定义贴子类型的面包屑

时间:2015-10-06 作者:Adrian

到目前为止,我在每个网站上都使用了以下面包屑功能,但今天的网站有3种自定义帖子类型,客户希望面包屑能够正常工作,例如,我们只得到了“home//naire blanco”,而不是“home/wines/naire blanco”。

function the_breadcrumb() {
        echo \'<ol class="breadcrumb" itemprop="breadcrumb">\';
    if (!is_home()) {
        echo \'<li><a href="\';
        echo get_option(\'home\');
        echo \'">\';
        echo \'Home\';
        echo \'</a></li>\';
        if (is_category() || is_single() || is_post_type()) {
            echo \'<li>\';
            the_category(\' </li><li> \');
            echo get_post_type(\' </li><li> \');
            if (is_single()) {
                echo \'</li><li>\';
                the_title();
                echo \'</li>\';
            }
        } elseif (is_page()) {
            if($post->post_parent){
                $anc = get_post_ancestors( $post->ID );

                foreach ( $anc as $ancestor ) {
                    $output = $output . \'<li><a href="\'.get_permalink($ancestor).\'" title="\'.get_the_title($ancestor).\'">\'.get_the_title($ancestor).\'</a></li> \';
                }
                echo $output;
                echo \'<strong title="\'.$title.\'"> \'.$title.\'</strong>\';
            } else {
                echo \'<li><a class="active" href="\';
                echo the_permalink();
                echo \'">\';
        echo the_title();
        echo \'</a></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 \'</ol>\';
}
正如您在代码中看到的,我尝试添加|| is_post_type() 之后is_category() || is_single() 使用echo get_post_type() 但这没有任何作用。

请给我一些指导。

谢谢

3 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

大多数面包屑函数的问题是它依赖于$post 对象。不仅是$post 全球完全不可靠(参见我的帖子here),它甚至可能不包含我们需要的数据,即使$post 全局未受损

这个$post 全局存档页面(包括类别、标记、日期、分类术语、作者和自定义帖子类型存档)包含循环前的第一篇帖子。第一篇帖子并非总是来自所选的归档文件(您所在的归档文件页面),这意味着您要查找的信息可能不正确,即使$post global未受到损害。第一篇文章可能不来自所选归档的情况是通过自定义粘滞和the_posts 滤器

我最近重写了一个非常旧的breadcrumb函数,它使用$GLOBALS[\'wp_the_query\'] 对象和保存在$GLOBALS[\'wp_the_query\'] 全球的这样,我们就有了99.999%可靠的面包屑,它不依赖于$post 全局或任何泄露的数据。

以下是函数:(需要PHP 5.4+。此外,可以根据需要进行更改)

function get_hansel_and_gretel_breadcrumbs()
{
    // Set variables for later use
    $here_text        = __( \'You are currently here!\' );
    $home_link        = home_url(\'/\');
    $home_text        = __( \'Home\' );
    $link_before      = \'<span typeof="v:Breadcrumb">\';
    $link_after       = \'</span>\';
    $link_attr        = \' rel="v:url" property="v:title"\';
    $link             = $link_before . \'<a\' . $link_attr . \' href="%1$s">%2$s</a>\' . $link_after;
    $delimiter        = \' &raquo; \';              // Delimiter between crumbs
    $before           = \'<span class="current">\'; // Tag before the current crumb
    $after            = \'</span>\';                // Tag after the current crumb
    $page_addon       = \'\';                       // Adds the page number if the query is paged
    $breadcrumb_trail = \'\';
    $category_links   = \'\';

    /** 
     * Set our own $wp_the_query variable. Do not use the global variable version due to 
     * reliability
     */
    $wp_the_query   = $GLOBALS[\'wp_the_query\'];
    $queried_object = $wp_the_query->get_queried_object();

    // Handle single post requests which includes single pages, posts and attatchments
    if ( is_singular() ) 
    {
        /** 
         * Set our own $post variable. Do not use the global variable version due to 
         * reliability. We will set $post_object variable to $GLOBALS[\'wp_the_query\']
         */
        $post_object = sanitize_post( $queried_object );

        // Set variables 
        $title          = apply_filters( \'the_title\', $post_object->post_title );
        $parent         = $post_object->post_parent;
        $post_type      = $post_object->post_type;
        $post_id        = $post_object->ID;
        $post_link      = $before . $title . $after;
        $parent_string  = \'\';
        $post_type_link = \'\';

        if ( \'post\' === $post_type ) 
        {
            // Get the post categories
            $categories = get_the_category( $post_id );
            if ( $categories ) {
                // Lets grab the first category
                $category  = $categories[0];

                $category_links = get_category_parents( $category, true, $delimiter );
                $category_links = str_replace( \'<a\',   $link_before . \'<a\' . $link_attr, $category_links );
                $category_links = str_replace( \'</a>\', \'</a>\' . $link_after,             $category_links );
            }
        }

        if ( !in_array( $post_type, [\'post\', \'page\', \'attachment\'] ) )
        {
            $post_type_object = get_post_type_object( $post_type );
            $archive_link     = esc_url( get_post_type_archive_link( $post_type ) );

            $post_type_link   = sprintf( $link, $archive_link, $post_type_object->labels->singular_name );
        }

        // Get post parents if $parent !== 0
        if ( 0 !== $parent ) 
        {
            $parent_links = [];
            while ( $parent ) {
                $post_parent = get_post( $parent );

                $parent_links[] = sprintf( $link, esc_url( get_permalink( $post_parent->ID ) ), get_the_title( $post_parent->ID ) );

                $parent = $post_parent->post_parent;
            }

            $parent_links = array_reverse( $parent_links );

            $parent_string = implode( $delimiter, $parent_links );
        }

        // Lets build the breadcrumb trail
        if ( $parent_string ) {
            $breadcrumb_trail = $parent_string . $delimiter . $post_link;
        } else {
            $breadcrumb_trail = $post_link;
        }

        if ( $post_type_link )
            $breadcrumb_trail = $post_type_link . $delimiter . $breadcrumb_trail;

        if ( $category_links )
            $breadcrumb_trail = $category_links . $breadcrumb_trail;
    }

    // Handle archives which includes category-, tag-, taxonomy-, date-, custom post type archives and author archives
    if( is_archive() )
    {
        if (    is_category()
             || is_tag()
             || is_tax()
        ) {
            // Set the variables for this section
            $term_object        = get_term( $queried_object );
            $taxonomy           = $term_object->taxonomy;
            $term_id            = $term_object->term_id;
            $term_name          = $term_object->name;
            $term_parent        = $term_object->parent;
            $taxonomy_object    = get_taxonomy( $taxonomy );
            $current_term_link  = $before . $taxonomy_object->labels->singular_name . \': \' . $term_name . $after;
            $parent_term_string = \'\';

            if ( 0 !== $term_parent )
            {
                // Get all the current term ancestors
                $parent_term_links = [];
                while ( $term_parent ) {
                    $term = get_term( $term_parent, $taxonomy );

                    $parent_term_links[] = sprintf( $link, esc_url( get_term_link( $term ) ), $term->name );

                    $term_parent = $term->parent;
                }

                $parent_term_links  = array_reverse( $parent_term_links );
                $parent_term_string = implode( $delimiter, $parent_term_links );
            }

            if ( $parent_term_string ) {
                $breadcrumb_trail = $parent_term_string . $delimiter . $current_term_link;
            } else {
                $breadcrumb_trail = $current_term_link;
            }

        } elseif ( is_author() ) {

            $breadcrumb_trail = __( \'Author archive for \') .  $before . $queried_object->data->display_name . $after;

        } elseif ( is_date() ) {
            // Set default variables
            $year     = $wp_the_query->query_vars[\'year\'];
            $monthnum = $wp_the_query->query_vars[\'monthnum\'];
            $day      = $wp_the_query->query_vars[\'day\'];

            // Get the month name if $monthnum has a value
            if ( $monthnum ) {
                $date_time  = DateTime::createFromFormat( \'!m\', $monthnum );
                $month_name = $date_time->format( \'F\' );
            }

            if ( is_year() ) {

                $breadcrumb_trail = $before . $year . $after;

            } elseif( is_month() ) {

                $year_link        = sprintf( $link, esc_url( get_year_link( $year ) ), $year );

                $breadcrumb_trail = $year_link . $delimiter . $before . $month_name . $after;

            } elseif( is_day() ) {

                $year_link        = sprintf( $link, esc_url( get_year_link( $year ) ),             $year       );
                $month_link       = sprintf( $link, esc_url( get_month_link( $year, $monthnum ) ), $month_name );

                $breadcrumb_trail = $year_link . $delimiter . $month_link . $delimiter . $before . $day . $after;
            }

        } elseif ( is_post_type_archive() ) {

            $post_type        = $wp_the_query->query_vars[\'post_type\'];
            $post_type_object = get_post_type_object( $post_type );

            $breadcrumb_trail = $before . $post_type_object->labels->singular_name . $after;

        }
    }   

    // Handle the search page
    if ( is_search() ) {
        $breadcrumb_trail = __( \'Search query for: \' ) . $before . get_search_query() . $after;
    }

    // Handle 404\'s
    if ( is_404() ) {
        $breadcrumb_trail = $before . __( \'Error 404\' ) . $after;
    }

    // Handle paged pages
    if ( is_paged() ) {
        $current_page = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : get_query_var( \'page\' );
        $page_addon   = $before . sprintf( __( \' ( Page %s )\' ), number_format_i18n( $current_page ) ) . $after;
    }

    $breadcrumb_output_link  = \'\';
    $breadcrumb_output_link .= \'<div class="breadcrumb">\';
    if (    is_home()
         || is_front_page()
    ) {
        // Do not show breadcrumbs on page one of home and frontpage
        if ( is_paged() ) {
            $breadcrumb_output_link .= $here_text . $delimiter;
            $breadcrumb_output_link .= \'<a href="\' . $home_link . \'">\' . $home_text . \'</a>\';
            $breadcrumb_output_link .= $page_addon;
        }
    } else {
        $breadcrumb_output_link .= $here_text . $delimiter;
        $breadcrumb_output_link .= \'<a href="\' . $home_link . \'" rel="v:url" property="v:title">\' . $home_text . \'</a>\';
        $breadcrumb_output_link .= $delimiter;
        $breadcrumb_output_link .= $breadcrumb_trail;
        $breadcrumb_output_link .= $page_addon;
    }
    $breadcrumb_output_link .= \'</div><!-- .breadcrumbs -->\';

    return $breadcrumb_output_link;
}
您可以简单地如下所示

echo get_hansel_and_gretel_breadcrumbs();
在需要的地方

这个breadcrumb函数还负责自定义帖子类型

SO网友:perfectionist1

Excellent!执行公认答案代码(Pieter Goosen)。

Note: 只是增加了一点任务。制作validation check 该功能的before calling 它是这样的:

<?php 
   if(function_exists(\'get_hansel_and_gretel_breadcrumbs\')): 
      echo get_hansel_and_gretel_breadcrumbs();
   endif;
?>
另外,我将该函数保存到一个名为breadcrumbs.php 在项目根文件夹中,并将其包括在内in functions.php 文件使用include_once(\'breadcrumbs.php\'); 更具组织性。

SO网友:user153920
function get_hansel_and_gretel_breadcrumbs()
{
    // Set variables for later use
    $here_text        = __( \'You are currently here!\' );
    $home_link        = home_url(\'/\');
    $home_text        = __( \'Home\' );
    $link_before      = \'<span typeof="v:Breadcrumb">\';
    $link_after       = \'</span>\';
    $link_attr        = \' rel="v:url" property="v:title"\';
    $link             = $link_before . \'<a\' . $link_attr . \' href="%1$s">%2$s</a>\' . $link_after;
    $delimiter        = \' &raquo; \';              // Delimiter between crumbs
    $before           = \'<span class="current">\'; // Tag before the current crumb
    $after            = \'</span>\';                // Tag after the current crumb
    $page_addon       = \'\';                       // Adds the page number if the query is paged
    $breadcrumb_trail = \'\';
    $category_links   = \'\';

    /** 
     * Set our own $wp_the_query variable. Do not use the global variable version due to 
     * reliability
     */
    $wp_the_query   = $GLOBALS[\'wp_the_query\'];
    $queried_object = $wp_the_query->get_queried_object();

    // Handle single post requests which includes single pages, posts and attatchments
    if ( is_singular() ) 
    {
        /** 
         * Set our own $post variable. Do not use the global variable version due to 
         * reliability. We will set $post_object variable to $GLOBALS[\'wp_the_query\']
         */
        $post_object = sanitize_post( $queried_object );

        // Set variables 
        $title          = apply_filters( \'the_title\', $post_object->post_title );
        $parent         = $post_object->post_parent;
        $post_type      = $post_object->post_type;
        $post_id        = $post_object->ID;
        $post_link      = $before . $title . $after;
        $parent_string  = \'\';
        $post_type_link = \'\';

        if ( \'post\' === $post_type ) 
        {
            // Get the post categories
            $categories = get_the_category( $post_id );
            if ( $categories ) {
                // Lets grab the first category
                $category  = $categories[0];

                $category_links = get_category_parents( $category, true, $delimiter );
                $category_links = str_replace( \'<a\',   $link_before . \'<a\' . $link_attr, $category_links );
                $category_links = str_replace( \'</a>\', \'</a>\' . $link_after,             $category_links );
            }
        }

        if ( !in_array( $post_type, [\'post\', \'page\', \'attachment\'] ) )
        {
            $post_type_object = get_post_type_object( $post_type );
            $archive_link     = esc_url( get_post_type_archive_link( $post_type ) );

            $post_type_link   = sprintf( $link, $archive_link, $post_type_object->labels->singular_name );
        }

        // Get post parents if $parent !== 0
        if ( 0 !== $parent ) 
        {
            $parent_links = [];
            while ( $parent ) {
                $post_parent = get_post( $parent );

                $parent_links[] = sprintf( $link, esc_url( get_permalink( $post_parent->ID ) ), get_the_title( $post_parent->ID ) );

                $parent = $post_parent->post_parent;
            }

            $parent_links = array_reverse( $parent_links );

            $parent_string = implode( $delimiter, $parent_links );
        }

        // Lets build the breadcrumb trail
        if ( $parent_string ) {
            $breadcrumb_trail = $parent_string . $delimiter . $post_link;
        } else {
            $breadcrumb_trail = $post_link;
        }

        if ( $post_type_link )
            $breadcrumb_trail = $post_type_link . $delimiter . $breadcrumb_trail;

        if ( $category_links )
            $breadcrumb_trail = $category_links . $breadcrumb_trail;
    }

    // Handle archives which includes category-, tag-, taxonomy-, date-, custom post type archives and author archives
    if( is_archive() )
    {
        if (    is_category()
             || is_tag()
             || is_tax()
        ) {
            // Set the variables for this section
            $term_object        = get_term( $queried_object );
            $taxonomy           = $term_object->taxonomy;
            $term_id            = $term_object->term_id;
            $term_name          = $term_object->name;
            $term_parent        = $term_object->parent;
            $taxonomy_object    = get_taxonomy( $taxonomy );
            $current_term_link  = $before . $taxonomy_object->labels->singular_name . \': \' . $term_name . $after;
            $parent_term_string = \'\';

            if ( 0 !== $term_parent )
            {
                // Get all the current term ancestors
                $parent_term_links = [];
                while ( $term_parent ) {
                    $term = get_term( $term_parent, $taxonomy );

                    $parent_term_links[] = sprintf( $link, esc_url( get_term_link( $term ) ), $term->name );

                    $term_parent = $term->parent;
                }

                $parent_term_links  = array_reverse( $parent_term_links );
                $parent_term_string = implode( $delimiter, $parent_term_links );
            }

            if ( $parent_term_string ) {
                $breadcrumb_trail = $parent_term_string . $delimiter . $current_term_link;
            } else {
                $breadcrumb_trail = $current_term_link;
            }

        } elseif ( is_author() ) {

            $breadcrumb_trail = __( \'Author archive for \') .  $before . $queried_object->data->display_name . $after;

        } elseif ( is_date() ) {
            // Set default variables
            $year     = $wp_the_query->query_vars[\'year\'];
            $monthnum = $wp_the_query->query_vars[\'monthnum\'];
            $day      = $wp_the_query->query_vars[\'day\'];

            // Get the month name if $monthnum has a value
            if ( $monthnum ) {
                $date_time  = DateTime::createFromFormat( \'!m\', $monthnum );
                $month_name = $date_time->format( \'F\' );
            }

            if ( is_year() ) {

                $breadcrumb_trail = $before . $year . $after;

            } elseif( is_month() ) {

                $year_link        = sprintf( $link, esc_url( get_year_link( $year ) ), $year );

                $breadcrumb_trail = $year_link . $delimiter . $before . $month_name . $after;

            } elseif( is_day() ) {

                $year_link        = sprintf( $link, esc_url( get_year_link( $year ) ),             $year       );
                $month_link       = sprintf( $link, esc_url( get_month_link( $year, $monthnum ) ), $month_name );

                $breadcrumb_trail = $year_link . $delimiter . $month_link . $delimiter . $before . $day . $after;
            }

        } elseif ( is_post_type_archive() ) {

            $post_type        = $wp_the_query->query_vars[\'post_type\'];
            $post_type_object = get_post_type_object( $post_type );

            $breadcrumb_trail = $before . $post_type_object->labels->singular_name . $after;

        }
    }   

    // Handle the search page
    if ( is_search() ) {
        $breadcrumb_trail = __( \'Search query for: \' ) . $before . get_search_query() . $after;
    }

    // Handle 404\'s
    if ( is_404() ) {
        $breadcrumb_trail = $before . __( \'Error 404\' ) . $after;
    }

    // Handle paged pages
    if ( is_paged() ) {
        $current_page = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : get_query_var( \'page\' );
        $page_addon   = $before . sprintf( __( \' ( Page %s )\' ), number_format_i18n( $current_page ) ) . $after;
    }

    $breadcrumb_output_link  = \'\';
    $breadcrumb_output_link .= \'<div class="breadcrumb">\';
    if (    is_home()
         || is_front_page()
    ) {
        // Do not show breadcrumbs on page one of home and frontpage
        if ( is_paged() ) {
            $breadcrumb_output_link .= $here_text . $delimiter;
            $breadcrumb_output_link .= \'<a href="\' . $home_link . \'">\' . $home_text . \'</a>\';
            $breadcrumb_output_link .= $page_addon;
        }
    } else {
        $breadcrumb_output_link .= $here_text . $delimiter;
        $breadcrumb_output_link .= \'<a href="\' . $home_link . \'" rel="v:url" property="v:title">\' . $home_text . \'</a>\';
        $breadcrumb_output_link .= $delimiter;
        $breadcrumb_output_link .= $breadcrumb_trail;
        $breadcrumb_output_link .= $page_addon;
    }
    $breadcrumb_output_link .= \'</div><!-- .breadcrumbs -->\';

    return $breadcrumb_output_link;
}

相关推荐

Breadcrumbs - get the author?

我有自己的函数breadcrumbs()。在其中,我调用is\\u author()来确定我是否在作者页面上。如果是真的,我想知道我在哪个作者的页面上。我尝试了\\u author(),但没有结果。我还查阅了WP codex。有人能帮忙吗?