Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Dynamic drop down list from custom field values
Say that each article has a custom field named place.
I would like textpattern to dynamically create the following:
<option value="New York">New York</option>
<option value="Boston">Boston</option>
<option value="Washington">Washington</option>
etc. etc.
There would be many articles for each city, but each city should only appear once in the list.
I have done something similar for categories:
<txp:category_list parent="places" break="">
<option value="<txp:category />"><txp:category title="1" /></option>
</txp:category_list>
QUESTION: How do I do this?
PS: Wishful thinking
It would be great if a plugin like custom_field_list existed…
• Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
• MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
• JapaneseStreets.com – Japanese street fashion (mostly txp)
Offline
Re: Dynamic drop down list from custom field values
smd_each can help you do this.
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
Re: Dynamic drop down list from custom field values
With core powered article_custom
<txp:article_custom limit="9999" sort="custom_1 asc" place="_%">
<txp:if_different>
<option value="<txp:custom_field name="place" />">
<txp:custom_field name="place" />
</option>
</txp:if_different>
</txp:article_custom>
Or with smd_query
<txp:smd_query query="SELECT custom_1 FROM textpattern WHERE custom_1 != '' GROUP BY custom_1 ORDER BY custom_1 asc">
<option value="{custom_1}">{custom_1}</option>
</txp:smd_query>
In both examples, the custom_1 is the column of your custom field. Top of my head, both untested. Fingers crossed, they may work.
Offline
Re: Dynamic drop down list from custom field values
Sorry, I assumed you had a custom_field with multiple values in each article, like a comma delimited list.
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
Re: Dynamic drop down list from custom field values
Thanks a lot, Gocom and MattD.
Gocom, your solution took care of my issue completely.
For some reason smd_query didn’t work for me, but the core powered article_custom works great. I had tried article_custom before but I wasn’t familiar with if_different
, so I ended up with repeated entries. Cool stuff.
I have added a little lemon to this cocktail:
I pull the custom fields out of my url with adi_gps
. Using if_variable
, the following code now adds selected
to the currently selected option.
<txp:article_custom limit="9999" sort="custom_1 asc" place="_%">
<txp:if_different>
<option <txp:if_variable name='place' value='<txp:custom_field name="place" />'>selected</txp:if_variable> value="<txp:custom_field name="place" />">
<txp:custom_field name="place" />
</option>
</txp:if_different>
</txp:article_custom>
By the way, limit
is set to 9999
. What happens if you have more than 10,000 articles?
• Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
• MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
• JapaneseStreets.com – Japanese street fashion (mostly txp)
Offline
Offline
Re: Dynamic drop down list from custom field values
Gocom wrote:
It won’t go thru all articles. You can set the limit to higher value ofcourse (
99999
). :-)
I thought that was the top limit for some reason… I think a limit of 100,000 should probably cover me for a few weeks. ;-)
Got it, Gocom. Thanks!
• Old Photos of Japan – Japan in the 1850s~1960s (100% txp)
• MeijiShowa – Stock photos of Japan in the 1850s~1960s (100% txp)
• JapaneseStreets.com – Japanese street fashion (mostly txp)
Offline