Forum

Thread tagged as: Question, Blog

How to exclude blog post I'm reading from aside column

Hi

On our blog posts we have an aside column which shows the latest 5 blog posts, using perch_blog_custom

I'm trying to exclude the post your currently viewing from displaying in the aside list. I've got so far...

$postID = PerchSystem::get_var('postID');
                perch_blog_custom(array(
                    'sort' => 'postDateTime',
                    'sort-order' => 'DESC',
                    'count' => 5,
                    'template' => 'blog-post-aside.html',
                    'filter' => 'postID',
                    'match' => 'neq',
                    'value' => $postID,
                ));

...but it's not working.

How do I pass postID to perch_blog_custom so it can filter out the current post you're reading from the aside column.

Many thanks

Glen Piggott

Glen Piggott 0 points

  • 7 months ago

Hello Glen,

Assuming you already know the slug of the current post (perhaps you're getting it with perch_get('s')), it would be easier to use it for your filter:

perch_blog_custom(array(
    'sort' => 'postDateTime',
    'sort-order' => 'DESC',
    'count' => 5,
    'template' => 'blog-post-aside.html',
    'filter' => 'postSlug',
    'match' => 'neq',
    'value' => perch_get('s'),
));

Spot on, thanks Hussein

Hi Hussein Al Hammad

I don't suppose you also know how to only show posts from the same category of the main post?

I've currently got...

perch_blog_custom(array(
                    'sort' => 'postDateTime',
                    'sort-order' => 'DESC',
                    'count' => 5,
                    'template' => 'post_aside.html',
                    'filter' => 'postSlug',
                    'match' => 'neq',
                    'value' => perch_get('s'),
                    'category' => perch_get('catDept'),
                ));

...but it's not showing anything.

catDept is the category ID of the main post that I wish to show other posts that have the same category. There are 2 categories for each post if that makes a difference, department and office?

Many thanks

I believe you need to use the category path for filtering. If you want to filter for multiple categories, you need to use an array 'category' => ['blog/category1', 'blog/category2'].

Since you already know the post's slug, I'd use perch_blog_post_categories() to get the post's categories.

$cats = perch_blog_post_categories(perch_get('s'), [
'template' => 'cat_path.html',
'skip-template' => true,
]);

$cat_paths = array();
foreach($cats as $cat) {
    $cat_paths[] = $cat['catPath'];
}

templates/blog/cat_path.html:

<perch:category id="catPath" />

Then you should be able to use 'category' => $cat_paths.

Thanks very much for your help Hussein.

Unfortunately, that's showing all posts from all categories.

My aim is to only show other posts that are in the same category as the main post you're reading.

I'm only filtering by one category ID catDept but the path needs to be dynamic, depending on what post your viewing.

Here's what I've got so far...

$cats = perch_blog_post_categories(perch_get('s'), [
                        'template' => 'cat_path.html',
                        'skip-template' => true,
                    ]);

                    $cat_paths = array();
                    foreach($cats as $cat) {
                        $cat_paths[] = $cat['catPath'];
                    }

                    perch_blog_custom([
                        'sort' => 'postDateTime',
                        'sort-order' => 'DESC',
                        'count' => 5,
                        'template' => 'post_aside.html',
                        'category' => $cat_paths,
                        'filter' => 'postSlug',
                        'match' => 'neq',
                        'value' => perch_get('s'),
                    ]);

I'm only filtering by one category

I thought you wanted to display posts that belong to any category which the current post belongs to!

the path needs to be dynamic, depending on what post your viewing.

If you already know the category ID, you can get the path with perch_categories():

$cat_path = perch_categories([
    'template' => 'cat_path.html',
    'filter' => 'catID',
    'match' => 'eq',
    'value' => $catID,
], true);

The cat_path.html template needs to be in templates/categories

Sorry for any confusion with my explanation.

Thank you for all your help, but unfortunately, nothing is displaying, here's my code so far...

$cat_path = perch_categories([
                        'template' => 'cat_path.html',
                        'filter' => 'catID',
                        'match' => 'eq',
                        'value' => $catID,
                    ], true);

                    perch_blog_custom([
                        'sort' => 'postDateTime',
                        'sort-order' => 'DESC',
                        'count' => 5,
                        'template' => 'post_aside.html',
                        'category' => $cat_path,
                        'filter' => 'postSlug',
                        'match' => 'neq',
                        'value' => perch_get('s'),
                    ]);

and then /perch/templates/categories/cat_path.html looks like

<perch:category id="catPath" />

..what am I missing???

How are you setting the value of $catID? Did you confirm you're getting the correct category path?

Also, make sure cat_path.html doesn't have any whitespace.

$catID = perch_blog_category(perch_get('cat'));

perch_blog_category() outputs the title of a category.

What does perch_get('cat') get you? Do you have a URL parameter called cat?

No, the blog URLs are blog/[slug:s].

To be honest I'm just guessing now.

From the docs:

The perch_get() function does the same job as $_GET in PHP, but has some convenient safeguards and options built in.

So...

There are 2 categories for each post if that makes a difference, department and office?

I understand each post belongs to 2 categories and you want to filter on only one of these categories. How are you assigning which category is the "main" category?

It might be a good idea to open a new thread so others can help too (this is marked as solved so they may not check it)

Hi Hussein

Many thanks.

Yes, the blog posts have 2 category sets, "department" and "office".

On the blog post page, my aim was in the aside column to have the latest posts from the same "department" category as the post itself.

Can you show us your blog post template?

/perch/templates/pages/blog/post.php looks like this...

<?php perch_blog_check_preview(); ?>
<!doctype html>
<html lang="en-gb">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<?php perch_blog_post_meta(perch_get('s')); ?>

<link rel="alternate" type="application/rss+xml" title="RSS" href="/blog/rss">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Overpass:300,400,600,700">
<link rel="stylesheet" href="/assets/css/fontawesome-all.min.css">
<link rel="stylesheet" href="/assets/css/bootstrap-grid.min.css">
<link rel="stylesheet" href="/assets/css/global.css">
<link rel="stylesheet" href="/assets/css/blog.css">

<?php perch_blog_post_webmention_endpoint(perch_get('s')); ?>
</head>


<?php perch_layout('global/header'); ?>

<div class="container">
    <?php
        $title = perch_blog_post_field(perch_get('s'), 'postTitle', true);
        PerchSystem::set_var('postTitle', $title);
        perch_content_custom('Post Title', array(
            'template'=>'blog/post_title.html'
        ));
    ?>

    <div class="row">
        <article class="col-lg-9">
            <?php
                perch_blog_post(perch_get('s'));
                perch_blog_post_tags(perch_get('s'));
                perch_blog_post_comments(perch_get('s'));
                perch_blog_post_comment_form(perch_get('s'));
            ?>
        </article>

        <aside class="col-lg-3">
            <div class="popular-posts bg-black5">
                <?php
                    perch_categories([
                        'template' => 'cat_id.html',
                    ]);

                    perch_categories([
                        'template' => 'cat_path.html',
                        'filter' => 'catID',
                        'match' => 'eq',
                        'value' => $catID,
                    ]);

                    perch_blog_custom([
                        'sort' => 'postDateTime',
                        'sort-order' => 'DESC',
                        'count' => 5,
                        'template' => 'post_aside.html',
                        'category' => $cat_path,
                        'filter' => 'postSlug',
                        'match' => 'neq',
                        'value' => perch_get('s'),
                    ]);
                ?>
            </div>
        </aside>
    </div>


</div>

<?php
    perch_content('Accreditations');
    perch_content('Phone Numbers Banner');

    perch_layout('global/footer');
    perch_layout('global/js');
    perch_layout('home/js');
    perch_layout('global/tracking');

and here is /perch/templates/blog/post_aside.html

<perch:before>
<h3>Popular <perch:categories id="catDept" set="department"><perch:category id="catTitle" type="text" /></perch:categories> Posts</h3>
</perch:before>

    <a href="<perch:blog id="postURL" />" rel="bookmark" class="bg-white">
        <perch:if exists="image"><span class="img"><img src="<perch:blog id="image" type="image" width="600" height="315" crop="true" />" alt="<perch:blog id="postTitle" />" /></span><perch:else /><span class="img"><img src="/perch/resources/blog/no-image.png" alt="<perch:blog id="postTitle" />"></span></perch:if>
        <span class="details">              
            <perch:blog id="postTitle" />
            <small class="black60"><perch:blog id="postDateTime" format="d F Y" /></small>
        </span>
    </a>

    <perch:noresults>
        <h3>Popular <perch:categories id="catDept" set="department"><perch:category id="catTitle" type="text" /></perch:categories> Posts</h3>
        <p>No posts to show</p>
    </perch:noresults>

<perch:after>
</perch:after>


I actually wanted to see your templates/blog/post.html

Still work in progress but looks like this...

<perch:blog id="postTitle" type="text" label="Title" required="true" suppress="true" />
<perch:blog id="excerpt" type="textarea" label="Introduction Paragraph" size="s" suppress="true" />
<perch:blog id="postDescHTML" type="textarea" label="Post" editor="redactor" html="true" required="true" suppress="true" />

<perch:blog id="video" type="text" label="YouTube video ID" help="Leave blank if no video" divider-before="Video &amp; Image" suppress="true" />
<perch:blog id="image" type="image" label="Image" bucket="blog" width="1200" height="630" crop="true" help="Recommended image size: 1200 x 630px" suppress="true" />
<perch:blog id="image" type="image" width="600" height="315" crop="true" suppress="true" bucket="blog" />
<perch:blog id="imgCaption" type="text" label="Image caption" suppress="true" />

<perch:blog id="postDateTime" type="date" label="Date" time="true" format="Y-m-d H:i:s" divider-before="Meta information" suppress="true" />
<perch:categories id="catDept" set="department" label="Select which department(s) this post relates to" suppress="true"></perch:categories>
<perch:categories id="catOffice" set="office" label="Select which office(s) this post relates to" suppress="true"></perch:categories>









<perch:if exists="video">
    <div class="embed-responsive embed-responsive-16by9">
        <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/<perch:blog id="video" />?rel=0"></iframe>
    </div>    
<perch:else />
    <perch:if exists="image">
        <div class="black50 bg-black5 post-img">
            <img src="<perch:blog type="image" id="image" width="1200" height="630" crop="true" />" alt="<perch:blog id="postTitle" />">
            <perch:if exists="imgCaption"><p><perch:blog id="imgCaption" /></p></perch:if>
        </div>
    </perch:if>
</perch:if>


<div class="row">
    <div class="col-lg-3 post-info">
        <p><i class="fa fa-clock-o" aria-hidden="true"></i><time class="dt-published" datetime="<perch:blog id="postDateTime" />"><perch:blog id="postDateTime" type="date" time="true" format="%d %B %Y" /></time></p>
        <p><i class="fa fa-tags" aria-hidden="true"></i><perch:categories id="catDept" set="department" label="Select which department(s) this post relates to"><a href="archive.php?cat=<perch:category id="catSlug" type="slug" />" class="p-category"><perch:category id="catTitle" type="text" /></a>, </perch:categories></p>
        <li<perch:if not-exists="authorGivenName"> class="last"</perch:if>><i class="fa fa-commenting" aria-hidden="true"></i>Comments: <perch:blog id="postCommentCount" type="hidden"/></li>
            <perch:if exists="authorGivenName"><li class="last"><i class="fa fa-user" aria-hidden="true"></i>By <span class="p-author h-card"><perch:blog id="authorGivenName" type="hidden" /> <perch:blog id="authorFamilyName" type="hidden" /></span></li></perch:if>
        </ul>
    </div>

    <div class="col-lg-9">
        <p class="lead"><perch:blog id="excerpt" /></p>
        <perch:blog id="postDescHTML" html="true" />
    </div>
</div>





            <div class="share-buttons">
                <h6>Share this blog:</h6>
                <!-- AddToAny BEGIN -->
                <p class="a2a_kit a2a_kit_size_32 a2a_default_style">
                <a class="a2a_button_facebook a2a_counter"></a>
                <a class="a2a_button_twitter a2a_counter"></a>
                <a class="a2a_button_google_plus a2a_counter"></a>
                <a class="a2a_button_linkedin a2a_counter"></a>
                <a class="a2a_dd a2a_counter" href="https://www.addtoany.com/share"></a>
                </p>
                <script async src="https://static.addtoany.com/menu/page.js"></script>
                <!-- AddToAny END -->
            </div>

<perch:showall/>

ALL WORKING - thanks your all your help Hussein

                    $catID = perch_blog_post_field(perch_get('s'), 'catDept', true);

                    $cat_path = perch_categories([
                        'template' => 'cat_path.html',
                        'filter' => 'catID',
                        'match' => 'eq',
                        'value' => $catID,
                    ], true);

                    perch_blog_custom([
                        'sort' => 'postDateTime',
                        'sort-order' => 'DESC',
                        'count' => 5,
                        'template' => 'post_aside.html',
                        'category' => $cat_path,
                        'filter' => 'postSlug',
                        'match' => 'neq',
                        'value' => perch_get('s'),
                    ]);

Great!