Php 在短码问题中调用数组

Php 在短码问题中调用数组,php,arrays,wordpress,shortcode,Php,Arrays,Wordpress,Shortcode,我正在将多个模板上的一些重复代码压缩成一个可以轻松重用的短代码 我把特色图片url称为内联背景图片样式,效果非常好!这是密码 <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?> <?php while (have_posts()) : the_post(); ?> <

我正在将多个模板上的一些重复代码压缩成一个可以轻松重用的短代码

我把特色图片url称为内联背景图片样式,效果非常好!这是密码

<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
     <?php while (have_posts()) : the_post(); ?>
         <div class="single-casestudy" style="background-image: url('<?php echo $image[0]; ?>'); background-size: 100%;">
              <div class="case-study-content">
                  <h4><?php the_title(); ?></h4>
                  <p><?php echo excerpt(22); ?></p>
              </div>
         </div>
     <?php endwhile ; ?>
当不在短代码中时,它可以正常工作,我可以在现场站点上看到我的特色图片作为背景,但是当将其放入短代码函数中时,它不会出错,它就是不工作!下面是我用于快捷码功能的代码。看起来$image[0]没有将任何数据拉入数组

function otherinfo_function() {
            $args = array( 'post_type' => 'casestudy', 
                                    'posts_per_page' => -1,
                                    'orderby' => menu_order,
                                    'order' => RAND
                                );

            query_posts($args);

            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ),'single-post-thumbnail' );
            while (have_posts()) : the_post();

            echo $image[0];
                $output = $output . '<div class="single-casestudy" style="background-image: url(' . $image[0] .'); background-size: 100%;">';
                    $output = $output . '<div class="case-study-content">';

                        $output = $output . '<h4>' . get_the_title() . '</h4>';
                        $output = $output . '<p>' . excerpt(22) . '</p>';

                    $output = $output . '</div>';
                $output = $output . '</div>';

            endwhile ;

        $output = $output . '</div>';
return $output;
}

add_shortcode('otherinfo', 'otherinfo_function');
如果有人能帮忙,那就太好了


Sam

首先,为什么要使用?请将您的帖子检索器替换为类或,如果问题仍然存在,请告知我们。此外,您的短代码中的对象$post未定义。请将其作为全局$post添加到函数定义的第一行。添加$global post并没有修复@RahilWazir的第一条注释中的错误,值得一读。关于你最后的评论,$globalpost不会纠正这个错误——你的$s放错地方了。根据Rahil的第二条评论,它应该是global$post。然后尝试将wp\u get\u attachment\u image\u src调用移动到\u post;之后的循环中;。谢谢@Hobo,我不知道为什么我以前没想到把它放到循环中!谢谢,这很有效