WordPress.org

Ready to get started?Download WordPress

Forums

Quick Theme / Function question (2 posts)

  1. iperez_genius
    Member
    Posted 1 year ago #

    I am trying to create a global variable that can be access throughout the entire theme, ie the header, footer and sidebar, single and page .php files.

    I have added the code block below to my function.php file

    function determine_thing($post){
    
    		global $post;
    		global $thingType;
    		$thingType = "";
    		$id = $post->ID;
    		$thingType = get_post_meta($id,'thingtype',true);
    		$category = get_the_category($id); 
    
    	    switch ($category[0]->slug)
    		{
    			case "thing":
    			$thingType = "a";
    			break;
    
    			case "thing2":
    			$thingType = "b";
    			break;
    		default:
    			$thingType = "c";
    			break;
    		}
    
    return $thingType;
    
    }
    add_action('the_thing', 'determine_thing');

    I believe the registration of this function was successful but i am at a loss of how to call it? I have tried the code block below and its not working. Also will this actually achieve what i want?

    global $post;
    $thingType = do_action('the_thing', $post);

    thanks in advance

    Ilan

  2. datdesignguy
    Member
    Posted 1 year ago #

    Your function doesn't ACTUALLY require any arguments since you're already pulling in the $post variable by setting global $post in your function.

    So I'm wondering if perhaps it would make more sense for you to attach your determine_thing function to a standard WP action such as wp or wp_head.

    Your function doesn't need to return anything either if you take this approach, as you've already set the $thingType variable to global as well.

    I hope this gives you some ideas. Be sure and let us know what solves your problem!

    -greg

Topic Closed

This topic has been closed to new replies.

About this Topic