Getting the current article's category ID

Getting the current article's category ID

Postby chronot150 on Fri Jan 25, 2008 11:28 am

The first-post pleasantries:

Hi, I'm Dan.

Thank you for such an awesome product. I installed and uninstalled a handful of blog platforms before coming across LifeType, and there's no way I'm looking back.

OK, so here's what I'm working on:

I've customized my design pretty heavily. I've got a nav menu for my whole site on the left side. On static pages, I have the current page's menu item highlighted (so if I'm at the home page, the "Home" link is highlighted).

I'm trying to get the dynamic pages to do the same. I used what I read from other posts to get the blog ID of the blog or article I'm looking at:

Code: Select all
$blog = $this->_tpl_vars["blog"];
$bid = $blog->getId();


So then I just put an {if} in each link, changing the color if the blog id corresponds with that blog. It works wonderfully.

One of the blogs, however, is basically just a set of other blogs, so I have a sub-menu show up when that blog is selected. Each of the "sub-blogs" is a category in the parent blog. So, when said blog is selected, the menu looks like this:

-blog

-blog

-blog
---category
---category
---category

If you haven't guessed already, I want to do the same thing for the categories as the blogs. I print_r'd _tpl_vars, and I can see the correct category there, I just don't know how to extract it from the array. It comes right after the "text" part, and looks like this:

Code: Select all
[_categoryIds] => Array ( [0] => 7 )


If you can tell me how to use that value, you will alleviate the two-day headache I've had from trying to figure this out.

Thanks again,
Dan
chronot150
 
Posts: 6
Joined: Fri Jan 25, 2008 6:44 am

Postby jondaley on Fri Jan 25, 2008 6:27 pm

Not quite following you, but I think you are asking for:

Code: Select all
$ids = $this->_tpl_vars["_categoryIds"];
foreach($ids as $id){
  dostuff($id);
}


Part of the reason I am not following you isn't that I haven't ever used the _tpl_vars variable, but used our api instead like the rest of the template code.

It isn't guaranteed that when you hack into the available variables directly that those will stay consistent, so it would be better to use the smarty variable:

Code: Select all
{foreach name=categories from=$post->getCategories() item=postCategory}
      {assign var=categoryName value=$postCategory->getName()}
      <a href="{$url->categoryLink($postCategory)}">{$categoryName}</a>:


etc.
jondaley
Lifetype Expert
 
Posts: 6169
Joined: Thu May 20, 2004 6:19 pm
Location: Pittsburgh, PA, USA
LifeType Version: 1.2.11 devel branch

Postby chronot150 on Fri Jan 25, 2008 11:31 pm

Jon,

Thanks for the reply.

Your method works great as long as I'm viewing one of the posts in a category. If I go to another blog, it gives me

Code: Select all
Fatal error: Call to a member function on a non-object in /hsphere/local/home/website/website.com/blog/tmp/1/%%8D^8D4^8D4BED01%%header.template.php on line 59


Line 59 in that file is

Code: Select all
<?php $_from = $this->_tpl_vars['post']->getCategories(); if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array'); }$this->_foreach['categories'] = array('total' => count($_from), 'iteration' => 0);


I tried wrapping the {foreach} you gave me in {if $postCategory}, but that's not what I need; it doesn't pass on any page. Do you know what I need to do to fix this?

Dan
chronot150
 
Posts: 6
Joined: Fri Jan 25, 2008 6:44 am

Postby jondaley on Sat Jan 26, 2008 2:24 am

You are trying to get to all of the categories on any page? or do you want the if statement to only run this on a post page?

There is an $articlecategories object that has all of the categories in your blog.

Can you put your code in the postandcomments.template? That only gets shown on a post page.

I use this code (in main.template) to detect whether I am on the front page or not. I was looking at this code the other day, and I think the $showAll is old, ie. I don't think that actually gets set anywhere.

Code: Select all
{if $category || $date || $showAll || $smarty.env.REQUEST_URI != "/" }
{assign var="OnMainPage" value=0}
{else}
{assign var="OnMainPage" value=1}
{/if}
jondaley
Lifetype Expert
 
Posts: 6169
Joined: Thu May 20, 2004 6:19 pm
Location: Pittsburgh, PA, USA
LifeType Version: 1.2.11 devel branch

Postby chronot150 on Sat Jan 26, 2008 4:47 am

I think it will have to be in the header.template since that's where the menu is. I'll try to clarify: When it all boils down, I want a variable which reflects the category that I'm currently viewing. For instance, if my permalink looks like this...

http://www.website.com/blog/blog/(BLOGNAME)/(CATEGORYNAME)/2008/01/21/(TITLE)

...I want a variable with the value (CATEGORYNAME). That's why I said earlier it would be useful to get the ID of the category from the _tpl_vars array, since the printout of the array shows that what I want is stored there.

Code: Select all
[_categoryIds] => Array ( [0] => 7 )


That is exactly what I want, I just don't know how to get that value into my template.
chronot150
 
Posts: 6
Joined: Fri Jan 25, 2008 6:44 am

Postby jondaley on Sat Jan 26, 2008 3:45 pm

you are using the smarty foreach code I posted? I think all you need is a surrounding {if $post} around the foreach.
jondaley
Lifetype Expert
 
Posts: 6169
Joined: Thu May 20, 2004 6:19 pm
Location: Pittsburgh, PA, USA
LifeType Version: 1.2.11 devel branch

Postby chronot150 on Sat Jan 26, 2008 5:58 pm

That's perfect. Thanks for the help.

Dan
chronot150
 
Posts: 6
Joined: Fri Jan 25, 2008 6:44 am

Postby chronot150 on Sat Jan 26, 2008 7:34 pm

If this thread applies to anyone else, I added an {else} so it works on category view, too. The whole shebang looks like this:

Code: Select all
{php}
$blog = $this->_tpl_vars["blog"];
$bid = $blog->getId();
{/php}
{if $post}
{foreach name=categories from=$post->getCategories() item=postCategory}
{assign var=cid value=$postCategory->getId()}
{/foreach}
{elseif $category}
{assign var=cid value=$category->getId()}
{/if}


and samples of my links look like this, for blog and category respectively:

Code: Select all
<a href="/blog/blog/blog1/" style="color: {php}if($bid==1){echo "#3399cc";}{/php}">BLOG</a>
<a href="/blog/blog/blog/category1/" style="color: {if $cid==1}#3399cc{/if}">CATEGORY 2</a>


Dan
Last edited by chronot150 on Sun Jan 27, 2008 3:51 pm, edited 1 time in total.
chronot150
 
Posts: 6
Joined: Fri Jan 25, 2008 6:44 am

Postby jondaley on Sun Jan 27, 2008 12:51 am

I am not sure if that foreach code is doing what you think it is - at least, if you have a post put into more than one category, it will choose the last category to display. But, maybe you don't care about that.
jondaley
Lifetype Expert
 
Posts: 6169
Joined: Thu May 20, 2004 6:19 pm
Location: Pittsburgh, PA, USA
LifeType Version: 1.2.11 devel branch

Postby chronot150 on Sun Jan 27, 2008 3:50 pm

Yeah, I understand that it's a little sloppy, but since the category links will only be displayed when one certain blog is selected, and that blog only has posts with one category each, it doesn't cause a problem.
chronot150
 
Posts: 6
Joined: Fri Jan 25, 2008 6:44 am


Return to Templates

cron