如果查询有结果,则显示包装器div,然后循环结果

时间:2016-12-06 作者:m.javad Koushki

在下面代码的第二个查询中,如果有任何结果,我试图只显示查询结果的包装器div,然后在显示包装器div后在列表中循环结果。

然而,我得到了Parse error: syntax error, unexpected \'}\' on line 59. 我做错了什么?

<?php
// Creating the widget
class wpb_box extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
\'wpb_box\',
// Widget name will appear in UI
__(\'ابزارک اختصاصی باکس مطالب\', \'bigtheme\'),
// Widget description
array( \'description\' => __( \'ابزارک اختصاصی نمایش باکس مطالب دسته های مختلف در صفحه اصلی سایت\', \'bigtheme\' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
        $name = apply_filters( \'widget_title\', $instance[\'name\'] );
        $link1 = apply_filters( \'widget_title\', $instance[\'link1\'] );
$link = apply_filters( \'widget_title\', $instance[\'link\'] );
        $display = apply_filters( \'widget_title\', $instance[\'display\'] );
        $color = apply_filters( \'widget_title\', $instance[\'color\'] );
// This is where you run the code and display the output
?>
                    <article class="container-fluid">
                        <h5 style="background:<?php echo $color ?>;"><a href="<?php echo $link ?>" target="_blank"><?php echo $name ?></a></h5> 
                        <?php  
            $portfolio = new WP_Query(array(
            \'post_status\' =>\'publish\',
            \'post_type\' =>\'post\',
            \'cat\' =>\'\'.$link1.\'\',
            \'posts_per_page\' =>\'1\',
            \'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1
            )); if($portfolio->have_posts()) : while($portfolio->have_posts()) : $portfolio->the_post(); ?> 
                        <div class="first-post col-sm-6 col-xs-12">
                            <a href="<?php the_permalink() ?>" target="_blank"><?php the_post_thumbnail(\'thumb\',array( \'class\'=> "img-responsive")); ?></a>
                            <h2><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></h2>
                            <?php the_excerpt(); ?>
                        </div>  
                        <?php endwhile; endif;  wp_reset_query(); ?>
                        <?php  
            $portfolio = new WP_Query(array(
            \'post_status\' =>\'publish\',
            \'post_type\' =>\'post\',
            \'cat\' =>\'\'.$link1.\'\',
            \'offset\' => \'1\',
            \'posts_per_page\' =>\'\'.$display.\'\',
            \'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1            
            )); if($portfolio->have_posts()) : while($portfolio->have_posts()) : $portfolio->the_post(); ?> 
                        <div class="other-post col-sm-6 col-xs-12">
                            <ul style="color:#f0ad4e;">
                            <?php if($portfolio->have_posts()) : while($portfolio->have_posts()) : $portfolio->the_post();?>
                                <li><h2><a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a></h2></li>
                            <?php endwhile; endif;  wp_reset_query(); ?>
                            </ul>
                        </div>
                    </article>
<?php
echo $args[\'after_widget\'];
}
public function form( $instance ) {
        $name   = ( isset( $instance[ \'name\' ] ) ) ? $instance[ \'name\' ] : \'\';
        $link1  = ( isset( $instance[ \'link1\' ] ) ) ? $instance[ \'link1\' ] : \'\';
$link   = ( isset( $instance[ \'link\' ] ) ) ? $instance[ \'link\' ] : \'\';
        $color  = ( isset( $instance[ \'color\' ] ) ) ? $instance[ \'color\' ] : \'\';
        $display    = ( isset( $instance[ \'display\' ] ) ) ? $instance[ \'display\' ] : \'\';
?>
            <p>
                <label for="<?php echo $this->get_field_id( \'color\' ); ?>"><?php _e( \'رنگ باکس مطالب:\' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( \'color\' ); ?>" name="<?php echo $this->get_field_name( \'color\' ); ?>" type="text" value="<?php echo esc_attr( $color ); ?>" placeholder="مثال : #CCC , #dd3333 , black , blue" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( \'name\' ); ?>"><?php _e( \'عنوان باکس:\' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( \'name\' ); ?>" name="<?php echo $this->get_field_name( \'name\' ); ?>" type="text" value="<?php echo esc_attr( $name ); ?>" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( \'link1\' ); ?>"><?php _e( \'دسته بندی مطلب\' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( \'link1\' ); ?>" name="<?php echo $this->get_field_name( \'link1\' ); ?>" type="text" value="<?php echo esc_attr( $link1 ); ?>" />
            </p>
<p>
                <label for="<?php echo $this->get_field_id( \'link\' ); ?>"><?php _e( \'لینک آرشیو\' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( \'link\' ); ?>" name="<?php echo $this->get_field_name( \'link\' ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>" />
            </p>

            <p>
                <label><?php _e( \'نمایش توضیحات مطالب\' ); ?></label>
                <select class="widefat" id="<?php echo $this->get_field_id( \'display\' ); ?>" name="<?php echo $this->get_field_name( \'display\' ); ?>">
                    <option <?php selected( $instance[\'display\'], \'block\'); ?> value="block">بله</option> 
                    <option <?php selected( $instance[\'display\'], \'none\'); ?> value="none">خیر</option> 
                </select>
            </p>

<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance[\'name\'] = ( ! empty( $new_instance[\'name\'] ) ) ? strip_tags( $new_instance[\'name\'] ) : \'\';
        $instance[\'link1\'] = ( ! empty( $new_instance[\'link1\'] ) ) ? strip_tags( $new_instance[\'link1\'] ) : \'\';
$instance[\'link\'] = ( ! empty( $new_instance[\'link\'] ) ) ? strip_tags( $new_instance[\'link\'] ) : \'\';
        $instance[\'link2\'] = ( ! empty( $new_instance[\'link2\'] ) ) ? strip_tags( $new_instance[\'link2\'] ) : \'\';
        $instance[\'link3\'] = ( ! empty( $new_instance[\'link3\'] ) ) ? strip_tags( $new_instance[\'link3\'] ) : \'\';
        $instance[\'link4\'] = ( ! empty( $new_instance[\'link4\'] ) ) ? strip_tags( $new_instance[\'link4\'] ) : \'\';
        $instance[\'link5\'] = ( ! empty( $new_instance[\'link5\'] ) ) ? strip_tags( $new_instance[\'link5\'] ) : \'\';
        $instance[\'color\'] = ( ! empty( $new_instance[\'color\'] ) ) ? strip_tags( $new_instance[\'color\'] ) : \'\';
        $instance[\'display\'] = ( ! empty( $new_instance[\'display\'] ) ) ? strip_tags( $new_instance[\'display\'] ) : \'\';
        $instance[\'source\'] = ( ! empty( $new_instance[\'source\'] ) ) ? strip_tags( $new_instance[\'source\'] ) : \'\';
        $instance[\'time\'] = ( ! empty( $new_instance[\'time\'] ) ) ? strip_tags( $new_instance[\'time\'] ) : \'\';
        return $instance;
    }
} // Class wpb_box ends here
// Register and load the widget
function wpb_box() {
register_widget( \'wpb_box\' );
}
add_action( \'widgets_init\', \'wpb_box\' );
?>

1 个回复
最合适的回答,由SO网友:Emil 整理而成

您的第二个查询构建得有点不正确-扩展和缩进,如下所示:

if ($portfolio->have_posts()) :
    while($portfolio->have_posts()) :
        $portfolio->the_post(); ?> 
        <div class="other-post col-sm-6 col-xs-12">
            <ul style="color:#f0ad4e;">
                <?php
                if($portfolio->have_posts()) :
                    while($portfolio->have_posts()) :
                        $portfolio->the_post();
                        ?>
                        <li><h2><a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a></h2></li>
                <?php endwhile; endif;  wp_reset_query(); ?>
            </ul>
        </div>
您正在两次启动最后一个查询。我想你想做的只是展示other-post-当有第二次查询的结果时,div,然后循环查看这些结果以列出其他帖子的标题。您可以通过以下方式实现:

if ($portfolio->have_posts()): ?> 
    <div class="other-post col-sm-6 col-xs-12">
        <ul style="color:#f0ad4e;">
        <?php while($portfolio->have_posts()) : $portfolio->the_post();?>
            <li><h2><a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a></h2></li>
        <?php endwhile; ?>
        </ul>
    </div>
<?php endif;  wp_reset_query(); ?>
本质上,这是检查查询是否返回任何结果——如果返回,则显示容器div并创建while循环以显示列表中的实际帖子。

以下是更改后的完整代码(带有适当的缩进以便可读):

<?php
// Creating the widget
class wpb_box extends WP_Widget {
    function __construct() {
        parent::__construct(
            // Base ID of your widget
            \'wpb_box\',
            // Widget name will appear in UI
            __(\'ابزارک اختصاصی باکس مطالب\', \'bigtheme\'),
            // Widget description
            array( \'description\' => __( \'ابزارک اختصاصی نمایش باکس مطالب دسته های مختلف در صفحه اصلی سایت\', \'bigtheme\' )
        ));
    }
    // Creating widget front-end
    // This is where the action happens
    public function widget( $args, $instance ) {
        $name = apply_filters( \'widget_title\', $instance[\'name\'] );
        $link1 = apply_filters( \'widget_title\', $instance[\'link1\'] );
        $link = apply_filters( \'widget_title\', $instance[\'link\'] );
        $display = apply_filters( \'widget_title\', $instance[\'display\'] );
        $color = apply_filters( \'widget_title\', $instance[\'color\'] );
        // This is where you run the code and display the output
        ?>
        <article class="container-fluid">
            <h5 style="background:<?php echo $color ?>;"><a href="<?php echo $link ?>" target="_blank"><?php echo $name ?></a></h5> 
            <?php  
            $portfolio = new WP_Query(array(
                            \'post_status\' =>\'publish\',
                            \'post_type\' =>\'post\',
                            \'cat\' =>\'\'.$link1.\'\',
                            \'posts_per_page\' =>\'1\',
                            \'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1
            ));
            if($portfolio->have_posts()) : while($portfolio->have_posts()) : $portfolio->the_post(); ?> 
                <div class="first-post col-sm-6 col-xs-12">
                    <a href="<?php the_permalink() ?>" target="_blank"><?php the_post_thumbnail(\'thumb\',array( \'class\'=> "img-responsive")); ?></a>
                    <h2><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></h2>
                    <?php the_excerpt(); ?>
                </div>  
            <?php endwhile; endif;  wp_reset_query(); ?>
            <?php  
            $portfolio = new WP_Query(array(
                            \'post_status\' =>\'publish\',
                            \'post_type\' =>\'post\',
                            \'cat\' =>\'\'.$link1.\'\',
                            \'offset\' => \'1\',
                            \'posts_per_page\' =>\'\'.$display.\'\',
                            \'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1            
            ));
            if ($portfolio->have_posts()): ?> 
                <div class="other-post col-sm-6 col-xs-12">
                    <ul style="color:#f0ad4e;">
                        <?php while($portfolio->have_posts()) : $portfolio->the_post();?>
                            <li><h2><a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a></h2></li>
                        <?php endwhile; ?>
                    </ul>
                </div>
            <?php endif;  wp_reset_query(); ?>
        </article>
        <?php
        echo $args[\'after_widget\'];
    }

    public function form( $instance ) {
        $name   = ( isset( $instance[ \'name\' ] ) ) ? $instance[ \'name\' ] : \'\';
        $link1  = ( isset( $instance[ \'link1\' ] ) ) ? $instance[ \'link1\' ] : \'\';
        $link   = ( isset( $instance[ \'link\' ] ) ) ? $instance[ \'link\' ] : \'\';
        $color  = ( isset( $instance[ \'color\' ] ) ) ? $instance[ \'color\' ] : \'\';
        $display    = ( isset( $instance[ \'display\' ] ) ) ? $instance[ \'display\' ] : \'\';
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( \'color\' ); ?>"><?php _e( \'رنگ باکس مطالب:\' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( \'color\' ); ?>" name="<?php echo $this->get_field_name( \'color\' ); ?>" type="text" value="<?php echo esc_attr( $color ); ?>" placeholder="مثال : #CCC , #dd3333 , black , blue" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( \'name\' ); ?>"><?php _e( \'عنوان باکس:\' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( \'name\' ); ?>" name="<?php echo $this->get_field_name( \'name\' ); ?>" type="text" value="<?php echo esc_attr( $name ); ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( \'link1\' ); ?>"><?php _e( \'دسته بندی مطلب\' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( \'link1\' ); ?>" name="<?php echo $this->get_field_name( \'link1\' ); ?>" type="text" value="<?php echo esc_attr( $link1 ); ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( \'link\' ); ?>"><?php _e( \'لینک آرشیو\' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( \'link\' ); ?>" name="<?php echo $this->get_field_name( \'link\' ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>" />
        </p>

        <p>
            <label><?php _e( \'نمایش توضیحات مطالب\' ); ?></label>
            <select class="widefat" id="<?php echo $this->get_field_id( \'display\' ); ?>" name="<?php echo $this->get_field_name( \'display\' ); ?>">
                <option <?php selected( $instance[\'display\'], \'block\'); ?> value="block">بله</option> 
                <option <?php selected( $instance[\'display\'], \'none\'); ?> value="none">خیر</option> 
            </select>
        </p>
        <?php
    }
// Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance[\'name\'] = ( ! empty( $new_instance[\'name\'] ) ) ? strip_tags( $new_instance[\'name\'] ) : \'\';
        $instance[\'link1\'] = ( ! empty( $new_instance[\'link1\'] ) ) ? strip_tags( $new_instance[\'link1\'] ) : \'\';
        $instance[\'link\'] = ( ! empty( $new_instance[\'link\'] ) ) ? strip_tags( $new_instance[\'link\'] ) : \'\';
        $instance[\'link2\'] = ( ! empty( $new_instance[\'link2\'] ) ) ? strip_tags( $new_instance[\'link2\'] ) : \'\';
        $instance[\'link3\'] = ( ! empty( $new_instance[\'link3\'] ) ) ? strip_tags( $new_instance[\'link3\'] ) : \'\';
        $instance[\'link4\'] = ( ! empty( $new_instance[\'link4\'] ) ) ? strip_tags( $new_instance[\'link4\'] ) : \'\';
        $instance[\'link5\'] = ( ! empty( $new_instance[\'link5\'] ) ) ? strip_tags( $new_instance[\'link5\'] ) : \'\';
        $instance[\'color\'] = ( ! empty( $new_instance[\'color\'] ) ) ? strip_tags( $new_instance[\'color\'] ) : \'\';
        $instance[\'display\'] = ( ! empty( $new_instance[\'display\'] ) ) ? strip_tags( $new_instance[\'display\'] ) : \'\';
        $instance[\'source\'] = ( ! empty( $new_instance[\'source\'] ) ) ? strip_tags( $new_instance[\'source\'] ) : \'\';
        $instance[\'time\'] = ( ! empty( $new_instance[\'time\'] ) ) ? strip_tags( $new_instance[\'time\'] ) : \'\';
        return $instance;
    }
} // Class wpb_box ends here
// Register and load the widget
function wpb_box() {
register_widget( \'wpb_box\' );
}
add_action( \'widgets_init\', \'wpb_box\' );
?>