Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2012-07-12 06:02:19
- Quimbly
- Member
- Registered: 2012-07-12
- Posts: 22
Not able to display Custom Field -- Need help, Please!
My goal is this: I want to add an extra <style> tag, with values dependent on a custom field for specific articles.
I basically followed the instructions on this page: http://textpattern.com/faq/111/how-do-i-use-custom-fields
I created a custom field with this name and saved it: custom_background
I set the value of this custom field on my target article, and gave it a value of: image2.jpg
Finally, on my presentation default page, just before the end </head> tag, I added this code:
<txp:if_custom_field name=“custom_background”>
<style type=“text/css”>body { background: url(images/<txp:custom_field name=“custom_background”/>) no-repeat !important; }</style>
</txp:if_custom_field>
I want to be able to override the background image on different article pages based on the value I set in the custom field. Different article, different background image.
Unfortunately, this isn’t working. Nothing is being displayed, so the if_custom_field isn’t triggering for this article page when I browse to it.
I tried a different approach, just to see if I could get the custom field to display:
<!— TEST —>
<style type=“text/css”>body{ background: url(images/<txp:custom_field name=“custom_background”/>) no-repeat !important; } </style>
<!— END TEST —>
The output is:
<!— TEST —>
<style type=“text/css”>body{ background: url(images/) no-repeat !important; }</style>
<!— END TEST —>
Notice the lack of value where the custom_background field should be.
Any suggestions on what I’m doing wrong and how to get this working would be greatly appreciated.
Last edited by Quimbly (2012-07-12 06:06:51)
Offline
Re: Not able to display Custom Field -- Need help, Please!
Custom fields work in an article and article_custom context
Try
<txp:article_custom id='<txp:article_id />'>
<txp:if_custom_field name="custom_background">
<style type="text/css">body { background: url(/images/<txp:custom_field name="custom_background"/>) no-repeat !important; }</style>
</txp:if_custom_field>
</txp:article_custom>
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
#3 2012-07-12 06:44:33
- Quimbly
- Member
- Registered: 2012-07-12
- Posts: 22
Re: Not able to display Custom Field -- Need help, Please!
Thank you for the reply, colak!
Interesting… Your code displays the value of the custom field (yay!), but now it is triggering on every page, not just on the article page I want. Maybe I’m misunderstanding custom fields. What I wanted was to be able to set the custom field on only 1 or 2 articles, and have the the custom_background field empty on all other pages of the site — thus keeping the background image the default for all pages except the 1 or 2 article pages.
The custom field seems to be acting global, yet each article has its own custom_background field and value. What am I missing?
Offline
Re: Not able to display Custom Field -- Need help, Please!
As colak says, you have to be in an article context. So => test if this is actually true :
If you are sure the article is pulled by a txp:article
:
<txp:if_individual_article>
<txp:if_custom_field name="custom_background">
<style type="text/css">body { background: url(images/<txp:custom_field name="custom_background"/>) no-repeat !important; }</style>
</txp:if_custom_field>
</txp:if_individual_article>
If it is pulled by a txp:article_custom
, you can replace if_individual_article
with if_article_list
but you have to be sure that there is always one, and only one, article in the list. By always, I mean either in a txp:article
or in a txp:article_custom
generated content.
Offline
Re: Not able to display Custom Field -- Need help, Please!
i once had a similar problem and solved it this way:
<txp:if_individual_article>
<body id="<txp:section />" style="background-image:url(<txp:image_url id='<txp:custom_field name="background-image" default="no_id" />' />);">
<txp:else />
<body id="<txp:section />">
</txp:if_individual_article>
however, in this case the background change was only needed in individual article mode. so i was always in article context. you can see it working here
Offline