Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

As we all know, the twenty-eleven theme comes with a random banner feature which, if the site visitor clicks it, takes him to the home page.

So there could be quite a few different images for the banner, but they all take you to the same URL when you click them.

What I would like to have is that each banner image will have its own URL.

How do I accomplish this?

share|improve this question
1  
even if you could override the URL, how would you propose to edit/add/change the urls from the backend? –  Tom J Nowell Apr 13 '12 at 17:14
    
@Tom J Nowell That's an excellent question and +1 for that. I, of course, prefer some sort of a plugin that will allow me to change the URLs from the backend, but if something like this isn't already available, then I will compromise on hacking the child theme's PHP files. Any idea which file I should be looking at? –  ef2011 Apr 13 '12 at 19:07

1 Answer 1

up vote 1 down vote accepted

I recommend you register a post type called 'header_slide' and use the featured image of said posts as the banner. Do a query with a maximum of 1 post per page and display the banner, and add the url as a custom field.

Something similar to this:

$q = new WP_Query(array(
    'post_type' => 'header_slide',
    'posts_per_page' => 1,
    'orderby ' => 'rand'
));

if($q->have_posts()){
    while($q->have_posts()){
        $q->the_post();
        if(has_post_thumbnail()){
            $url = get_post_meta($post->ID,'custom_header_link',true);
            if(empty($url)){
                $url = home_url();
            }
            echo '<a href="'.$url.'">';
            the_post_thumbnail(array(920,200)); // adjust numbers to fit desired size
            echo '</a>';
        }
    }
} else {
    // hmm no header slides were found, ABORT!!!!!! or maybe display a default
}

wp_reset_postdata();

To generate the code for registering the header_slide post type, go here:

http://themergency.com/generators/wordpress-custom-post-types/

Answer the questions and it will generate a chunk of code for you at the end, put that code into your themes functions.php, and make sure it supports custom fields and featured images/post thumbnails

share|improve this answer
    
What a great detailed answer. If this also allows editing the URL from the backend, then I couldn't have expected a better answer than this. Now, I do have a header plugin installed which associates each page with a specific banner. Will this get in the way? BTW, my own grepping found that the URL is currently fixed at header.php around line #74. –  ef2011 Apr 16 '12 at 12:59
    
This solution is separate from your header plugin. You should have made us aware you already had a plugin when you asked the question. My solution is self contained, and separate –  Tom J Nowell Apr 16 '12 at 13:17
    
Didn't notice your link to header.php, I must point out that whatever you do, it will not be using the standard wordpress header banner API call –  Tom J Nowell Apr 16 '12 at 15:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.