Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-02-25 09:41:21

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Different info on different articles_id in a sidebar?

Hello,

I am trying figure out the txp logic for showing different info block on different articles of the same section.

For example, in the sidebar I put:

<txp:if_article_id id="6">
Info for article 6
<txp:else />
the rest stuff
<txp:if_article_id>

but How do I put info for several article_ids?

<txp:if_article_id id="6">
Info for article 6
<txp:if_article_id>
<txp:if_article_id id="7">
Info for article 7
<txp:if_article_id>
the rest stuff

In this case the rest stuff will show anyway.

Also in testing mode it says that this tag cannot be used outside the article context or something…

Offline

#2 2014-02-25 10:50:24

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,355
GitHub

Re: Different info on different articles_id in a sidebar?

Custom fields? Perhaps use a custom field per article for the info, have a misc form for ‘the rest stuff’, then use something like this:

<div id="sidebar">
<txp:if_custom_field name="info">
<txp:custom_field name="info" />
<txp:output_form name="rest_stuff" />
</txp:if_custom_field>
</div>

This will check for a custom field named info, and if it’s empty it will output the form rest_stuff.

Offline

#3 2014-02-25 13:09:58

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Re: Different info on different articles_id in a sidebar?

gaekwad wrote #279220:

Custom fields? Perhaps use a custom field per article for the info, have a misc form for ‘the rest stuff’, then use something like this:

<div id="sidebar">...

This will check for a custom field named info, and if it’s empty it will output the form rest_stuff.

Even we talk about several custom_fields, how do i put it?

<div id="sidebar">
<txp:if_custom_field name="info"> 
<txp:custom_field name="info" /> one article id 
another article_id ???
<txp:output_form name="rest_stuff" />
</txp:if_custom_field>
</div>

Offline

#4 2014-02-25 13:22:30

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,355
GitHub

Re: Different info on different articles_id in a sidebar?

Custom fields names are the same across all articles, but the content of the custom field is set on each article individually. You can change the content of the custom field for each article.

Are you able to post some code that you are already using for your site, or perhaps post a link to it?

Offline

#5 2014-02-25 14:10:35

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Different info on different articles_id in a sidebar?

admi wrote #279213:
<txp:if_article_id id="6">
Info for article 6
<txp:if_article_id>
<txp:if_article_id id="7">
Info for article 7
<txp:if_article_id>

I assume the if_individual_article tag, here used without any ID, is the way to go. It always acts on the current article or, used with an else tag, on its relative counterparts like in example 2 of the tag’s page.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#6 2014-02-25 14:24:23

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Re: Different info on different articles_id in a sidebar?

gaekwad wrote #279227:

Custom fields names are the same across all articles, but the content of the custom field is set on each article individually. You can change the content of the custom field for each article.

Are you able to post some code that you are already using for your site, or perhaps post a link to it?

Ok, how do I use custom_fields for many articles like in the example below? Still dont understand, to me using article_id seems simpler…

<div id="sidebar">
<txp:if_article_id id="1">
  London Office Description and pics
</txp:if_article_id>
<txp:if_article_id id="2">
  Paris Office Description and pics
</txp:if_article_id>
<txp:if_article_id id="3">
  Tokio Office Description and pics
</txp:if_article_id>
<txp:else />
Some other stuff
</div>

I just dont know the right wrapping.

Offline

#7 2014-02-25 14:25:34

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Re: Different info on different articles_id in a sidebar?

uli wrote #279232:

admi wrote #279213:

<txp:if_article_id id="6">...

I assume the if_individual_article tag, here used without any ID, is the way to go. It always acts on the current article or, used with an else tag, on its relative counterparts like in example 2 of the tag’s page.

Ok, i will try that. I did not see your post when posting mine previous.

Offline

#8 2014-02-25 14:32:08

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Different info on different articles_id in a sidebar?

admi wrote #279235:

how do I use custom_fields for many articles like in the example below?

If there wasn’t the possibility of omitting the ID attribute, you could fake it like that:

<txp:if_article_id id='<txp:article_id />'>

(OK, don’t do it, it’s just an example for the general mechanics in TXP.)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#9 2014-02-25 14:52:55

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Re: Different info on different articles_id in a sidebar?

uli wrote #279236:

If there wasn’t the possibility of omitting the ID attribute, you could fake it like that:

<txp:if_article_id id='<txp:article_id />'>

