• Resolved inspirationally

    (@inspirationally)


    Hello,

    I enabled custom header images and uploaded three different header images. Now I want to show one at the top, one in the middle and one at the bottom (footer) of the page, but I’m stuck.

    If I want to show ALL header images (for a slider for example) I know I can call

    <?php $headers = get_uploaded_header_images();
    foreach($headers as $header) {
    echo $header['url'];
     } ?>

    and this works quite perfect, showing all images in a row.

    But I can’t manage to show just the first or just the second or just the third.

    I tried

    $headers = get_uploaded_header_images();
    foreach($headers as $header)
    {
       echo $header[0];
    }

    and

    <?php $headers = get_uploaded_header_images();
    foreach($headers as $header)
    {
       echo $header[0]['url'];
    } ?>

    but I get the error message
    Notice: Undefined offset: 0

    How can I achieve this?
    I know I can use css for themes, but I want others without css experience to easily exchange the pics or just change it to random header / slider.

    Thanks very much, Martina.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello inspirationally,

    I think you will not need the foreach loop if you just want to drop into a particular header.

    Can you try something like

    <?php $headers = get_uploaded_header_images(); at the top of the page, then when you call a header just use

    echo $headers[0]['url']; or echo $headers[1]['url'];

    When you are inside the foreach, $header will only have one occurrence of the header with the array holding the meta information for that particular header. Keeping in the headerS array will allow you to pick from any.

    Of course you may have to do some error checking to make sure you do have the number header you are calling.

    Let me know if that works at all.

    Drew

    Thread Starter inspirationally

    (@inspirationally)

    Hey Drew,

    thanks very much.

    unfortunately, this throws the same error:

    Notice: Undefined offset: 0 (or 1).

    I also tried

    $headers = get_uploaded_header_images();
    $i="0";
    while ($i < 1000) {
    echo $headers[$i]['url'];
    $i++;
    }

    just to catch all possible numbers of this relatively new installation.
    They all throw the same error, as if you simply can’t call them with numbers.

    Kind regards, Martina.

    Does indeed seem strange,

    Have you tried printing the $headers array, to see how it is made up?

    <pre>
    <?php print_r ($headers); ?>
    </pre>
    Thread Starter inspirationally

    (@inspirationally)

    Hey Drew,

    I’m no php-professional (obviously), so I did not know this handy php function, which is of a big help, thank you!

    This tells me

    Array ( [header-11.jpg] => Array ( [attachment_id] => 104 [url] => https://example.org/wp-content/uploads/2015/09/header-11.jpg [thumbnail_url] => https://example.org/wp-content/uploads/2015/09/header-11.jpg [alt_text] => [width] => 2362 [height] => 1550 )

    and so on.

    And with

    echo $headers[“header-11.jpg”][‘url’];

    I can indeed echo the URL to that image. Just that it varies depending on the image name, so that it does not make any sense to write that.

    Now I continued thinking about that and came to this solution (stopping the foreach loop after two steps):

    $headers = get_uploaded_header_images();
    $i=0;
    foreach($headers as $key => $header) {
    $header=$header['url'];
    $i++;
    if($i==2) break;
        }
    echo $header;

    for the second header.

    I also wrote a little function for the functions.php, and it works with just additional_header_image(2); in the template files.

    /************* ADDITIONALL HEADER IMAGE *************/
    
    function additional_header_image($n=2) {
    $headers = get_uploaded_header_images();
    $i=0;
    foreach($headers as $key => $header) {
    $header=$header['url'];
    $i++;
    if($i==$n) break;
        }
    echo $header;
    }

    Thank you, Drew, for your great help giving me the right direction.

    Martina.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Call a certain custom header image’ is closed to new replies.