如何编辑此插件以在短代码中添加字段?

时间:2017-11-14 06:44:13

标签: php arrays wordpress plugins

我正在使用Wordpress中的这个主题并尝试编辑插件,以便在Visual Composer中添加时,它会询问您想要显示多少个帖子的问题?我将此添加到属性'number_of_post'=> '4',并添加了'posts_per_page'=> $ number_of_post,WP_Query参数,它似乎仍然无法正常工作。

以下是该插件的实际代码:

//Work Area
function ratio_work_area( $atts , $content = null ){
 // Attributes
    extract( shortcode_atts(
        array(                                                                                                                 
            'sec_title_before' => 'Latest',                   
            'sec_title_after' => 'works',     
            'sec_btn_text' => 'See More Work',    
            'sec_btn_link' => '#',  
            'number_of_post' => '4',   
        ), $atts )
    );
ob_start();
?>

<!-- START PORTFOLIO -->    
<section id="portfolio" class="works_area section-padding"> 
    <div class="container-fluid">
        <div class="row text-center">
            <div class="section-title wow zoomIn">
                <h2><?php echo esc_html($sec_title_before);?> <span><?php echo esc_html($sec_title_after);?></span></h2>
                <div></div>
            </div>
            <div class="col-md-12">
                <div class="our_work_menu">

                    <?php $terms = get_terms('cat_portfolios');
                        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
                    ?>
                    <ul>
                        <li class="filter wow fadeIn" data-wow-duration="1.5s" data-wow-delay=".25s" data-filter="all"><?php esc_html_e('All' , 'ratio');?></li>
                        <?php foreach ( $terms as $term ) :?>
                        <li class="filter" data-filter=".<?php echo esc_attr($term->slug); ?>"><?php echo esc_html($term->name); ?></li>
                        <?php endforeach;?>
                    </ul>
                    <?php }?>

                </div>              
            </div>

            <div class="work_all_item">
    <?php 

                // WP_Query arguments
                $args = array (
                    'post_type'              => array( 'portfolios' ),
                    'posts_per_page'              => $number_of_post,
                );

                // The Query
                $ratio_port_query = new WP_Query( $args );

                // The Loop
                if ( $ratio_port_query->have_posts() ) {
                    while ( $ratio_port_query->have_posts() ) {
                        $ratio_port_query->the_post(); 
                        $ratio_port_image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()),'ratio_image_1200_1200');
                        ?>
                        <?php 

                            $portfolio_terms = get_the_terms(get_the_id(), 'cat_portfolios');

                            if ( ! empty( $portfolio_terms ) && ! is_wp_error( $portfolio_terms ) ):

                                $portfolios_cat_slug = array();

                                foreach($portfolio_terms as $portfolio_term){
                                    $portfolios_cat_slug[] = $portfolio_term->slug ;
                                }

                                $portfolios_cat_array = join(" ", $portfolios_cat_slug);
                                $portfolios_class_array = join(" ", $portfolios_cat_slug);
                            endif;
                        ?>          

                        <div class="grid-item col-md-3 col-sm-6 col-xs-12 mix all <?php echo esc_attr($portfolios_class_array);?>">
                            <div class="single_our_work">
                                <div class="sing_work_photo">
                                    <figure>                                
                                        <img src="<?php echo esc_url($ratio_port_image['0']);?>" alt="">
                                        <div class="sing_work_text_link">
                                            <div class="sing_work_content_wrap">
                                                <div class="sing_work_content">                                                                                         
                                                    <div class="sing_link_img">
                                                        <a href="<?php echo esc_url($ratio_port_image['0']);?>" class="lightbox search" data-gall="gall-work"><i class="fa fa-eye"></i></a>
                                                        <a href="<?php the_permalink();?>" class="link"><i class="fa fa-link"></i></a>
                                                    </div>  

                                                    <h5><?php the_title();?></h5>
                                                <?php $terms = get_the_terms(get_the_ID(), 'cat_portfolios');
                                                        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
                                                            foreach ( $terms as $term ) {
                                                    ?>
                                                    <p><?php echo esc_html($term->name); ?></p>

                                                    <?php
                                                            }
                                                    } ?>                                                        
                                                </div>
                                            </div>
                                        </div>  
                                    </figure>
                                </div>              
                            </div>
                        </div>


                <?php   }
                } else {
                    // no posts found
                }

                // Restore original Post Data
                wp_reset_postdata();            

            ?>

            </div>              
        </div>
        <?php if($sec_btn_link){ ?>
            <div class="portfolio_btn text-center">
                <a href="<?php echo esc_url($sec_btn_link);?>" class="btn-light-bg"><?php echo esc_html($sec_btn_text);?></a>
            </div>
        <?php } ?>
    </div>          
</section>      
<!-- END PORTFOLIO -->  

<?php 
  return ob_get_clean();
}
add_shortcode ('work_area', 'ratio_work_area' );

1 个答案:

答案 0 :(得分:0)

请尝试使用'number_of_posts' => '4',;你错过了字母 s 。 如果这不起作用,请尝试使用 posts_per_page 而不是 number_of_posts 。这取决于您使用的版本。