Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-12-07 03:25:06

otti
Member
From: Australia
Registered: 2008-04-16
Posts: 75
Website

Keywords from category articles

I have used the Textpattern keyword tag for many websites successfully until now, where I have included some product categories. I have a section “product-categories” with a category assigned to. This page contains the links to other category pages of that section. Then I have various articles with the same section assigned to, but the each has a different category (category1).
The link: http://www.hssptyltd.com.au/product-categories
The code for my dynamic meta data as as follows:

<txp:if_category>
<title><txp:category title="1" /> - <txp:section title="1" /> | <txp:site_name /></title>
<meta name="Keywords" content="<txp:keywords />" />
<meta name="Description" content="<txp:custom_field name="meta_desc" />" />
<txp:else />
<title><txp:section title="1" /> | <txp:site_name /></title>
<meta name="Keywords" content="<txp:keywords />" />
<meta name="Description" content="<txp:custom_field name="meta_desc" />" />
</txp:if_category>

It is working, but only for the all pages without categories assigned to. When it comes to the categories the keywords from all the category articles do not get used. They all use the keywords from the main product-categories article.

I’m sure it is something simple to get the keywords from the category pages to be used. Thanks very much in advance.

Otti

Offline

#2 2011-12-07 06:22:31

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,001
Website GitHub

Re: Keywords from category articles

Hi Otti,

Keywords and custom fields are drawn from the respective fields in the individual article so will only be used if you are actually showing an article. txp:if_category only detects if a category is specified for a list page, but on section and category landing pages there’s no article to draw the infos from.

I think what you need instead is txp:if_article_category (tho’ I can’t remember off-hand whether it works without a name attribute). Worth a try.

If on the other hand you want to get the keywords and description for a landing / list page from the first article in that section or category, wrap your tags in a txp:article statement with limit="1" set and it should use the data from the first article that matches the section/category criteria…

Last edited by jakob (2011-12-07 06:31:33)


TXP Builders – finely-crafted code, design and txp

Offline

#3 2011-12-07 08:30:43

otti
Member
From: Australia
Registered: 2008-04-16
Posts: 75
Website

Re: Keywords from category articles

Hi Jakob,

Thanks for your reply. I have tried txp:if_article_custom as you suggested, but no luck. For some reason, txp:keywords does not go past the first article of that section. It will always get the keywords from the first article of that section. I don’t want to create a section for each category – that would defeat the purpose of the categories. I will have to keep looking, since no solution yet.

Otti

Offline

#4 2011-12-07 16:01:59

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,001
Website GitHub

Re: Keywords from category articles

Hi Otti, I hope you tried if_article_category and not if_article_custom (which doesn’t exist) as you wrote.

But anyway, from your description it sounds like you want keywords drawn from the first relevant article when you are in a category list or section list page. That should be possible with:

<title><txp:if_category><txp:category title="1" /> - </txp:if_category><txp:section title="1" /> | <txp:site_name /></title>
<txp:article limit="1">
  <meta name="keywords" content="<txp:keywords />" />
  <meta name="description" content="<txp:custom_field name="meta_desc" />" />
</txp:article>

txp:article is context-sensitive so doesn’t need an extra if_category. The only other thing that changes in the category list view is the category name in the title tag, so you only need txp:if_category for that. Note: If your first article is a sticky article (e.g. for the category description information) you’ll need <txp:article limit="1" status="sticky">...</txp:article>.


TXP Builders – finely-crafted code, design and txp

Offline

#5 2011-12-08 00:55:21

otti
Member
From: Australia
Registered: 2008-04-16
Posts: 75
Website

Re: Keywords from category articles

Hi Jakob,

Sorry for the wrong word in my tag, but yes I tried if_article_category.

I want the keywords drawn from articles which are all in one section, but have different categories assigned to. They display correctly but do not draw the keywords and description correctly. They draw both from the first article in that section. I think I might change all the categories and go to sections. This way there should be no hassle there.

Many thanks again
otti

Offline

#6 2011-12-08 09:00:18

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,001
Website GitHub

Re: Keywords from category articles

You should be able to do what you want – without hassle – with the setup you have.

I want the keywords drawn from articles which are all in one section, but have different categories assigned to.

Aha, so on the section and landing pages you want the keywords from all the articles in other categories, not just the category that is shown. Then try this:

<title><txp:if_category><txp:category title="1" /> - </txp:if_category><txp:section title="1" /> | <txp:site_name /></title>
<meta name="keywords" content="<txp:article_custom section="product-categories" category='<txp:category_list parent="product-categories" break=","><txp:category /></txp:category_list>' limit="999" break=""><txp:keywords /><txp:if_last_article><txp:else />,</txp:if_last_article></txp:article_custom>" />
<txp:article limit="1">
<meta name="description" content="<txp:custom_field name="meta_desc" />" />
</txp:article>

That should output the keyword field from all the articles in the section “product-categories” that are also in all the child categories of a parent category called “product-categories”. The txp:if_last_article inserts a comma between each set of keywords but not at the end of the list.

A couple of things to note:

  • If this is the only place you are using categories in the site you can leave out the parent bit, but if you are using categories elsewhere then you might need to organise your product-categories under a parent category name so that you can list only the relevant categories (you may already have that and just need to change the parent category name to match yours).
  • You may find that is is sufficient to show just the product category titles for the keyword field, rather than the keywords in each article in each field. In the past search engines didn’t log more than a certain number of keywords. Nowadays Google doesn’t pay attention to the meta keywords anyway. If you want to do that you can do: <meta name="keywords" content="<txp:category_list parent="product-categories" break=","><txp:category /></txp:category_list>" />.
  • I didn’t include the description in the example above as you’ll likely go way over the length limit for descriptions, but if you want to do that the same principle applies.

