Forum
Perch:if against blog category?
I have a page element that I would like to display only if the post is in a certain category, but I can't get it to work. I'm guessing it's to do with the query?
This is my post template, I'd be grateful of any guidance.
<article class="h-entry">
<perch:if id="categories" match="eq" value="news-views" >
<div id="news-views-header">
<div class="container">
<div id="news-views-issue">
<h5>News & Views</h5>
<p><perch:blog id="postDateTime" type="date" time="true" format="%Y" />: <perch:categories id="categories" set="blog" label="Categories" display-as="checkboxes"><perch:category id="catTitle" type="text" /></perch:categories></p>
</div>
</div>
</div>
</perch:if>
<div class="page-header cover <perch:if exists="image"><perch:blog id="theme" label="Cover Photo Colour" type="select" options="dark,light" order="3" />" style="background-image: url(<perch:blog id="image" type="image" label="Cover Photo" order="2" />);background-size: cover;"</perch:if>">
<div class="container question-title">
<h1><perch:blog id="postTitle" type="text" label="Title" required="true" size="xl autowidth" order="1" /></h1>
</div>
</div>
<div id="intro">
<div class="container">
<perch:blog id="excerpt" type="textarea" label="Intro" markdown="true" order="4" size="s" />
</div>
</div>
<div id="content" class="container">
<!-- BLOCKS -->
<perch:blocks order="5">
<perch:block type="text" label="Text">
<perch:blog id="text" type="textarea" label="Text" markdown="true" editor="markitup" />
</perch:block>
<perch:block type="largeheading" label="Large Heading">
<h4><perch:blog id="heading" type="text" label="heading" title="true" /></h4>
</perch:block>
<perch:block type="mediumheading" label="Medium Heading">
<h5><perch:blog id="heading" type="text" label="heading" required="true" title="true" /></h5>
</perch:block>
<perch:block type="smallheading" label="Small Heading">
<h6><perch:blog id="heading" type="text" label="heading" required="true" title="true" /></h6>
</perch:block>
<perch:block type="pullquote" label="Pull Quote">
<blockquote class="<perch:blog id="class" type="select" options="Pull left|left,Pull right|right,Centre|centre" label="Alignment" order="2" />">
<perch:blog id="pullQuote" type="textarea" size="xs" label="Pull Quote" />
</blockquote>
</perch:block>
<perch:block type="image" label="Full Image">
<img class="full-image" src="<perch:blog type="image" id="LargeImage" label="Image" width="1400" />" alt="<perch:blog type="text" id="alt" label="Description" help="e.g. Photo of MD John Smith with his best wig on" title="true" />" />
</perch:block>
<perch:block type="mediumimage" label="Medium Image">
<div class="medium-image <perch:blog id="class" type="select" options="Pull left|left,Pull right|right,Centre|centre" label="Alignment" order="2" />">
<img src="<perch:blog type="image" id="MediumImage" label="Image" width="700" />" />
<perch:if exists="caption"><p class="caption"><perch:blog type="textarea" size="xs" id="caption" label="Image Caption" /></p></perch:if>
</div>
</perch:block>
<perch:block type="twoimages" label="2 Images">
<div class="two-images">
<img class="left" src="<perch:blog type="image" id="LeftImage" label="Left Image" width="700" />" />
<img class="right" src="<perch:blog type="image" id="RightImage" label="Right Image" width="700" height="500" />" />
</div>
</perch:block>
<perch:block type="smallimage" label="Small Image">
<div class="small-image <perch:blog id="class" type="select" options="Pull left|left,Pull right|right,Centre|centre" label="Alignment" order="2" /> ">
<img src="<perch:blog type="image" id="SmallImage" label="Image" width="250" />" />
<perch:if exists="caption"><p class="caption"><perch:blog type="textarea" size="xs" id="caption" label="Image Caption" /></p></perch:if>
</div>
</perch:block>
<perch:block type="youtube" label="YouTube Video">
<div class="video-container">
<perch:blog id="YouTube" type="youtube" label="YouTube URL" output="embed" />
</div>
</perch:block>
</perch:blocks>
<div class="author">
<perch:if exists="author_image">
<img src="<perch:blog id="author_image" type="image" label="Author Image" type="hidden" />" width="60" />
</perch:if>
<p class="meta">Posted
<perch:if exists="authorGivenName">
by <a href="/blog/archive.php?author=<perch:blog id="authorSlug" type="hidden" />"><span class="p-author h-card"><perch:blog id="authorGivenName" type="hidden" /> <perch:blog id="authorFamilyName" type="hidden" /></span></a>
</perch:if>
on <time class="dt-published" datetime="<perch:blog id="postDateTime" type="date" label="Date" time="true" format="Y-m-d H:i:s" divider-before="Meta information" />"><perch:blog id="postDateTime" type="date" time="true" format="%d %B %Y" /></time>.</p>
<perch:categories id="categories" set="blog" label="Categories" display-as="checkboxes">
<a href="archive.php?cat=<perch:category id="catSlug" type="slug" />" class="p-category">
<perch:category id="catTitle" type="text" />
</a>
</perch:categories>
</div> <!-- Close Author -->
</div> <!-- Close Container -->
</article>
Why do this in the template?
Hello Phil,
Your
<perch:if>
tags should be inside your<perch:categories>
tags.Assuming
news-views
is thecatSlug
:Simple answer, I don't know. I probably did it because it would appear above the post before it's called. I've moved it to my post.php and it loads the element, but on every post regardless of category and isn't taking the if statement into account (including taking into account what Hussein said). Here's my post.php code.
Here's the debug as well:
Perch tags aren't rendered in PHP. You can keep your tags in your HTML template.
I think Drew was suggesting a different approach.
Do you want to always display the post, or only display the post if it's in a certain category?
Oh right. I guess I could do a second post type, which made me realise I was trying to do it this way because I wanted to keep things simple (for my client to update) and some posts have already been published in their News & Views series.
I've now got things mostly working with this query in my post template, the only bit not working is the Year.
The posts always appear, I just wanted to only display a page element when the post was in a specific category.
Post types should be used if you need to have different edit forms for different types of posts. So I don't think this is the best solution here.
I don't think you can have
perch:blog
tags insideperch:categories
pair tag.Also I see you have a
perch:categories
pair tag inside anotherperch:categories
pair tag. You don't have to do this.You're also missing the
id
attribute in yourperch:if
tag.The following works except for the date:
One solution is to pass a variable with the date to the template so you can use it in the
perch:category
namespace:In your
post.php
:In your template:
You can, you just need to bring the parent content into scope with
scope-parent="true"
Good to know! I wasn't aware this worked for categories too. This makes things easier:
Thanks guys, finally returned to this and got it working. Appreciate your help.