PHP是否可以在循环内重复相同的功能?

时间:2019-12-09 20:04:46

标签: php

页面加载23到30秒。如果在循环内调用了函数“ thim_feature_image”。如果从循环中删除该功能,则页面加载非常快。因此,我想必须对此功能做一些事情。我需要你的帮助。我猜是因为在同一页面上重复了很多次该功能。我希望除了创建新功能之外,还有其他快速修复方法。

代码如下:

//在循环内调用函数“ thim_feature_image”的页面,代码如下:

<?php
        while ( $menus->have_posts() ) {
            $menus->the_post();
            $postdate      = get_the_time( 'Y-m-d' );
            $postdatestamp = strtotime( $postdate );

            $label         = '';
            $label_options = get_post_meta( get_the_id(), 'menu_recommended' );
            if ( ( ( time() - ( 60 * 60 * 24 * $day_new ) ) < $postdatestamp ) && $day_new != 0 ) {
                $label = '<span class="label">' . esc_html__( 'New', 'nem' ) . '</span>';
            } elseif ( $label_options ) {
                $label = '<span class="label">' . esc_html__( $label_options[0], 'nem' ) . '</span>';
            }
            ?>
            <div class="item">
                <div class="inner image-<?php echo esc_attr( $instance['show_image'] ); ?>">
                    <?php if ( $instance['show_image'] == 'true' ) : ?>
                        <div class="image">
                          <a <?php echo thim_feature_image_link( auto, auto, false ); ?>">
                            <?php

                            if ( $show_link === 'true' || $show_link === '1' ) {
                                thim_feature_image( 70, 70, true );
                            } else {
                                echo thim_feature_image( 70, 70, false );
                            }
                            ?>
                            </a>
                        </div>
                    <?php endif; ?>

                </div>
            </div>

            <?php
            $i ++;
        }
        ?>

//函数“ thim_feature_image”存储在一个单独的文件中,它看起来像:

/********************************************************************
 * Get image attach
 ********************************************************************/
function thim_feature_image( $width = 1024, $height = 768, $link = true ) {
    global $post;
    $thumbnail = get_the_post_thumbnail( $post->ID, 'full' );
    if ( $thumbnail ) {
        $thumbnail_src = simplexml_load_string( $thumbnail )->attributes()->src;
        if ( $thumbnail_src ) {
            $img_url     = explode( '?', $thumbnail_src[0] );
            $data        = @getimagesize( $img_url[0] );
            $width_data  = $data[0];
            $height_data = $data[1];
            if ( $link ) {
                if ( ( $width_data <= $width ) || ( $height_data <= $height ) ) {
                    echo '<div class="thumbnail"><a href="' . esc_url( get_permalink() ) . '" title = "' . get_the_title() . '">';
                    echo '<img src="' . $img_url[0] . '" alt= "' . get_the_title() . '" title = "' . get_the_title() . '" />';
                    echo '</a></div>';
                } else {
                    $image_crop = aq_resize( $img_url[0], $width, $height, true );
                    echo '<div class="thumbnail"><a href="' . esc_url( get_permalink() ) . '" title = "' . get_the_title() . '">';
                    echo '<img src="' . $image_crop . '" alt= "' . get_the_title() . '" title = "' . get_the_title() . '" />';
                    echo '</a></div>';
                }
            } else {
                if ( ( $width_data <= $width ) || ( $height_data <= $height ) ) {
                    return '<img src="' . $img_url[0] . '" alt= "' . get_the_title() . '" title = "' . get_the_title() . '" />';
                } else {
                    $image_crop = aq_resize( $img_url[0], $width, $height, true );

                    return '<img src="' . $image_crop . '" alt= "' . get_the_title() . '" title = "' . get_the_title() . '" />';
                }
            }
        }
    }
}

0 个答案:

没有答案