首页 文章

创建自定义funcitonin wordpress以从数据库返回数据

提问于
浏览
0

我为wordpress管理面板创建了一个自定义图像上传器,需要从wp_options表中检索数据 . 我有以下功能:

//function to get all slider images
function getSliderImages(){
    global $wpdb, $theme_shortname;
    $query = "SELECT * FROM $wpdb->options AS o1 
    WHERE o1.option_name LIKE '%".$theme_shortname."_header_image%'";
    $imgs = $wpdb->get_results($query);

    $images = array();
    //loop through images and remove unusable results
    foreach($imgs as $i){
        $id = substr($i['option_name'],0,-1);
        if(is_numeric($id)){
            $images[] = $i['option_value'];
        }
    }

    return($images);
}

如何在前端的header.php中访问返回的数组?这个函数目前在themes / themename / functions.php中

1 回答

  • 1

    您正在声明一个可在所有模板文件中使用的全局函数 . 您可以在任何模板中使用 <?php $images = getSliderImages(); ?> .

相关问题