Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to add a drop down list of all posts into a theme admin section...without forklifting the theme code.

I need to pass the post title and id into the 'options'=>array() like so:

'getting-started-link-id'=>array(
    'title'=>'LINK GETTING STARTED TO:',
    'name'=>'page-option-item-getting-started-link-id',
    'options'=>array('54'=>'Post #1','23'=>'Post #2', '654'=>'Post #3',)
    'type'=>'combobox'
),   

I wrote a function that takes all the posts ID and name and turns them into a string:

function post_page_options(){
    $post_page_options = get_posts('category=orderby=title&order=asc&numberposts=');
    foreach( $post_page_options as $value ) :
        $str .= "'$value->ID'" . "=>'" . $value->post_title . "', ";
    endforeach; 
    return $str;
}

$str returns this:
'54'=>'Post #1','23'=>'Post #2', '654'=>'Post #3',

I tried this but it does not work:

    'options'=>array( post_page_options() )

I tried turning the $str into an array() but the the array does not come out looking like the the format as in $str.

Somehow adding the results of the $str into the 'options'=> seems like the easist approach so the theme admin code will do what it is already written to do which is take the 'options'=> array and turn it into a drop down list.

share|improve this question
    
My initial thought is that you look into serialize and unserialize as that is how arrays must be stored/retrieved in the database. –  GhostToast Jun 3 '13 at 18:28
add comment

1 Answer

up vote 1 down vote accepted

This...

array('54'=>'Post #1','23'=>'Post #2', '654'=>'Post #3',)

... is not a string. It is an array definition.

All you need to do is create an array, which is what you want, and skip the string completely.

function post_page_options(){
    $post_page_options = get_posts('category=orderby=title&order=asc&numberposts=');
    foreach( $post_page_options as $value ) :
        $str[$value->ID] = $value->post_title;
    endforeach; 
    return $str;
}
share|improve this answer
    
That was spot on and worked. I tried that first but could not get the arraignment right. Thank you so much. –  Jason Jun 3 '13 at 18:47
    
Well one slight problem...I get this: <option rel="Module 1 - Getting Started">Module 1 - Getting Started</option> and I need <option rel="34">Module 1 - Getting Started</option> where rel="34" is the post id. –  Jason Jun 3 '13 at 19:21
    
The code that uses the array is using the value for both. To use an array like you want it needs to be using the key for one and the value for another. –  s_ha_dum Jun 3 '13 at 19:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.