0

I would like to display two images in my header.

functions.php:

define('BP_DTHEME_DISABLE_CUSTOM_HEADER', true);

//add support for featured images with post type of header image
add_theme_support( 'post-thumbnails', array('header_image') ); 

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'header_image',
        array(
            'labels' => array(
                'name' => __( 'Header Image' ),
                'singular_name' => __( 'Header Image' )
            ),
        'public' => true,
        'has_archive' => true,
        'supports' => array('thumbnail') //(featured image, current theme must also support post-thumbnails)
        )
    );
}

header.php:

        <?php
            $args = array( 'post_type' => 'header_image');
            $header_images = new WP_Query( $args );
        ?>
        <?php   foreach($image as $header_images){ ?>
                <div class="header-image">
                <?php echo get_the_post_thumbnail($image, 'thumbnail'); ?>
                </div>
        <?php       
            }               
        ?>

Now I have two problems:

1) I am getting a php error: Warning: Invalid argument supplied for foreach() in header.php

2) Where do I append two image paths to the header_image post type array so that i can reference the two images in header.php?

thanks for response

2 Answers 2

1

I think you should be using the WordPress loop instead of a foreach, since you're using WP_Query.

 <?php if( $header_images->have_posts() ) { while( $header_images->have_posts() ) { $header_images->the_post(); ?>
      <div class="header-image">
           <?php echo get_the_post_thumbnail( $post->ID, 'thumbnail' ); ?>
      </div>
 <?php } } ?>
5
  • This is giving me a parse error on the first line of your code Jun 26, 2012 at 20:07
  • Sorry I was missing a closing paranthesis. Fixed.
    – Shane
    Jun 26, 2012 at 20:12
  • Im still a little confused. How is someone supposed to add the thumbnail? Or should this be asked in another question? Jun 26, 2012 at 20:28
  • @JohnMerlino Well in your code above, you added thumbnail support for your custom post type. So, to add a thumbnail, you should add / edit a post in your custom post type admin section, and add a featured image there.
    – Shane
    Jun 26, 2012 at 20:31
  • Im still having problems with uploading. I get this error: Unable to create directory /Users/johnmerlino/Documents/github/cms/wp-content/uploads/2012/06. Is its parent directory writable by the server? It doesn't seem to be uploading to right place. Jun 26, 2012 at 21:01
0

You've got your order wrong on your foreach.

<?php foreach($image as $header_images){ ?>

Should be

<?php foreach($header_images as $image ){ ?>

Since you're using a WP_Query, be sure to use <?php wp_reset_query(); ?> after the closing bracket of your foreach.

2
  • True that the order in his foreach is wrong, but $header_images is a WP_Query object. I don't see how his code would work even with the proper foreach order, unless he did a foreach( $header_images->posts as $image ). No?
    – Shane
    Jun 26, 2012 at 20:25
  • foreach works for arrays and objects - I was simply fixing the parse error. Since foreach will work, the OP will need to check out exactly what's being returned as $image inside the foreach and code accordingly. There's usually multiple ways of getting info from a WP_Query object, and your way above is just as good (although it does need an endwhile and a wp_reset_query() after).
    – SickHippie
    Jun 26, 2012 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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