How can I use a custom hook with images combined with an $args = array?

Q: How can I use a custom hook with images combined with an $args = array?

I need help with managing images via functions in WordPress.

I am building a WordPress theme that matches my static website at http://craigwebbart.com.

The website header features two images; a small image in the right corner of the navigation and a second image placed above the main content area.

These images are intended to be random and not generally associated with the page content, but sometime they are.

WordPress does not seem to offer a method to insert images except with certain hooks such as header_image and featured_image.

I used the header_image as a hook for the right_rail image and the theme_add_navpics function allows to load random images via the customizer.

There is no 2nd header_image hook for the other image, and I’d rather not use either the header_image or featured_image hooks for these image. I’d rather reserve those for other uses.

I have hit upon “hooks” as an alternate method to insert images. The insert_rt_rail_image function works but I’m stuck on how to reconcile it with adding a random image array accessible via the customizer.

I cannot find a way to dynamically call images via function.php – No path is available.

Q: How can I use a custom hook with images combined with an $args = array?

Thank in advance for any clues.

// CUSTOM NAVPICS
function theme_add_navpics() {
$args = array(
    'flex-width'    => true,
    'width'                  => 278,
    'flex-height'    => true,
    'height'                 => 36,
    'default-image' => get_template_directory_uri() . '/assets/images/navpics/danger_plane.jpg',
    'random-default'         => true,
    'uploads'       => true,
);
add_theme_support( 'custom-navpics', $args );
}
<!-- IN HEADER: -->

<!-- NAVPIC: -->
<figure id="">
<img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="917&middot;797&middot;0983" />
</figure>
  • – - – - – - – - – - – - –
// IN FUNCTIONS:

// Create an action hook to target the right rail image

function insert_rt_rail_image() {

echo '<figure><img src="/wp-content/themes/cwebba/assets/images/navpics/rt_rail_ocean-boy.jpg" /></figure>';}

add_action('rt_rail_image', 'insert_rt_rail_image');
<!-- HOOK TO INSERT IMAGE: -->
<?php do_action('rt_rail_image'); ?>

Have you searched on wordpress.org for any questions similar to yours? I often found that any ‘unique’ requirement that I had on a WordPress site already was attempted by someone else who found a suitable solution.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.