0

I'm having trouble printing a variable in my template of the body. The variable is set in the header.php and the page loads both header.php and template.php. This is the code in the header.php with the variable:

<?php 
    // Post is PAGE
    $post = get_post();
    $page_id = get_the_ID($post);
    // echo $page_id." - ";
    $page_parent_id = wp_get_post_parent_id($page_id);
    if($page_parent_id==0) { $page_parent_id = $page_id; }
    switch($page_parent_id) {
        case 24:
            $logo_url = "/wp-content/uploads/2019/03/logo-lestradeditorino.png";
            $home_url = "/torino";
        break;
        case 27:
            $logo_url = "/wp-content/uploads/2019/03/logo-lestradedimilano.png";
            $home_url = "/milano";
        break;
        case 29:
            $logo_url = "/wp-content/uploads/2019/03/logo-lestradedigenova.png";
            $home_url = "/genova";
        break;
        case 34:
            $logo_url = "/wp-content/uploads/2019/03/logo-lestradediroma.png";
            $home_url = "/roma";
        break;
        case 42030:
            $logo_url = "/wp-content/uploads/2022/07/logo-lestradedinapoli.png";
            $home_url = "/napoli";
        break;
        case 50629:
            $logo_url = "/wp-content/uploads/2022/07/logo-lestradediparma.png";
            $home_url = "/parma";
        break;
        case 52120:
            $logo_url = "/wp-content/uploads/2022/07/logo-lestradediverona.png";
            $home_url = "/verona";
        break;
        default:
            $logo_url = esc_url($theme_settings['logo']);
            $home_url = "/";
    }

    // Post is POST of CATEGORY
    $post_parent = array();
    $category = get_the_category();

    // FIX - TANDU - BEGIN
    if ( ! empty( $category ) ) { // New
        if ( count($category) > 1 ) {
            $post_parent = get_ancestors($category[0]->term_id,'category');
            $post_parent = array_reverse($post_parent);
        } else {
            $post_parent[0] = $category[0]->cat_ID;
        }
    } // New 
    // FIX - TANDU - END
    $post_parent = array_reverse($post_parent);
    // print_r($post_parent);

    switch($post_parent[0]) {
        case 4:
            $logo_url = "/wp-content/uploads/2019/03/logo-lestradeditorino.png";
            $home_url = "/torino";
        break;
        case 12:
            $logo_url = "/wp-content/uploads/2019/03/logo-lestradedimilano.png";
            $home_url = "/milano";
        break;
        case 18:
            $logo_url = "/wp-content/uploads/2019/03/logo-lestradedigenova.png";
            $home_url = "/genova";
        break;
        case 222:
            $logo_url = "/wp-content/uploads/2019/03/logo-lestradediroma.png";
            $home_url = "/roma";
        break;
        case 419:
            $logo_url = "/wp-content/uploads/2022/07/logo-lestradediparma.png";
            $home_url = "/parma";
        break;
        case 386:
            $logo_url = "/wp-content/uploads/2022/07/logo-lestradediverona.png";
            $home_url = "/verona";
        break;
        case 385:
            $logo_url = "/wp-content/uploads/2022/07/logo-lestradedinapoli.png";
            $home_url = "/napoli";
        break;
    }
?>

In the template.php this is my code:

<?php echo $home_url; ?>

I need the $home_url variable to be printable in the any template of my site. How can i do it?

I tried to print the variable and other variables from my header in my body template but doesn't work.

  • 1
    Does this answer your question? [How to declare a global variable in php?](https://stackoverflow.com/questions/13530465/how-to-declare-a-global-variable-in-php) – maio290 Apr 06 '23 at 14:29
  • @maio290 There's no function in this code, all the variables are global. – Barmar Apr 06 '23 at 15:31
  • How are you loading `template.php`? If you use `include('template.php')` then it should be able to access the variable. – Barmar Apr 06 '23 at 15:32
  • @Barmar i'm working on wordpress, so I choose my template from the template selector for my page and inside the code of template.php I'm loading the header with get_header(); – Mirko Mina Apr 07 '23 at 13:15
  • Even in the WordPress world, magic variables are often considered dangerous because someone else can stomp that at any given moment. Instead, I'd steer you towards creating a function that gets what you want. Here's a sample of that: https://3v4l.org/XOAa7#v8.1.17 – Chris Haas Apr 07 '23 at 14:37

0 Answers0