Forum

Thread tagged as: Question, Blog

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 &amp; 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>
Phil Bowell

Phil Bowell 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Why do this in the template?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Phil,

Your <perch:if> tags should be inside your <perch:categories> tags.

Assuming news-views is the catSlug:

<perch:categories id="categories" set="blog">
    <perch:if id="catSlug" value="news-views">
        <h1><perch:category id="catTitle" type="text" /></h1>
    </perch:if>
</perch:categories>

Drew McLellan said:

Why do this in the template?

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.

<?php include('../perch/runtime.php'); ?>
<?php perch_layout('blog.header'); ?>

    <!--  change cols2-nav-right to cols2-nav-left if you want the sidebar on the left -->
<div id="blog">

    <perch:categories id="categories" set="blog">
      <perch:if id="catSlug" 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>
    </perch:categories>

    <div class="post">
        <?php perch_blog_post(perch_get('s')); ?>
    </div>

    <div class="comments-section">
      <?php perch_blog_post_comments(perch_get('s')); ?>

      <?php perch_blog_post_comment_form(perch_get('s')); ?>
    </div>

    <?php PerchUtil::output_debug(); ?>


</div>

<?php perch_layout('global.footer'); ?>

</body>
</html>

Here's the debug as well:


Debug Message [1] SELECT * FROM perch2_pages WHERE pagePath='/blog/post.php' LIMIT 1 [9] SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageDepth >=0 AND pageDepth<=1 ORDER BY pageTreePosition ASC [1] SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/blog/post.php' LIMIT 1 [2] SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-006-001', '000-006', '000') ORDER BY pageTreePosition DESC [9] Using template: /templates/navigation/item.html [1] SELECT SQL_CALC_FOUND_ROWS DISTINCT tbl.* FROM ( SELECT idx.itemID, main.*, idx2.indexValue as sortval FROM perch2_blog_index idx JOIN perch2_blog_posts main ON idx.itemID=main.postID AND idx.itemKey='postID' JOIN perch2_blog_index idx2 ON idx.itemID=idx2.itemID AND idx.itemKey='postID' AND idx2.indexKey='_id' WHERE 1=1 AND ((idx.indexKey='postSlug' AND idx.indexValue='the-return-journey-by-lord-allenby')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey ) as tbl WHERE (postStatus='Published' AND postDateTime<='2018-01-09 17:26:00' ) GROUP BY itemID, sortval ORDER BY sortval ASC LIMIT 0, 10 [1] SELECT FOUND_ROWS() AS `count` [44] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0 [1] Using template: /templates/blog/posts/new-freeform.html [13] SELECT * FROM perch2_blog_authors ORDER BY authorFamilyName, authorGivenName ASC [3] SELECT * FROM perch2_blog_sections ORDER BY sectionTitle ASC [10] SELECT * FROM perch2_blogs ORDER BY blogTitle ASC [2] SELECT * FROM perch2_categories ORDER BY catTreePosition ASC [1] SELECT * FROM perch2_blog_posts WHERE postStatus='Published' AND postDateTime<='2018-01-09 17:26:00' AND postSlug='the-return-journey-by-lord-allenby' [nil] SELECT * FROM perch2_blog_comments WHERE 1=1 AND postID=194 AND commentStatus='LIVE' ORDER BY commentDateTime ASC [] Using template: /templates/blog/comment.html [1] SELECT * FROM perch2_blog_posts WHERE postID=194 AND postStatus='Published' AND postDateTime<='2018-01-09 17:26:00' Using template: /templates/blog/comment_form.html
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Perch tags aren't rendered in PHP. You can keep your tags in your HTML template.

I think Drew was suggesting a different approach.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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.

<perch:categories id="categories" set="blog">
<perch:if match="eq" value="news-views" >
<div id="news-views-header">
  <div class="container">
  <div id="news-views-issue">
    <h5>News &amp; 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>
</perch:categories>

Drew McLellan said:

Do you want to always display the post, or only display the post if it's in a certain category?

The posts always appear, I just wanted to only display a page element when the post was in a specific category.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

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.

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've now got things mostly working with this query in my post template, the only bit not working is the Year.

I don't think you can have perch:blog tags inside perch:categories pair tag.

Also I see you have a perch:categories pair tag inside another perch:categories pair tag. You don't have to do this.

You're also missing the id attribute in your perch:if tag.

The following works except for the date:

<perch:categories id="categories" set="blog">
    <perch:if id="catSlug" value="news-views" >
        <div id="news-views-header">
            <div class="container">
                <div id="news-views-issue">
                    <h5>News &amp; Views</h5>
                    <p><perch:blog id="postDateTime" type="date" time="true" format="%Y" />: <perch:category id="catTitle" type="text" /></p>
                </div>
            </div>
        </div>
    </perch:if>
</perch:categories>

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:

$post_date = perch_blog_post_field(perch_get('s'), 'postDateTime', true); 
PerchSystem::set_var('post_date', $post_date);
perch_blog_post(perch_get('s'));

In your template:

<perch:categories id="categories" set="blog">
    <perch:if id="catSlug" value="news-views" >
        <div id="news-views-header">
            <div class="container">
                <div id="news-views-issue">
                    <h5>News &amp; Views</h5>
                    <p><perch:category id="post_date" format="%Y" />: <perch:category id="catTitle" type="text" /></p>
                </div>
            </div>
        </div>
    </perch:if>
</perch:categories>
Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't think you can have perch:blog tags inside perch:categories pair tag.

You can, you just need to bring the parent content into scope with scope-parent="true"

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

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:

<perch:categories id="categories" set="blog" scope-parent="true">
    <perch:if id="catSlug" value="news-views" >
        <div id="news-views-header">
            <div class="container">
                <div id="news-views-issue">
                    <h5>News &amp; Views</h5>
                    <p><perch:category id="parent.postDateTime" type="date" time="true" format="%Y" />: <perch:category id="catTitle" type="text" /></p>
                </div>
            </div>
        </div>
    </perch:if>
</perch:categories>

Thanks guys, finally returned to this and got it working. Appreciate your help.