Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2020-10-09 15:03:59

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

check if viewing category/author in section context

Hi again gurus,

Can you let me know how I can check whether a category/author list is being viewed within a section context? i.e. when viewed via…

<txp:category title link this_section />
<txp:author link this_section />

…as opposed to the sectionless…

<txp:category title link />
<txp:author link />

Thanks all!

Offline

#2 2020-10-09 19:35:12

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: check if viewing category/author in section context

Does <txp:if_section /> not work?

Offline

#3 2020-10-09 21:37:46

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: check if viewing category/author in section context

Probably 😀

Will check on Monday and let you know either way. Ta!

Offline

#4 2020-10-10 08:56:46

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: check if viewing category/author in section context

Hi again, is there a more DRY way of writing this in Textpattern? Seems like a lot of repeated code to me which I’m sure there’s a clever way to reduce?

<nav class="breadcrumbs-container" aria-label="<txp:text item="breadcrumb_nav" />">
    <ol class="breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">
        <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
            <a itemprop="item" href="<txp:site_url />"><span itemprop="name"><txp:text item="home" /></span></a>
            <meta itemprop="position" content="1">
        </li>
        <txp:if_search>
            <li class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                <b itemprop="name" aria-current="location"><txp:text item="search_results" /></b>
                <meta itemprop="item" content="<txp:site_url />?q=">
                <meta itemprop="position" content="2">
            </li>
        <txp:else />
            <txp:if_author>
                <txp:if_section>
                    <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                        <a itemprop="item" href="<txp:section url />"><span itemprop="name"><txp:section title /></span></a>
                        <meta itemprop="position" content="2">
                    </li>
                    <li class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                        <b itemprop="name" aria-current="location"><txp:author /></b>
                        <meta itemprop="item" content="<txp:author format="url" />">
                        <meta itemprop="position" content="3">
                    </li>
                <txp:else />
                    <li class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                        <b itemprop="name" aria-current="location"><txp:author /></b>
                        <meta itemprop="item" content="<txp:author format="url" />">
                        <meta itemprop="position" content="2">
                    </li>
                <txp:if_section>
            <txp:else />
                <txp:if_category>
                    <txp:if_section>
                        <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                            <a itemprop="item" href="<txp:section url />"><span itemprop="name"><txp:section title /></span></a>
                            <meta itemprop="position" content="2">
                        </li>
                        <li class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                            <b itemprop="name" aria-current="location"><txp:category title /></b>
                            <meta itemprop="item" content="<txp:category url />">
                            <meta itemprop="position" content="3">
                        </li>
                    <txp:else />
                        <li class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                            <b itemprop="name" aria-current="location"><txp:category title /></b>
                            <meta itemprop="item" content="<txp:category url />">
                            <meta itemprop="position" content="2">
                        </li>
                    <txp:if_section>
                <txp:else />
                    <txp:if_article_list>
                        <li class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                            <b itemprop="name" aria-current="location"><txp:section title /></b>
                            <meta itemprop="item" content="<txp:section url />">
                            <meta itemprop="position" content="2">
                        </li>
                    <txp:else />
                        <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                            <a itemprop="item" href="<txp:section url />"><span itemprop="name"><txp:section title /></span></a>
                            <meta itemprop="position" content="2">
                        </li>
                        <li class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                            <b itemprop="name" aria-current="location"><txp:title /></b>
                            <meta itemprop="item" content="<txp:permlink />">
                            <meta itemprop="position" content="3">
                        </li>
                    </txp:if_article_list>
                </txp:if_category>
            </txp:if_author>
        </txp:if_search>
    </ol>
</nav>

Offline

#5 2020-10-10 12:05:18

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: check if viewing category/author in section context

As far as I can see, a common pattern is

            <li class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                <b itemprop="name" aria-current="location"><!-- name --></b>
                <meta itemprop="item" content="<!-- item -->">
                <meta itemprop="position" content="<!-- position -->">
            </li>

You could try to fill <!-- items --> inside your conditional blocks, like

            <txp:if_author>
                <txp:variable name="name" value='<txp:author />' />
                <txp:if_section>
                    <li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                        <a itemprop="item" href="<txp:section url />"><span itemprop="name"><txp:section title /></span></a>
                        <meta itemprop="position" content="2">
                    </li>
                    <txp:variable name="position" value="3" />
                <txp:else />
                    <txp:variable name="position" value="2" />
                <txp:if_section>
            <txp:else />
....

and the append the common block after conditionals:

            <li class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                <b itemprop="name" aria-current="location"><txp:variable name="name" /></b>
                <meta itemprop="item" content="<txp:variable name="item" >">
                <meta itemprop="position" content="<txp:variable name="position" />">
            </li>

Offline

Board footer

Powered by FluxBB