Last edited by jakob (2011-12-08 09:02:53)


TXP Builders – finely-crafted code, design and txp

Offline

#7 2011-12-08 11:29:50

otti
Member
From: Australia
Registered: 2008-04-16
Posts: 75
Website

Re: Keywords from category articles

Hi Jakob,

Wooh, thank you so much to go through all that effort and explaining to me how I could get this code to work. Apologies again if I was not quite clear enough. Your code works like a dream but there is (of course) the issue with all the keywords from all category articles within this section. Every page has no the same keywords and all of them, as you have pointed already out.

What I wanted is only the keywords from each article on that particular page i.e. only the keywords from the article accumulators on the Accumulators page. I have tried to modify your code and can get a solution but only with repetitive code:

<title><txp:if_category><txp:category title="1" /> - </txp:if_category><txp:section title="1" /> | <txp:site_name /></title>
<meta name="keywords" content="<txp:if_category name="cat-accumulators"><txp:article_custom section="product-categories" category='cat-accumulators' limit="999" break=""><txp:keywords /><txp:if_last_article><txp:else />,</txp:if_last_article></txp:article_custom></txp:if_category><txp:if_category name="cat-coolers"><txp:article_custom section="product-categories" category='cat-coolers' limit="999" break=""><txp:keywords /><txp:if_last_article><txp:else />,</txp:if_last_article></txp:article_custom></txp:if_category>" />
<txp:article limit="1">
<meta name="description" content="<txp:custom_field name="meta_desc" />" />
</txp:article>

As you can see I replaced your category list with a single category name (in the above example I only did it for two categories: cat-accumulators and cat-coolers. This would be a very cumbersome way, especially when categories are added. Also it does not work for the main article of the section product-categories which has the category name product-categories.

I also looked up the category lists in the tag reference, but can’t seem to find a way how to loop through the articles depending on the shown page, in order to display only the relevant keywords form that particular article. Do you know a way?

As you have pointed out that I would do the same for description, once it is working.

Again your help is very much appreciated. Thanks to you, I’m not fully convinced as yet to convert all to sections.

Cheers
otti

Offline

#8 2011-12-08 11:55:35

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,001
Website GitHub

Re: Keywords from category articles

What I wanted is only the keywords from each article on that particular page i.e. only the keywords from the article accumulators on the Accumulators page

Hmm, that was what the code in this post was supposed to do as txp:article is context-sensitive, meaning that it takes the articles from the section on the section page and from the category on the category page.

However, your version can be drastically shortened as follows:

<title><txp:if_category><txp:category title="1" /> - </txp:if_category><txp:section title="1" /> | <txp:site_name /></title>
<txp:article_custom section="product-categories" category='<txp:category />' break="">
<meta name="keywords" content="<txp:keywords />" />
<meta name="description" content="<txp:custom_field name="meta_desc" />" />
</txp:article_custom>

Essentially this is the same as the earlier post, but using txp:article_custom instead of txp:article. The attribute category='<txp:category />' plugs the currently showing category into the article filter (for this you need to use single quotes instead of normal double quotes so that it inserts ‘the results of the tag’).


TXP Builders – finely-crafted code, design and txp

Offline

#9 2011-12-08 23:48:12

otti
Member
From: Australia
Registered: 2008-04-16
Posts: 75
Website

Re: Keywords from category articles

Hi Jakob,

It seems that using txp:article_custom instead of txp:article is the solution. Your shortened example works and I only need to allow for non-category pages with a txp:if_category conditional and added another category section I have. Here is my final version:

<txp:if_category>
<title><txp:if_category><txp:category title="1" /> - </txp:if_category><txp:section title="1" /> | <txp:site_name /></title>
<txp:article_custom section="product-categories,services-and-maintenance" category='<txp:category />' break="">
<meta name="keywords" content="<txp:keywords />" />
<meta name="description" content="<txp:custom_field name="meta_desc" />" />
</txp:article_custom>
<txp:else />
<title><txp:section title="1" /> | <txp:site_name /></title>
<meta name="Keywords" content="<txp:keywords />" />
<meta name="Description" content="<txp:custom_field name="meta_desc" />" />
</txp:if_category>

Perfect. I’m not sure though, if I will go down the category path next time again. I think it is just easier to create sections. The onbly problem is that I have to allow the client to create sections too, which leaves more room for problems :-)

Anyway, many thanks again and warm greetibngs to good old Germany.

Otti

Offline

#10 2011-12-09 07:46:52

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,001
Website GitHub

Re: Keywords from category articles

Hi Otti,

Glad I could be of help. In your example code there’s a bit of copy-past doubling left in there … you can EITHER:

  • take out the opening and closing txp:if_category tags surrounding the <txp:category title="1" /> in the first title tag, OR
  • shift the first title tag above the opening txp:if_category tag and remove the second title tag in the txp:else clause entirely.

In terms of organisation, categories are a natural way for grouping collections of articles within a part (=section) of the site and as such are great for product categories when you have a series of articles in each category. In the case of the site you linked to above, it seems that there is just the one article in each category that profiles the kinds of different products on offer but doesn’t go on to list the individual products in those categories. In that case you could simply use a normal article for each product-category and skip the use of textpattern’s categories altogether.


TXP Builders – finely-crafted code, design and txp

Offline

#11 2011-12-09 08:26:23

otti
Member
From: Australia
Registered: 2008-04-16
Posts: 75
Website

Re: Keywords from category articles

Hi Jakob,

I will take you advice in and see if I can shorten the code. I agree that I should have used sections, since there is only one article for each category. But, as usual, it was all planned out differently and then the client changed his mind.

Thanks again and have a great weekend.

Otti

Offline

Board footer

Powered by FluxBB