Forum
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
Are the images a field attached to the category?
You can get the post categories with:
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 :
and
I've got it working on the blog/index page
and
Also on the index page I can get the navigation active class to work using
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 =
code in template post-back-to-line.html :
This is incorrect:
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.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?
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 :
post-photo.html has :
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
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
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 :
But in post.php I have called another template post-menu.html :
The code in post-menu.html is :
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?
That's not the way
perch:before
andperch: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/