Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Article Specific Sidebar Content | Best Practice?
Hello All,
On several sites I’ve built recently I’ve needed to allow the user to choose from a list of sidebar content on a per article basis.
These sidebars are basically just other small articles that the user can add themselves and assign to the section sidebars
.
My current solution is to define a custom field called article-sidebars
, then have the user put a comma separated list of ID’s in there.
This solution has really only been made viable on a larger scale because adi_matrix now makes it possible to change these ID’s easy in multiple articles. But I digress…
To call these articles I summon the mighty smd_each via this handy snippet:
<txp:if_custom_field name="article-sidebars">
<txp:smd_each type="field" include="article-sidebars" subset="2" collate="1">
<txp:article_custom form="sidebar" id="{smd_var_value}" sort="FIELD(ID,{smd_var_value})" />
</txp:smd_each>
</txp:if_custom_field>
This all works perfectly fine, but somehow still feels like a “hackish” solution.
Partially because — unlike images and files — there’s not a method to select the sidebar articles without memorizing the ID’s.
OK, Get to the point Reno.
I’m posting this because:
1. I’d like to share this technique in case it’s useful to anyone else.
2. If there are other suggestions for accomplishing this I’d love to hear them.
3. If this method is the best it’s gonna get, then I would love a plugin that allows me to “pick” associated articles (like jmd_img_selector for images, or upm_file_popper for files), and I’m willing to throw some cash towards the right solution.
OK, now it’s your turn — fire at will.
Offline
Re: Article Specific Sidebar Content | Best Practice?
renobird wrote:
Hello All,
Hello Tom :)
Partially because — unlike images and files — there’s not a method to select the sidebar articles without memorizing the ID’s.
glz_custom_fields offers option for custom PHP scripts (plugins could be a better word) which can be used to populate custom fields. As such, glz_custom_fields with a custom PHP script could be used to generate multi-select field which would allow site authors to select the articles that would then appear in the sidebar.
For example, a script like this could be used with Gerhard’s lovely plugin:
<?php
/**
Lists articles used to populate the sidebar
@param $custom_field string The custom field.
@param $custom_id string The ID of the field.
@param $custom_value string The default/existing value.
*/
function sidebar_articles($custom_field, $custom_id, $custom_value) {
$out = array();
/*
Get the articles from section name "sidebars"
*/
$rs =
safe_rows(
'ID, Title',
'textpattern',
"Section='sidebars' ORDER BY Title asc"
);
/*
End here if nothing was returned
*/
if(!$rs)
return ' ';
foreach($rs as $a)
$out[$a['ID']] = $a['Title'];
/*
Generate multi-select field with
the data we have
*/
return
glz_selectInput(
$custom_field,
$custom_id,
$out,
$custom_value,
"",
1
);
}
?>
To use it, save it to glz_custom_field’s custom scripts directory, named as sidebar_articles.php
(same name as the function name, so that the function gets executed). Requires glz_custom_fields of course, not to mention it’s highly untested. Gotta stay true and not test anything (not really). Then it can be used as the custom-field-type-populator-setting-thing-something via the admin-side interface. Hope the script helps.
On the frontside of things normal article_custom, custom_field tag and rah_replace can be used. Latter allows turning the pipes (|
) to commas (,
). Required when feeding the values to article_custom. That bastard doesn’t take pipes from no one.
Like so:
<txp:if_custom_field name="article-sidebars">
<txp:article_custom id='<txp:rah_replace from="|" to="," delimiter=""><txp:custom_field name="article-sidebars" /></txp:rah_replace>' form="sidebar" />
</txp:if_custom_field>
Or something like that. I think. It should do something at least. My gun has fired (ugh, bad dirty joke is bad). :)
Last edited by Gocom (2011-06-21 04:28:41)
Offline
Re: Article Specific Sidebar Content | Best Practice?
Hi Tom
why not use a custom_field for that purpose?
Juste add a custom_field call it sidebar, and put there “1” if you wante to make it appear in side bar, is that not easy?
Another solution is to use category2 for that purpose: add a cat: sidebar and let people affect the a=desired article to that cat using category2, the advantage of this solution is they can use article admin tab to affect multiple article at once.
Cheers
Online
Re: Article Specific Sidebar Content | Best Practice?
renobird wrote:
I summon the mighty smd_each
I laughed out loud at this. I’m imagining Stef wearing a long robe, holding a big staff and pointing at something mountainous.
(Sorry, that’s not much help, but I had to share)
Last edited by gaekwad (2011-06-21 10:46:30)
Offline
Offline
Re: Article Specific Sidebar Content | Best Practice?
Jukka,
Wow! Thank you for that very detailed response — what you’ve suggested appears to be the holy grail.
I’ve been away from glz_custom_fields
for a while, and clearly it’s made some amazing strides and is more powerful than ever.
That bastard doesn’t take pipes from no one.
Best thing I’ve heard all week — resulted in a real LOL. :P
I’ll report back shortly, thank you my friend.
—
T
Offline
Re: Article Specific Sidebar Content | Best Practice?
renobird wrote:
I’ll report back shortly, thank you my friend.
No problem Tom. You’re welcome :)
Oh, yeah forgot to mention one thing. The custom script will need to be renamed after the custom field. I.e. if the custom field is named “article_sidebars” the file and the function should be named the same. I’m not sure if glz_custom_field removes invalid characters from the function name. If it doesn’t, you will need to change the hyphen to underscore (as hyphens are treated as minus signs, can’t have that).
After you have the script (article_sidebars.php) in the scripts dir, go to the glz_custom_field’s admin panel and create (or modify existing cf) and select “Custom Script” as the type. That’s it. You should now see list of articles populating the custom field.
Last edited by Gocom (2011-06-21 16:21:08)
Offline
Re: Article Specific Sidebar Content | Best Practice?
@Jukka – would you mind creating a TXP Tip for your excellent method?
Offline