dongzhengzhong1282 2014-07-28 08:26
浏览 10
已采纳

自定义帖子类型错误

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 ) {
  • 写回答

3条回答 默认 最新

  • dtu36380 2014-07-28 08:29
    关注

    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(); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了