試圖編寫一段代碼來擴展我在Wordpress中使用的主題。獲取自定義文章並放入選項數組 - wordpress插件
基本上,我想獲得所有自定義帖子類型,並把它放入一個選擇數組 - 我遇到的問題是,我需要在數組中添加選項值,我不能把foreach循環放在數組,所以不知道如何做到這一點。
在下面的代碼,你會看到代碼:
'options' => array(),
這就是自定義信息必須在格式:
'PostID' => esc_html__('Post Name', 'builder'),
這裏是我的代碼:
function get_fields() {
$fields = array(
'get_post_names' => array(
'label' => esc_html__('Url Opens', 'builder'),
'type' => 'select',
'option_category' => 'configuration',
'options' => array(),
'toggle_slug' => 'post_names',
'description' => esc_html__('Here you can choose whether or not your link opens in a new window', 'et_builder'),
),
);
global $wpdb;
$custom_post_type = 'custom_post_name';
$results = $wpdb->get_results($wpdb->prepare("SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s and post_status = 'publish'", $custom_post_type), ARRAY_A);
if (! $results)
return;
foreach($results as $index => $post) {
$fields['options'][] = array (
$post['ID'] => esc_html__($post['post_title'], 'builder'),
);
}
return $fields;
}
任何幫助將不勝感激。
感謝
對不起,沒有幫助,因爲不在陣列中 –