Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
How to group articles by custom field
I have read the FAQs and numerous thread here, which skirt around my problem but do not address it directly.
I have a particular section of articles that are labeled using a custom field called “colour”. Some articles are “red”, some are “blue”, “green”, etc. So I’ve filled in the custom field appropriately. When a reader goes to the main page for this section I want them to see a description of each colour, after which there will be a “read more” link that will then list excerpts of the articles for that colour. And if they drill down further they will get the individual article pages.
So basically, this is changing the usual two-level hierarchy and adding a third at the top. I need to group article for display.
I have implemented this by having a sticky article for each colour, with the custom field set appropriately. The form that dispatches contains the following:
<txp:if_section name="colour">
<txp:article form="colour-headings" section="colour" status="sticky" />
</txp:if_section>
Plus other stuff for other sections.
I’ve also created the “colour-headings” form to display the sticky articles. This works fine. Now, after each colour description I need to provide a link to a page that will show just the appropriate articles for that colour. It is easy enough to display these:
<txp:article form="colour-details" section="colour" status="live" limit="4" pageby="4" />
But how the heck do I form the correct link?
robin
Offline
Re: How to group articles by custom field
Not sure if I have understood you correctly with the link, so this is a guess.
txp:article_custom accepts the attribute customfield="value"
so if you use <txp:article_custom colour="red" section="colour" ... />
you’ll get a list of articles containing only the colour red.
Another way to get a complete listing of all your articles in the section “colour” arranged by colour is to use txp:if_different as described in this faq article but using the custom field instead of year/month as differentiator.
To link to the respective article, just use txp:permlink
as usual.
If you mean how do you link to a page that lists your articles by a certain colour, use the above in combination with chs_if_urlvar. See example in this thread here. Your links would then be appended with ?colour=red
or similar.
And, last of all, this would all be a bit simpler if you used categories for the colours, but possibly you have used them both already for some other reason. Hope one or some of those help!
TXP Builders – finely-crafted code, design and txp
Offline
Re: How to group articles by custom field
jakob wrote:
If you mean how do you link to a page that lists your articles by a certain colour, use the above in combination with chs_if_urlvar. See example in this thread here. Your links would then be appended with
?colour=red
or similar.
I am trying this out. There’s not much on this plugin and once installed the help doesn’t work, but I hope to piece it together. Thanks for the hint.
robin
Offline
Re: How to group articles by custom field
Um, hold on, chs_urlvar requires that you know all your variable values ahead of time? It looks like it, since you need to explicitly reference them like so:
<txp:chs_urlvar_exists var="colour">
<txp:chs_if_urlvar var="colour" value="red">
<p>red</p>
</txp:chs_if_urlvar>
<txp:chs_if_urlvar var="colour" value="blue">
<p>blue</p>
</txp:chs_if_urlvar>
</txp:chs_urlvar_exists>
That doesn’t work for me. As explained, the variables are going to come from a custom field. The textpattern code will know the variable name (“colour”), but not the values.
Is there any way to get the value from the URL dynamically?
robin
Offline
#5 2008-09-02 13:09:44
- redbot
- Plugin Author
- Registered: 2006-02-14
- Posts: 1,410
Re: How to group articles by custom field
robin746 wrote:
Is there any way to get the value from the URL dynamically?
You have to hardcode your links this way:
<a href="<txp:site_url />colour?colour=blue">blue</a>
and then in the page for section “colour”:
<txp:chs_if_urlvar var="colour" value="blue">
<txp:article_custom colour="blue" />
</txp:chs_if_urlvar>
<txp:chs_if_urlvar var="colour" value="red">
<txp:article_custom colour="red" />
</txp:chs_if_urlvar>
…and so on
By the way, you can use the more advanced smd_if instead of chs_if_urlvar
Offline
Re: How to group articles by custom field
And I do have plans to improve smd_if to enable you to get ‘at’ variables it has plucked off the URL line. Mind you, my plans are all going to pieces at the moment so don’t hold your breath on that.
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
Re: How to group articles by custom field
Hey Stef. Why is it that every three months when I have a question you pop up to help. ;-)
robin
Offline
Re: How to group articles by custom field
When a reader goes to the main page for this section I want them to see a description of each colour, after which there will be a “read more” link that will then list excerpts of the articles for that colour
For the description of each colour (the sticky article?) you just need to add the link as redbot describes to the form that displays those sticky articles.
That doesn’t work for me. As explained, the variables are going to come from a custom field. The textpattern code will know the variable name (“colour”), but not the values.
If you just need a ul-list/select drop-down or whatever with the available colours, use the txp:if_different
method to list the available values for the custom colour field. It’ll show each distinct value for the custom field colour once.
TXP Builders – finely-crafted code, design and txp
Offline
Re: How to group articles by custom field
robin746 wrote:
That doesn’t work for me. As explained, the variables are going to come from a custom field. The textpattern code will know the variable name (“colour”), but not the values.
No problem, and certainly no need to know the values – well the plugin requires it, but in common. Lets begin.
With Textpattern 4.0.7 you can do:
<txp:article_custom colour='<txp:php> echo gps("colour"); </txp:php>' />
And currently the alternatives are:
<txp:php>
echo article_custom(array(
'colour' => gps("colour"),
'form' => 'myform',
'section' => 'colour'
));
</txp:php>
Or, possibly if asy_wondertag’s parser works enough well:
<txp:asy_wondertag><txp:article_custom colour="<txp:php> echo gps('colour'); </txp:php>" /></txp:asy_wondertag>
Those automatically pick the custom field value from the color-get parameter. In example ?colour=red shows reds.
Edit. Englishman turned into u-less American. Here we go, now it’s back in London.
Last edited by Gocom (2008-09-02 14:18:40)
Offline
Re: How to group articles by custom field
Gocom, in common with what you are saying I just got the following link to work fine. This means I can launch pages without explicitly coding in the colour.
<txp:asy_wondertag>
<a href="<txp:site_url />section?colour=<txp:custom_field name="colour" />">read</a>
</txp:asy_wondertag>
However the plugin is failing with this error:
Tag error: <txp:chs_urlvar_exists var="colour"> -> Notice: Undefined index: colour on line 14
The code in the plugin triggering the error is as follows:
function chs_urlvar_exists($atts, $thing) {
$thevar = ($atts["var"]) ? $atts["var"] : "function";
return ($_GET[$thevar]) ? parse($thing) : "";
}
robin
Offline
Re: How to group articles by custom field
I confirmed that this works:
<txp:php>
echo gps("colour");
</txp:php>
So I changed the following plugin function (which was not working before) to read:
function chs_urlvar_echo($atts) {
$result = ($atts["var"]) ? gps($atts["var"]) : "";
return $result;
}
Now the following works nicely:
<txp:chs_urlvar_echo var="colour" />
Next thing: fix chs_urlvar_exists().
robin
Offline
Re: How to group articles by custom field
This pair of functions seem to work.
function chs_urlvar_exists($atts, $thing) {
$thevar = ($atts["var"]) ? $atts["var"] : "function";
return (gps($thevar)) ? parse($thing) : "";
}
function chs_urlvar_notexists($atts, $thing) {
$thevar = ($atts["var"]) ? $atts["var"] : "function";
return (gps($thevar)) ? "" : parse($thing);
}
Now that I’ve finished blindly debugging the plugin, I should figure out how to make my programme work. There are still some structural problems.
robin
Offline