• Hello. I’m using a child theme who has 5 boxes with a supposed graphic background. To load that photos first the child makes an array:

    <?php  
          $settings = array(
                              	 
          	// IMAGES
         	'sampledata' => array(
    					  1 => array("name" => "", "img" => home_url()."/wp-content/themes/childtheme/img/cat1.jpg", "icon" => ""), 
    					  2 => array("name" => "", "img" => home_url()."/wp-content/themes/childtheme/img/cat2.jpg", "icon" => ""), 
    					  3 => array("name" => "", "img" => home_url()."/wp-content/themes/childtheme/img/cat3.jpg", "icon" => ""), 
    					  4 => array("name" => "", "img" => home_url()."/wp-content/themes/childtheme/img/cat4.jpg", "icon" => ""), 
    					  5 => array("name" => "", "img" => home_url()."/wp-content/themes/childtheme/img/cat5.jpg", "icon" => ""), 
    					  6 => array("name" => "", "img" => home_url()."/wp-content/themes/childtheme/img/cat5.jpg", "icon" => ""),
    					  
    		),
          );
          
    	
        
    ?>

    Then that images are processed similar to this:

    <div  class="col-12 col-sm-6 col-md-4 cat-item">
             <div style="background-image: url('<?php echo $savadata[1]['image']; ?>');" class="cat-image">
                <a href="<?php echo $savadata[1]['link']; ?>" class="cover-wrapper"><?php echo $savadata[1]['name']; ?></a>
             </div>
          </div>
          <div class="col-12 col-sm-6 col-md-4 cat-item">
             <div style="background-image: url('<?php echo $savadata[2]['image']; ?>');" class="cat-image">
                <a href="<?php echo $savadata[2]['link']; ?>" class="cover-wrapper"><?php echo $savadata[2]['name']; ?></a>
             </div>
          </div>
          <div class="col-12 col-sm-6 col-md-4 cat-item">
             <div style="background-image: url('<?php echo $savadata[3]['image']; ?>');" class="cat-image">
                <a href="<?php echo $savadata[3]['link']; ?>" class="cover-wrapper"><?php echo $savadata[3]['name']; ?></a>
             </div>
          </div>
          <div class="col-12 col-sm-6 col-md-4 cat-item">
             <div style="background-image: url('<?php echo $savadata[4]['image']; ?>');" class="cat-image">
                <a href="<?php echo $savadata[4]['link']; ?>" class="cover-wrapper"><?php echo $savadata[4]['name']; ?></a>
             </div>
          </div>
          <div class="col-12 col-sm-6 col-md-4 cat-item">
             <div style="background-image: url('<?php echo $savadata[5]['image']; ?>');" class="cat-image">
                <a href="<?php echo $savadata[5]['link']; ?>" class="cover-wrapper"><?php echo $savadata[5]['name']; ?></a>
             </div>
          </div>
    	        <div class="col-12 col-sm-6 col-md-4 cat-item">
             <div style="background-image: url('<?php echo $savadata[6]['image']; ?>');" class="cat-image">
                <a href="<?php echo $savadata[6]['link']; ?>" class="cover-wrapper"><?php echo $savadata[6]['name']; ?></a>
             </div>
          </div>

    But the image never arrives there. Well it arrives but not seen. For example. I put this in any place in that code without affected just to monitor and what value returns:

    <?php echo $savadata[1]['image']; ?>

    And on the screen I see “Array”, then I know now the background url is entered as the word “Array” and is not valid. Now I enter this command to monitor if it is really right, the same as the previous one, just to monitor:

    <?php print_r(array_values($savadata[1]['image'])); ?>

    And here I see the perfect value:

    Array ( [0] => [1] => https://xxx.xxx.xxx.xxx/tests/wp-content/themes/childtheme/img/cat1.jpg [1] => )

    That is the value and is the correct link, well here I entered xxx to hide our ip for security, but is ok the result given for the command. But how can I enter in the “DIV” command the background url from there? In other words extract real the Srray value as I did with the print_r command where there it shows, but applicable to the “DIV” command.

    As can be seen the default command echo “$savadata[1][‘image’]“ and monitoring it gives “Array” word, and I suppose that is the problem the “DIV” is calling a bad link maybe.

    How can be solved?

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • It’s difficult to make out what your code is doing since you use different variable names.
    The first section has an array with one element called ‘sampledata’. That is an array with 6 elements. Each of the 6 is an array with 3 elements called ‘name’, ‘img’, ‘icon’.
    The URL should use the function get_stylesheet_directory_uri() instead of home_url() so that the folder can be a different name and in a different place.

    The second section uses a variable $savadata which uses a different name of ‘image’ where the other used ‘img’.

    Try again using print_r, but don’t put array_values.

    Thread Starter Alex E.

    (@coerrace)

    print_r alone gives the array all texts, including parentheses. In short. This is the variable I want to extract the data:

    $savadata[1]['image']; ?>');

    There is the link, the only problem with echo shows word “Array”, and if I use print_r it will be all formatting and information of the array.

    Thank you to look.

    Hi,

    like @joyously said, if you provide sample code, make sure it is consistent.

    Your $savedata array contain one element ‘sampledata’ (which contains the other elements). I am unsure what the reasoning for that may be, but given the array you posted, to echo a value use:

    echo $savadata['sampledata'][1]['img'];

    @joyously also indicated that you should use get_stylesheet_directory_uri() to get the path to the child theme folder.

    Then, to avoid redundancy in code, I would loop through the array and output the html and values using a foreach loop, e.g.

    foreach ($savadata['sampledata'] as $data) { 
        echo $data["img"];
    }

    (add your div’s and tags)

    If you also do not know what the ‘sampledata’ array element is for, better to omit it. And to clean up a bit, I would avoid further redundacy and use more relevant names like this:

    $categories = array(
        "cat1" => array("name" => "", "img" => "/img/cat1.jpg", "icon" => ""),
        "cat2" => array("name" => "", "img" => "/img/cat2.jpg", "icon" => ""),
        "cat3" => array("name" => "", "img" => "/img/cat3.jpg", "icon" => ""),
        "cat4" => array("name" => "", "img" => "/img/cat4.jpg", "icon" => "")
    );

    and then:

    foreach ($categories as $category) { 
        echo "<div>";
            echo get_stylesheet_directory_uri() . $category["img"];
        echo "</div>";
    }

    This assumes that the “name” and “icon” values in the array will be used somewhere in the html, which is not clear from your code sample either.

    btw, $savadata also refers to the $settings array in your first post. This as a result of the confusing and inconsistent info in the posts in this topic.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Child theme background Problem’ is closed to new replies.