Steve Smith
Steve Smith

Reputation: 752

Creating custom funcitonin wordpress to return data from database

I have created a custom image uploader for my wordpress admin panel and need to retrieve the data from the wp_options table. I have wrritne the below function:

//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);
}

How do I access the returned array in header.php on the front end? this function is currently in themes/themename/functions.php

Upvotes: 0

Views: 80

Answers (1)

Rob
Rob

Reputation: 12872

You are declaring a global function that is available within all of your template files. You can simply use <?php $images = getSliderImages(); ?> in any of your templates.

Upvotes: 1

Related Questions