Forum

Thread tagged as: Question

Starting `perch_item_index` at 2

Hi all

I was just wondering if there's a way of starting perch_item_index at 2 instead of the default 1?

I'm trying to create a breadcrumb trail using Schema markup with 'Home' as position 1, so need the first item in the 'loop' to have a position of 2.

<perch:before>
    <div class="container">
        <ol class="breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">
            <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><span itemprop="name">Home</span><meta itemprop="position" content="1" /></li>
</perch:before>
            <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><span itemprop="name"><perch:pages id="pageNavText" /></span><meta itemprop="position" content="<perch:pages id="perch_item_index" />" /></li>
<perch:after>
        </ol>
    </div>
</perch:after>

Many thanks

Glen Piggott

Glen Piggott 0 points

  • 4 years ago
Simon Clay

Simon Clay 127 points

Hi Glen,

The best way I can think of is to skip the template and loop through each item, adding +1 to a $num variable so on your page the code would be:

<!--this was perch:before-->
            <div class="container">
            <ol class="breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">
            <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><span itemprop="name">Home</span><meta itemprop="position" content="1" /></li>
            <!--this was perch:before-->

                <?php $result = perch_pages_navigation([
                'skip-template'=>true,
                ]);
                $num = 1;   
                foreach ($result as $pageInfo) {
                    $num = ++$num;
                    echo('<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><span itemprop="name">'.$pageInfo['pageNavText'].'</span><meta itemprop="position" content="'.$num.'" /></li>');
                    }

                ?>
                <!--this was perch:after-->
                </ol>
                </div>
                <!--this was perch:after-->
Drew McLellan

Drew McLellan 2638 points
Perch Support

I would create a template filter to add whatever value you want to the index.

https://docs.grabaperch.com/api/template-filters/