illdefinedartistry Posted July 17, 2015 Share Posted July 17, 2015 (edited) Hi guys, What I'm trying to do is convert an array that is currently hard coded, into one that is algorithmic and will dynamically change based on the number of pages on my site with the specified template. The array is a list of options that are to appear in a drop down menu. Hard code where 'Portraits' => __( 'Portraits', 'quemalabs_admin') is output => __(option title, function) : 'options' => array( 'Portraits' => __( 'Portraits', 'quemalabs_admin'), 'Concerts' => __( 'Concerts', 'quemalabs_admin' ), ) Here is what I have as the algorithmic code. This code currently outputs the correct number of options, however all of the titles display as "Array" $pages = get_pages(array( 'meta_key' => '_wp_page_template', 'meta_value' => 'page-home.php' )); foreach($pages as $page){ $keys[] = $page->post_title.'<br />'; } $objects = array_fill_keys($keys, 'quemalabs_admin'); $options = array_fill_keys($keys, $objects); ... 'options' => $options Edited July 17, 2015 by illdefinedartistry Quote Link to comment https://forums.phpfreaks.com/topic/297340-convert-from-hard-coding-to-algorithmic-coding/ Share on other sites More sharing options...
requinix Posted July 17, 2015 Share Posted July 17, 2015 (edited) That means the $page->post_title is an array (I think that's what's going on). So what's in the array? Use something like print_r() or var_dump() or even json_encode() to see, if you're not sure. Edited July 17, 2015 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/297340-convert-from-hard-coding-to-algorithmic-coding/#findComment-1516661 Share on other sites More sharing options...
maxxd Posted July 17, 2015 Share Posted July 17, 2015 Try removing the second array_fill_keys() and adjusting to 'options' => $objects. I don't have time to test, but it looks to me like you're putting the array $objects into each index of $options. Quote Link to comment https://forums.phpfreaks.com/topic/297340-convert-from-hard-coding-to-algorithmic-coding/#findComment-1516662 Share on other sites More sharing options...
illdefinedartistry Posted July 18, 2015 Author Share Posted July 18, 2015 Testing with 'options' => $objects. Fills each drop down with queenlabs_admin as each option When I tested with 'options' => $keys it shows up correctly in the drop down, however it does not return the correct results. The exact comments regarding the function are: // Array of 'value' => 'Label' pairs for select box Quote Link to comment https://forums.phpfreaks.com/topic/297340-convert-from-hard-coding-to-algorithmic-coding/#findComment-1516679 Share on other sites More sharing options...
illdefinedartistry Posted July 18, 2015 Author Share Posted July 18, 2015 That means the $page->post_title is an array (I think that's what's going on). So what's in the array? Use something like print_r() or var_dump() or even json_encode() to see, if you're not sure. $page->post_title returns a string which contains the wordpress post's title. Quote Link to comment https://forums.phpfreaks.com/topic/297340-convert-from-hard-coding-to-algorithmic-coding/#findComment-1516680 Share on other sites More sharing options...
maxxd Posted July 19, 2015 Share Posted July 19, 2015 Try setting the $keys array index to the post ID? I'm honestly not entirely sure what you're hoping to see as the output. Right now, the $keys array is going to be a numerically-indexed array of page slugs, so the select options are probably going to look a lot like this: <select value='1'>page_slug_1</select> <select value='2'>page_slug_47</select> <select value='3'>page_slug_15</select> etc.. If you want to associate the page with it's slug, create the array like this: foreach($pages as $page){ $keys[$page->ID] = $page->post_title; } then use $keys as the value of 'options'. I'm not familiar with the Quemalabs theme, so I don't know exactly how you're creating the select box, but from what you've said I think this'll work for you. Quote Link to comment https://forums.phpfreaks.com/topic/297340-convert-from-hard-coding-to-algorithmic-coding/#findComment-1516770 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.