(OK, don’t do it, it’s just an example for the general mechanics in TXP.)

As a matter of fact I need the id.

If I put

<txp:if_article_id id="1"> 1 </txp:if_article_id>
<txp:if_article_id id="2"> 2 </txp:if_article_id>

it works ok, but i dont know how to put the code for the rest of pages

Offline

#10 2014-02-25 15:24:09

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Re: Different info on different articles_id in a sidebar?

Ok, I solved it, may be, in a little primitive way:

I put thus:

<div id="sidebar">
<txp:if_article_id id="1"> 1 </txp:if_article_id>  for article 1
<txp:if_article_id id="2"> 2 </txp:if_article_id>  for article 2
<txp:if_article_id id='<txp:if_article_id />'> 3 </txp:if_article_id>  for the rest of ind. articles
<txp:if_article_list> 3 </txp:if_article_list> for article lists pages
</div>

Needless to say, in testing mode it give a warning as to using <txp:article_id /> on article list pages, in working mode it is Ok.

Offline

#11 2014-02-25 15:25:50

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,776
Website GitHub

Re: Different info on different articles_id in a sidebar?

admi wrote #279213:

How do I put info for several article_ids?

I’m a little confused here. Are your visitors viewing a single article (e.g. site.com/some-section/your-article) and you want to display the same sidebar content for all articles in this section? So if they navigate to another article in the same section, they see the same sidebar? Your example in your opening post seems to be slightly different to the one later on, though I may have misunderstood.

If so, your best bet might be <txp:article_custom>. You can grab articles from any section, category or by ID and do something inside the tag’s container for each one. So, for example, if you had a section called ‘offices’ which contained three articles “Paris”, “London” and “Tokyo” you could put in your Page / Form template for some-section, for example:

<txp:article_custom section="offices" wraptag="div" class="infoblock">
   <txp:title />
   <txp:body />
   <txp:if_custom_field name="phone_number">
      Phone: <txp:custom_field name="phone_number" />
   </txp:if_custom_field>
</txp:article_custom>

That would display each office article wrapped in a <div class="infoblock">, showing the office’s title, the body of the article (which could describe the office or have a description of its location or something), and if you defined a custom field in Admin->Advanced Prefs called phone_number and put the contact number for each office then it would show that too. You could add custom fields for more stuff, like map latitude/longitude and use the Google Maps API via javascript to pop up a map of the office.

If you prefer to do it by ID, you can use the id="6,9,15, ..." attribute to txp:custom_field txp:article_custom.

If you mean some other system for the sidebar, e.g. something linked to the currently viewed article then that’s a different requirement and gaekwad’s suggestion about using custom fields is probably the way to go, or as you say, conditionally checking the current article ID. In which case, you need to make sure your tags are ‘inside’ your txp:article or txp:article_custom tags to prevent the warning.

EDIT: ah, hang on. No, I see now that you want to display different content depending on the article. In that case, yes, gaekwad and sprinkworks are offering the right advice and you can ignore my rambling above. If you want to get clever, you can start messing with using txp:variables to hold stuff, but I’ll save that example for if you find that the custom fields solution doesn’t meet your needs.

Last edited by Bloke (2014-02-25 15:32:16)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#12 2014-02-25 15:27:25

springworks
Member
Registered: 2005-01-06
Posts: 172
Website

Re: Different info on different articles_id in a sidebar?

You seem to be trying to store article specific data into the templates, e.g.

<div id="sidebar">
<txp:if_article_id id="1">
  London Office Description and pics
</txp:if_article_id>
<txp:if_article_id id="2">
  Paris Office Description and pics
</txp:if_article_id>
<txp:if_article_id id="3">
  Tokio Office Description and pics
</txp:if_article_id>
<txp:else />
Some other stuff
</div>

What Pete (gakewad) is saying is that this information probably ought to be saved in the database in a custom field, with different contents of that custom field for each relevant article.

So, for example, you could store the office description in custom field 1, and IDs of the office pics in custom field 2.

Then, you can use Pete’s code to output the contents of the custom field, which will depend on the current article being viewed.

This is all about properly separating the content of the site from the templates, which should help to keep the templates cleaner and should make updating the information much easier than if it is mixed up with the HTML code in the templates.

Edit: Stef beat me to it!

Last edited by springworks (2014-02-25 15:28:23)

Offline

Board footer

Powered by FluxBB