代码之家  ›  专栏  ›  技术社区  ›  LOTUSMS

如何使wordpress快捷代码功能可重复使用?

  •  0
  • LOTUSMS  · 技术社区  · 3 年前

    我正在做一个自定义博客(没有模板,插件最少)。我在functions.php文件中有这个代码片段,用于按类别(DayTrips)显示博客。我的问题是,我想通过在使用级别使类别名称动态化,使其更易于维护,也不那么冗长。我有大约30个不同的类别。我不想把这个快捷代码复制30份,然后把每个都分配到一个类别。

    这是我的短代码

            // Blog Posts Queries
        function day_trips_blogs() {
            ob_start();
    
            $the_query = new WP_Query( 
                array( 
                    'category_name' => 'day-trips', <-- THIS PART
                    'post_status' => 'publish',
                    'orderby'=> 'date', 
                    'order' => 'DESC' 
                ) 
            ); 
            
            // The Loop
            if ( $the_query->have_posts() ) { 
                ?>
                    <div class="uk-grid postcards">
                    <?php
                        while ( $the_query->have_posts() ) {
                            $the_query->the_post();
    
                            if ( has_post_thumbnail() ) {     
                                $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post=""->ID), '335.95, 335.95' ); 
                                ?>   
                                <div class="uk-width-1-3@m uk-margin-bottom">
                                    <div class="uk-card uk-card-default uk-padding-remove">
                                        <div class="uk-card-header uk-padding-remove">
                                            <div class="uk-card-media-top">
                                                <a class="text-dark" href="<?php the_permalink() ?>">
                                                    <div class="card-image lazy-load-active text-center" style="background-size:cover; background-image: url( '<?php echo $backgroundImg[0]; ?>'); "></div> 
                                                </a>
                                            </div>
                                            <div class="uk-card-body uk-padding-small">
                                                <h3 class="uk-card-title uk-text-center">
                                                    <?php the_title() ?>
                                                </h3>
    
                                                <p class="uk-margin-bottom">
                                                    By: <?php echo get_the_author(); ?>
                                                </p>
    
                                                <p><?php the_excerpt() ?></p>
                                            </div>
                                        </div>
                                    </div>  
                                </div>
                            <?php
                        } else { 
                            // if no featured image is found
                            ?> 
                                <div class="uk-width-1-3@m uk-margin-bottom">
                                    <div class="uk-card uk-card-default uk-padding-remove">
                                        <div class="uk-card-body uk-padding-small">
                                            <a class="text-dark" href="<?php the_permalink() ?>">
                                                <h3 class="uk-card-title">
                                                    <?php the_title() ?>
                                                </h3>
                                            </a>
                                            <p><?php the_excerpt() ?></p>
                                        </div>
                                    </div>  
                                </div>       
                            <?php
                        }
                    }
            } else {   
                    // if no data is found
                ?>
                    <div class="postsbycategory widget_recent_entries uk-grid friends">
                        <div class="uk-width-1-1 uk-text-center">
                            <h4>No blogs are available yet. Come back soon!</h4>
                        </div>
                    </div>
                <?php
            }
    
            return ob_get_clean();
    
            /* Restore original Post Data */
            wp_reset_postdata();
        }
        add_shortcode('day-trips-posts', 'day_trips_blogs');
    

    我怎样才能通过 'category_name' => '??????' 到短代码本身,所以我可以这样称呼它

        [blogs category="day-trips"]
        [blogs category="recipes"]
        [blogs category="whatever"]
    
    1 回复  |  直到 3 年前
        1
  •  0
  •   mWin    3 年前

    可以将属性传递给shortcode:

    function day_trips_blogs( $atts ) {
    
        $atts = shortcode_atts( array(
            'my_category' => 'default-category',
        ), $atts, 'day-trips-posts' );
    
        ob_start();
    
        $the_query = new WP_Query( 
            array( 
                'category_name' => $atts['my_category']
                'post_status' => 'publish',
                'orderby'=> 'date', 
                'order' => 'DESC' 
            ) 
        ); 
    

    其余的代码可以保持不变,但我会在ob_start之前推送query,以便只在缓冲区内进行渲染。

    那么您将称之为:

    [一日游发布my_category=“foo”]