Php 面包屑增加元位置内容的数量

Php 面包屑增加元位置内容的数量,php,wordpress,Php,Wordpress,我是PHP新手,还在学习,所以请原谅并告诉我什么是合适的修复方法。我使用了Pieter Goosen回答的面包屑脚本。我希望面包屑与google和schema一起工作,不工作的部分是标记。需要为每个面包屑设置content=“number”,顺序为(1,2,3…)。在第8行,我添加了$I=1,然后更改为“”。但是,$i++并没有打印一个新的序列号,它只是在重复自己,就好像没有其他的$i++一样。我的猜测是全局应用规则并搜索使用的$i++,因为它可能是在规则中封闭应用的,但不知道如何做到这一点 代

我是PHP新手,还在学习,所以请原谅并告诉我什么是合适的修复方法。我使用了Pieter Goosen回答的面包屑脚本。我希望面包屑与google和schema一起工作,不工作的部分是
标记。需要为每个面包屑设置content=“number”,顺序为(1,2,3…)。在第8行,我添加了
$I=1,然后更改为“”。但是,
$i++
并没有打印一个新的序列号,它只是在重复自己,就好像没有其他的
$i++
一样。我的猜测是全局应用规则并搜索使用的$i++,因为它可能是在规则中封闭应用的,但不知道如何做到这一点

代码:

函数get_hansel_和_gretel_breadcrumbs()
{
//设置变量供以后使用
$here\u text=\uuuuuuu(‘您当前在这里!’);
$home_link=home_url('/');
$home_text=_uu('home');
$link_before='';
$i=1;
$link_after=''
$link_attr='rel=“v:url”property=“v:title”';
$link=$link\u在“.”之前。$link\u在“.”之后;
$delimiter='»;;//碎屑之间的分隔符
$before='';//在当前碎屑之前标记
$after='';//在当前碎屑后标记
$page_addon='';//如果查询被分页,则添加页码
$breadcrumb_trail='';
$category_links='';
/** 
*将我们自己的$wp_设置为_查询变量。由于
*可靠性
*/
$wp_the_query=$GLOBALS['wp_the_query'];
$queryed_object=$wp_the_query->get_queryed_object();
//处理单篇文章请求,包括单页、文章和附件
if(单数形式)
{
/** 
*设置我们自己的$post变量。由于
*可靠性。我们将$post_对象变量设置为$GLOBALS['wp_the_query']
*/
$post\u object=sanitize\u post($queryed\u object);
//设置变量
$title=apply_过滤器('the_title',$post_object->post_title);
$parent=$post\u object->post\u parent;
$post\u type=$post\u object->post\u type;
$post\u id=$post\u object->id;
$post_link=$before.$title.$after;
$parent_字符串=“”;
$post_type_link='';
如果('post'==$post\u类型)
{
//获取帖子类别
$categories=获取\u类别($post\u id);
如果($类别){
//让我们抓住第一类
$category=$categories[0];
$category\u links=get\u category\u parents($category,true,$delimiter);
$category_links=str_replace(“”;
$breadcrumb\u output\u link.=$page\u插件;
}
}否则{
$breadcrumb\u output\u link.=$here\u text.$delimiter;
$breadcrumb_output_link.='';
$breadcrumb\u output\u link.=$delimiter;
$breadcrumb\u output\u link.=$breadcrumb\u trail;
$breadcrumb\u output\u link.=$page\u插件;
}
$breadcrumb_output_link.='';
返回$breadcrumb\u输出\u链接;
}

您好。请在这个问题中输入代码,这样就可以更容易地理解您所说的哪一行以及整个代码,而不必在答案之间切换。谢谢,我只是觉得代码太长了,但您是对的,来回比较不方便。您好。请在这个问题中输入代码,这样更容易取消设置倒立你正在谈论的那一行以及整个代码,而不必在答案之间切换。谢谢,我只是觉得代码在这里太长了,但你是对的,来回比较不方便。
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">';
    $i                = 1;
    $link_after       = '<meta itemprop="position" content="' . $i++ . '" /></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->labels->singular_name . '<li class="curr-cat"   itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . $term_name . '</span><meta itemprop="position" content="' . $i++ . '" /></li>';
            $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;
}