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.

I'm using this same come in two files: single.php and home.php:

(Outside of the loop).

(There is a #mainbar div in both single posts and home page but the image should be different)

<?php // Set and display custom field
        $mainbar_right_title = get_post_meta($post->ID, 'Mainbar Right Title', true);
        $mainbar_right_image = get_post_meta($post->ID, 'Mainbar Right Image', true); ?>
        <div class="float-right">
            <h2><?php echo $mainbar_right_title; ?></h2>
            <img src="<?php echo $mainbar_right_image ?>" alt="" />
        </div> <?php
    ?>
    </div>

I thought the home page would get the value of its own custom field but instead is getting the value of the single post. Is this a default behaviour or I'm doing something wrong?

EDIT: I tried adding a value to another single post and the image is indeed the one of the post not of the another so the problem is only the home page.

home.php:

<?php
/**
 * Template Name: Home
 * @package WordPress
 * @subpackage Prominent
 * @since Prominent 1.0
 */
get_header(); ?>

<?php get_sidebar(); ?>

<div id="content">
    <?php // Insert image using custom field
        $postimageurl = get_post_meta($post->ID, 'Intro Image', true); ?>
        <div class="block-1">
            <img src="<?php echo $postimageurl; ?>" alt="Post Pic" />
        </div> <?php
    ?>

    <?php // Start the Loop
        if ( have_posts() ) while ( have_posts() ) : the_post();
    ?>  <div class="block-2 padding-top">
            <?php the_content(); ?>
        </div><!-- .entry-content -->

    <?php endwhile; // end of the loop. ?>

    <?php // Create and run custom loop
        $custom_posts = new WP_Query();
        $custom_posts->query('post_type=blocks&location=Front Page&section=Mainbar');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>  <div class="block-2 border-top">
            <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    <?php endwhile; ?>

        <?php // Set and display custom field
            $mainbar_left_title = get_post_meta($post->ID, 'Mainbar Left Title', true);
            $mainbar_left_image = get_post_meta($post->ID, 'Mainbar Left Image', true); ?>
            <div class="float-left">
                <h2><?php echo $mainbar_left_title; ?></h2>
                <img src="<?php echo $mainbar_left_image ?>" alt="" />
            </div> <?php
        ?>

        <?php // Set and display custom field
            $mainbar_right_title = get_post_meta($post->ID, 'Mainbar Right Title', true);
            $mainbar_right_image = get_post_meta($post->ID, 'Mainbar Right Image', true); ?>
            <div class="float-right">
                <h2><?php echo $mainbar_right_title; ?></h2>
                <img src="<?php echo $mainbar_right_image ?>" alt="" />
            </div> <?php
        ?>
        </div>

    <?php // Create and run custom loop
        $custom_posts = new WP_Query();
        $custom_posts->query('post_type=blocks&location=Front Page&section=Sidebar');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>  <div class="block-3 border-top">
            <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a>
            <p><?php the_excerpt(); ?></p>
            <p><?php echo get_post_meta($post->ID, "Other_Work", true); ?></p>
        </div>
    <?php endwhile; ?>

    <?php // Create and run custom loop
        $custom_posts = new WP_Query();
        $custom_posts->query('post_type=blocks&location=Front Page&section=Featured');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>  <div class="block-2 border-top block-height-2">
            <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_post_thumbnail('large'); ?></a>
            <p><?php the_excerpt(); ?></p>
        </div>
    <?php endwhile; ?>

    <?php // Create and run custom loop
        $custom_posts = new WP_Query();
        $custom_posts->query('post_type=blocks&location=Front Page&section=Content');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>  <div class="block-3 border-top block-height-2">
            <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
            <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a>
            <p><?php the_excerpt(); ?></p>
            <p><?php the_meta(); ?></p>
        </div>
    <?php endwhile; ?>
</div><!-- #content -->

<?php get_footer(); ?>
share|improve this question
add comment

2 Answers

You are doing something wrong.

First of all, do a var_dump(__FILE__); to make sure you're in the template you expect.

If that doesn't clear the mist, post the entire template, for context.

share|improve this answer
    
yes it is the template: string(56) "/home/alex/www/wpac/wp-content/themes/prominent/home.php". Please see my EDIT. –  janoChen Feb 18 '11 at 13:50
    
Ok, now just above $postimageurl = ..., do a var_dump($post); –  scribu Feb 18 '11 at 13:57
    
Thanks anyways your answer helped me. –  janoChen Feb 18 '11 at 14:03
add comment

I figured out why.

It is getting the ID from this line above:

$custom_posts->query('post_type=blocks&location=Front Page&section=Mainbar');

I thought Worpdress gets the IDs of the files (not the individual loops).

This fixed the problem:

<?php wp_reset_query(); ?>

Should I include that reset at the end of every custom loop?

share|improve this answer
2  
Yep. For query_posts(), use wp_reset_query(); for new WP_Query() use wp_reset_postdata(). –  scribu Feb 18 '11 at 14:23
    
Also, files don't have IDs. They're just files. :) –  scribu Feb 18 '11 at 14:23
    
@scribu Damn, I think it was a stupid question. But somethimes Wordpress 'detect' files names to make them templates, but I guess that's another story. –  janoChen Feb 18 '11 at 14:25
add comment

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.