Tagged: 

Viewing 8 reply threads
  • Author
    Posts
    • #2892
      igor
      Member

      I 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?

    • #2893
      Steve
      Keymaster

      @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 new choices parameter 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.

    • #2894
      igor
      Member

      You have awesome support and the best wordpress framework.

    • #2895
      Steve
      Keymaster

      @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 >

    • #2900

      @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
              )
            )
          )
        ));
    • #2906
      Steve
      Keymaster

      @countryipblocks

      You 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?

    • #2910
      igor
      Member

      Added 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

      • #2922
        Jason
        Keymaster

        Hi 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! 🙂

    • #2911
      igor
      Member
    • #2936
      igor
      Member

      Thank you, Jason. I forgot about this wordpress action

Viewing 8 reply threads
  • You must be logged in to reply to this topic.