Forum

Thread tagged as: Blog

Blog : Displaying images based on cat in post.html

Hi I have a blog with 3 categories.
I have 3 header images, one for each category. My problem is that although i can get the blog/index page to display the right image, i cant get it to wrok from the post.php page.

The bit that works : blog/index.php has an if statement

<?php
if(perch_get('cat') == 'news') {
  $img = 'image1.jpg';
} etc

to decide which image to use.

The bit that doesnt work : However, I can't get it to work on the post.php page. I have tried using

<?php perch_blog_post_field(perch_get('s'), 'post-photo.html'); ?>

with post-photo.html template displaying the image <img src="../images/<?php echo $img; ?>" />

but it doesnt work. I think the problem is to do with the fact the category is easy to see (on url) on blog/index but on the post up it has the post title - which is why I was trying to use perch-get to find out the category from the post page. But I can't be doing it right!

Thanks

https://galvininternational.com/blog/index.php?cat=case

cow shed

cow shed 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Are the images a field attached to the category?

You can get the post categories with:

perch_blog_post_categories(perch_get('s'));

The images are not in a field - I set them depending on which category it is - eg image 1 goes with category = "news".

I have 2 bits of code going on and I'm struggling to make them work together :

POST Page :

<?php
if(perch_blog_post_categories('cat') == 'news') {
  $img = 'news-galvininternationalaccounting.jpg';
}

 elseif (perch_blog_post_categories('cat') == 'blog') {
  $img = 'blog-galvininternationalaccounting.jpg';
}

 elseif (perch_blog_post_categories('cat') == 'case') {
  $img = 'case-studies-galvininternationalaccounting.jpg';
}

else {
  $img = 'approach-galvininternationalaccounting.jpg';
}
?>

and

    <img src="../images-johngalvininternational/page-johngalvininternationalaccountingtaxpayroll/<?php echo $img; ?>"  />

I've got it working on the blog/index page

<?php
if(perch_get('cat') == 'news') {
  $img = 'news-galvininternationalaccounting.jpg';
    $template = 'blog/post_in_list.html';
    $content = 'news';
}

 elseif (perch_get('cat') == 'blog') {
  $img = 'blog-galvininternationalaccounting.jpg';
  $template = 'blog/post_in_list.html';
  $content = 'blog';
}

 elseif (perch_get('cat') == 'case') {
  $img = 'case-studies-galvininternationalaccounting.jpg';
  $template = 'blog/post_in_list.html';
  $content = 'case';
}
elseif (perch_get('cat') == 'news') {
  $img = 'home-galvininternationalaccounting.jpg';
    $template = 'blog/post_in_list.html';
    $content = 'news';
}


 else {
  $img = 'approach-galvininternationalaccounting.jpg';
  $template = 'blog/post_in_list.html';
  $content = 'case';
}
?>

and

        <?php if (perch_get('cat')) {perch_content($content);}  ?> etc

Also on the index page I can get the navigation active class to work using

<li <?php if (perch_get('cat') == 'case'){echo 'class="active"';};?>><a href="index.php?cat=case"><strong>CASE STUDIES</strong></a></li>

but it wont work on the post.php page.

I have managed to get one thing working on the post.php file - I can display the category description based on the category (using a template) so I know it must be possible, but i cant make the image display the same way.

