Jump to content

Convert from Hard-coding to Algorithmic Coding


Recommended Posts

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 by illdefinedartistry
Link to comment
Share on other sites

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 by requinix
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.