自定义帖子类型错误

I'm using this piece of code to pull out cars and the car details in a custom post type within wordpress.

<h1>Cars for Sale</h1>
<?php $args = array( 'post_type' => 'carlistings', 'posts_per_page' => 10 ); ?>
<ul id="carsforsale">
    <?php $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <?php $cardetails = get_post_meta( $post->ID, 'cardetails', true );
            foreach( $cardetails as $cardetails ) { ?> <!-- Line # 13 -->
        <li class="pop"><span>
            <span class="thumb"><a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('thumbnail'); } ?></a></span>
            <span class="title"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></span>
            <strong>Make</strong> <?php echo $cardetails['make'] ?><br />
            <strong>Model</strong> <?php echo $cardetails['model'] ?><br />
            <strong>Year</strong> <?php echo $cardetails['year'] ?><br />
            <strong>Color:</strong> <?php echo $cardetails['color'] ?><br />
            <strong>Price:</strong> <?php echo $cardetails['price'] ?><br />
        </span></li>
<?php } ?>
<?php wp_reset_query(); ?>

The problem I have is, that after 3 enteries I get this error message

Warning: Invalid argument supplied for foreach() in /home/styleaut/public_html/wp-content/themes/styleautosales/page-30.php on line 13

line 13 falls on this part of the code:

foreach( $cardetails as $cardetails ) {

Add if statement before foreach like this:

<h1>Cars for Sale</h1>
<?php $args = array( 'post_type' => 'carlistings', 'posts_per_page' => 10 ); ?>
<ul id="carsforsale">
    <?php $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <?php $cardetails = get_post_meta( $post->ID, 'cardetails', true );
            if($cardetails) {
            foreach( $cardetails as $cardetails ) { ?>
        <li class="pop"><span>
            <span class="thumb"><a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('thumbnail'); } ?></a></span>
            <span class="title"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></span>
            <strong>Make</strong> <?php echo $cardetails['make'] ?><br />
            <strong>Model</strong> <?php echo $cardetails['model'] ?><br />
            <strong>Year</strong> <?php echo $cardetails['year'] ?><br />
            <strong>Color:</strong> <?php echo $cardetails['color'] ?><br />
            <strong>Price:</strong> <?php echo $cardetails['price'] ?><br />
        </span></li>
<?php } // end foreach
} // end if ?>
<?php wp_reset_query(); ?>

Change

foreach( $cardetails as $cardetails ) {

to

foreach( $cardetails as $value ) {

and you will need to change all $cardetails variables usage in the body of foreach to $value, f.e.:

$cardetails['make']

needs to be changed to:

$value['make']

$cardetails['model'] to $value['model']

and so on..

I think your problem is the way you're calling get_post_meta. By setting the last parameter to true, it will return the value in the first row of the post meta, which will always be a string. Try calling the function without that parameter.