(code in post.php =

<?php perch_blog_post_categories(perch_get('s'), 'post-back-to-line.html'); ?>

code in template post-back-to-line.html :

<perch:before>
<ul>
</perch:before>
   <div class="text-right">
     <a href="index.php?cat=<perch:category id="catSlug" />"> <perch:category id="desc" /></a>
   </div>
<perch:after></ul></perch:after>
Drew McLellan

Drew McLellan 2638 points
Perch Support

This is incorrect:

if(perch_blog_post_categories('cat') == 'news')

The first argument to perch_blog_post_categories() needs to be the slug of the post. It will then return your templated categories. Remember a post can have many categories, not just one, so you'll need to anticipate that.

You can pass an array of options as the second argument, including skip-template, which will give you back an array.

$categories = perch_blog_post_categories(perch_get('s'), [
    'skip-template' => true,
]);

https://docs.grabaperch.com/addons/blog/page-functions/perch-blog-post-categories/

Yes I managed to use this functionality to display the category description by using a template called post-back-to-line.html <?php perch_blog_post_categories(perch_get('s'), 'post-back-to-line.html'); ?>

However, this was very simple. I'm struggling with the if statement because as you say I have 3 categories not just one.

If i cant use if(perch_blog_post_categories('cat') == 'news') etc, how can I decide which template to use based on the category returned?

I really want to do something like <?php perch_blog_post_categories(perch_get('s'),

if cat = news use 'template 1' else if cat = blog use template 2 etc

nb what i really want is if cat = news display image 1

Can you help?

Drew McLellan

Drew McLellan 2638 points
Perch Support

If i cant use if(perch_blog_post_categories('cat') == 'news') etc, how can I decide which template to use based on the category returned?

Because the syntax is simply incorrect. You can do what you're proposing, but not with that code.

Personally I would name the image files the same as the category slugs and then you'd not need the if statement at all.

Thats a brilliant idea but I cant quite get it to work. Slightly complicated by fact I'm testing on the live server and there is a massive delay on the server picking up the just updated template (I clear cookies etc but still cant get round delay).

post.php has :

<?php perch_blog_post_categories(perch_get('s'), 'post-photo.html'); ?>

post-photo.html has :

<img src="../images-johngalvininternational/page-johngalvininternationalaccountingtaxpayroll/image?cat=<perch:category id="catSlug" />.jpg" alt="John Galvin International Accounting Tax Payroll" class="img-responsive" />  

should it be image?cat=<perch:category id="catSlug" />.jpg or image?"catSlug".jpg or image?'catSlug'.jpg

I know I'm nearly there becase post-photo.html works when i hardcode imagenews.jpg etc

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is that the whole template? Remember you need to include the <perch:categories id="..." set="..."> </perch:categories> section to output categories in a template.

It worked when i added perch before and after

<perch:before>



     <img src="../images-johngalvininternational/page-johngalvininternationalaccountingtaxpayroll/image<perch:category id="catSlug" />.jpg" alt="John Galvin International Accounting Tax Payroll" class="img-responsive" /> 


</perch:after>

because I didnt need to set anything as the images are named after the slug. Not sure if this was right way to do it but it worked!

Just one more thing .. so nearly there..

I want to be able to set the menu items to active depending on the category, but once I am in the post, i still have to get hold of the category. So in blog/index I was able to use :

<li <?php if (perch_get('cat') == 'case'){echo 'class="active"';};?>><a href="index.php?cat=case"><strong>CASE STUDIES</strong></a></li>

But in post.php I have called another template post-menu.html :

<?php perch_blog_post_categories(perch_get('s'), 'post-menu.html'); ?>

The code in post-menu.html is :

<perch:before>

     <li <?php if (<perch:category id="catSlug" /> == 'case'){echo 'class="active"';};?>><a href="index.php?cat=case"><strong>CASE STUDIES</strong></a></li>

      <li <?php if (<perch:category id="catSlug" />) == 'news'){echo 'class="active"';};?>><a href="index.php?cat=news"><strong>NEWS</strong></a></li>
               <li <?php if (<perch:category id="catSlug" />) == 'blog'){echo ' class="active"';};?>><a href="index.php?cat=blog"><strong>BLOG</strong></a></li>


</perch:after>

But that doesnt set the class to active. Is there a way i can just call the category from post.php like i did in the index and use the catSlug as before?

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's not the way perch:before and perch:after work at all. If something's doing what you expected with that syntax it's by fluke alone. Please check the documentation.

https://docs.grabaperch.com/docs/templates/conditionals/before/