Tagged: select
- This topic has 9 replies, 4 voices, and was last updated 7 years, 2 months ago by
igor.
-
AuthorPosts
-
-
November 30, 2014 at 6:14 pm #2892
igorMemberI need to create select with choices of posts, pages, etc
I know such code
,'choices' => piklist( get_posts( array( 'post_type' => 'post' ,'orderby' => 'post_date' ) ,'objects' ) ,array( 'ID' ,'post_title' ) )It’s working. But one small problem. First selected by default choice is a first page\post. I need empty first choice or maybe something with value = “empty”, name = “Choose your page”
How to do this?
-
November 30, 2014 at 10:10 pm #2893
SteveKeymaster@igor– Welcome to the Piklist community!
The code you’re using to create the choices just takes the data from
get_posts()and formats it correctly for Piklist, so it looks like this:,'choices' => array( 'key' => 'value' ,'key'=> 'value' ,'key' => 'value' )
To add some instructions as the first choice you can us the standard PHP Union operator, a plus sign
+. So your newchoicesparameter would look like this:,'choices' => array('none' => 'Choose your page') + piklist( get_posts( array( 'post_type' => 'post' ,'orderby' => 'post_date' ) ,'objects' ) ,array( 'ID' ,'post_title' ) )Let us know if you need any more help.
-
December 1, 2014 at 10:06 am #2894
igorMemberYou have awesome support and the best wordpress framework.
-
December 1, 2014 at 11:00 am #2895
SteveKeymaster@igor– Thank you so much! Glad we can help.
If you have the time, we would appreciate it if you could write the same thing at WordPress.org >
-
December 1, 2014 at 11:46 pm #2900
countryipblocksMember@Steve,
I’m trying this as well to add instructions as the first in select menu. What am I doing wrong that I get this error?
Parse error: syntax error, unexpected T_DOUBLE_ARROW in /home/nickel/public_html/wp-content/plugins/wpcountryipsecurity/parts/settings/countryipsecurity-countries.php on line 38
Here’s my code
piklist('field', array( 'type' => 'group' ,'field' => 'continent_north_america' ,'add_more' => true ,'label' => 'North America' ,'description' => 'Description' ,'fields' => array( array( 'type' => 'select' ,'field' => 'continent_north_america' ,'label' => 'North America' ,'columns' => 6 ,'attributes' => array( 'class' => 'text') ,'choices' => array('none' => 'Choose your country') + piklist( 'ai' => 'Anguilla' ,'ag' => 'Antigua and Barbuda' ,'bs' => 'Bahamas' ,'bb' => 'Barbados' ,'bz' => 'Belize' ,'bm' => 'Bermuda' ,'vg' => 'British Virgin Islands' ,'ca' => 'Canada' ,'ky' => 'Cayman Islands' ,'cr' => 'Costa Rica' ,'cu' => 'Cuba' ,'dm' => 'Dominica' ,'do' => 'Dominican Republic' ,'sv' => 'El Salvador' ,'gl' => 'Greenland' ,'gd' => 'Grenada and Carriacuou' ,'gp' => 'Guadeloupe' ,'gt' => 'Guatemala' ,'ht' => 'Haiti' ,'hn' => 'Honduras' ,'jm' => 'Jamaica' ,'mq' => 'Martinique' ,'mx' => 'Mexico' ,'pm' => 'Miquelon' ,'ms' => 'Montserrat' ,'cw' => 'Netherlands Antilles' ,'kn' => 'Nevis' ,'ni' => 'Nicaragua' ,'pa' => 'Panama' ,'pr' => 'Puerto Rico' ,'kn' => 'Saint Kitts and Nevis' ,'lc' => 'Saint Lucia' ,'vc' => 'Saint Vincent and the Grenadines' ,'tt' => 'Trinidad and Tobago' ,'tc' => 'Turks and Caicos Islands' ,'vi' => 'US Virgin Islands' ,'us' => 'United States of America' ,'um' => 'US Minor Outlying Islands' ,'sx' => 'Sint Maarten' ,'bq' => 'Bonaire' ,'sa' => 'Saba' ,'se' => 'Sint Eustatius' ) ) ) ,'validate' => array( array( 'type' => 'limit' ,'options' => array( 'min' => 1 ,'max' => 42 ) ) ) )); -
December 2, 2014 at 11:24 am #2906
SteveKeymasterYou only need to use the Piklist function if you are going to parse an array of data. Since you are manually creating the choiçes array (list), you can just including it in the array:
,'choices' => array( 'none' => 'Choose your country' ,'ai' => 'Anguilla' ,'ag' => 'Antigua and Barbuda'
Does that make sense?
-
December 2, 2014 at 3:06 pm #2910
igorMemberAdded review on wordpress.
Next question on answer.
In many others frameworks I see select 2 js script in select inputs. This is very usefull when you have many items. For example, if I have 200 posts, it’s very hard to use simple select.
Is it possible to add this script to selects on page? I see that piklist has some filters, but don’t see how to add scripts on page with piklist
-
December 3, 2014 at 11:06 am #2922
JasonKeymasterHi igor!
Rather than changing subject in a topic, I suggest making a new topic to help make your question easy for others to find in the future. 🙂
But you can do this easily! I do it all the time! What you’re going to want to do is enqueue the Select2 script into the admin side only. Everything on how to do that can be found here: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
And you can use a CDN url for Select2 instead of downloading the script and adding it to your theme/plugin.
Hope this helps! 🙂
-
-
December 2, 2014 at 3:08 pm #2911
igorMember -
December 3, 2014 at 2:21 pm #2936
igorMemberThank you, Jason. I forgot about this wordpress action
-
-
AuthorPosts
- You must be logged in to reply to this topic.