注意:未定义的变量:在第48行的C:\ wamp \ www \ moderna \ wp-content \ themes \ moderna \ inc \ shortcodes.php中发布

时间:2015-01-17 16:58:20

标签: php wordpress shortcode

function portfolio_shortcode($atts, $content = null){
    extract( shortcode_atts( array(
        'type' => 'post',
    ), $atts ) );

     $q = new WP_Query(
        array('posts_per_page' => 5, 'post_type' => 'portfolio')
        );              

    $list = '<div class="row">
                <section id="projects">
                    <ul id="thumbs" class="portfolio">';
    while($q->have_posts()) : $q->the_post();
        $idd = get_the_ID();

        $portfolio_large = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'portfolio-large' );
        $portfolio_thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'portfolio-image' );

        $list .= '

                    <!-- Item Project and Filter Name -->
                <li class="col-lg-3 design" data-id="id-0" data-type="web">
                <div class="item-thumbs">
                <!-- Fancybox - Gallery Enabled - Title - Full Image -->
                <a class="hover-wrap fancybox" data-fancybox-group="gallery" title="'.get_the_title().'" href="'.$portfolio_large[0].'">
                <span class="overlay-img"></span>
                <span class="overlay-img-thumb font-icon-plus"></span>
                </a>
                <img src="'.$portfolio_thumb[0].'" alt="'.get_the_title().'" />

                </div>
                </li>

        ';  

    endwhile;
    $list.= '</ul></section></div>';
    wp_reset_query();
    return $list;
}
add_shortcode('portfolio', 'portfolio_shortcode'); 

1 个答案:

答案 0 :(得分:4)

在尝试使用之前,您不会包含全局帖子。引起问题的线是:

$portfolio_large = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'portfolio-large' );
$portfolio_thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'portfolio-image' );

此特定错误有两种解决方案。

1)

global $post;添加到您的函数顶部。 E.g。

function portfolio_shortcode( $atts, $content = null ) {
    global $post;
    ...

2)

您已使用$idd = get_the_ID();获取ID。使用它而不是$post->ID。 E.g。

$portfolio_large = wp_get_attachment_image_src( get_post_thumbnail_id( $idd ), 'portfolio-large' );
$portfolio_thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $idd ), 'portfolio-image' );

还有第三种选择,我最后会提到它,因为如果你将来遇到类似的问题,它对你没有帮助。 get_post_thumbnail_id()将对循环中的当前帖子起作用。因此,您无需传入任何ID。