Custom global variable not working in function

Question

I’ve been stumped on this for a while and cannot for the life of me figure out why my function isn’t able to reference my global variable.

if ( is_a($query_obj, 'WP_Term') ) {
    $my_base_url = get_term_link($query_obj);
} elseif ( is_a($query_obj, 'WP_Post') ) {
    $my_base_url = get_permalink();
} else {
    $my_base_url = null;
}

function getDropdown($args, $items, $all=false) {
    global $my_base_url;

    if (isset($_GET[$args])) {
         $check = $_GET[$args];
    } else {
        $check = null;
    }
    $output = '<ul class="dropdown ' . $args . '">';
    if ($all == true) {
        $output .= '<li class="' . ($check == null ? 'active' : null) . '"><a href="' . remove_query_arg( $args, add_query_arg( null, null ) ) . '">All</a></li>';
    }
    foreach ($items as $key => $value) {
        $output .= '<li' . ($check == $value ? ' class="active"' : null) .'><a href="' . esc_url( add_query_arg( $args, $value ) ) . '">' . $key . '</a></li>';
    }
    $output .= '</ul>';
    return $output . '<p>' . $my_base_url . '</p>';
}

The return at the bottom just has a paragraph tacked out to return the variable but it shows up blank. The only way I’ve been able to use the base URL was by using it as a function argument and passing it that way. What am I missing?

0
tylorreimer 8 months 2020-11-28T11:10:24-05:00 0 Answers 8 views 0

Leave an answer

Browse
Browse