我不理解TwentySeventeen WordPress主题中这一部分的逻辑

时间:2017-02-02 00:53:02

标签: php wordpress wordpress-theming

我正在学习WordPress,并通过一些默认主题来尝试并了解它们的工作原理。我在TwentySeventeen主题中看到了这一部分(二十七年/ template-parts / page / content-front-page.php)并且让我困惑:

<?php if ( has_post_thumbnail() ) :
        $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'twentyseventeen-featured-image' );

        $post_thumbnail_id = get_post_thumbnail_id( $post->ID );

        $thumbnail_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'twentyseventeen-featured-image' );

        // Calculate aspect ratio: h / w * 100%.
        $ratio = $thumbnail_attributes[2] / $thumbnail_attributes[1] * 100;
        ?>

        <div class="panel-image" style="background-image: url(<?php echo esc_url( $thumbnail[0] ); ?>);">
            <div class="panel-image-prop" style="padding-top: <?php echo esc_attr( $ratio ); ?>%"></div>
        </div><!-- .panel-image -->

<?php endif; ?>

我仍然是一名新手程序员,所以也许我错过了一些明显的东西,但我不明白为什么会发生以下两件事:

  1. 为什么创建$ post_thumbnail_id变量但从未使用过?为什么不马上创建它而不是经常调用get_post_thumbnail_id($ post-&gt; ID)?
  2. $ thumbnail和$ thumbnail_attributes是一样的,对吗?那么为什么要创建两者?
  3. 这不会更有意义吗?

    <?php if ( has_post_thumbnail() ) :
    
            $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
    
            $thumbnail = wp_get_attachment_image_src( $post_thumbnail_id, 'twentyseventeen-featured-image' );
    
            // Calculate aspect ratio: h / w * 100%.
            $ratio = $thumbnail[2] / $thumbnail[1] * 100;
            ?>
    
            <div class="panel-image" style="background-image: url(<?php echo esc_url( $thumbnail[0] ); ?>);">
                <div class="panel-image-prop" style="padding-top: <?php echo esc_attr( $ratio ); ?>%"></div>
            </div><!-- .panel-image -->
    
    <?php endif; ?>
    

    我创建了一个子主题,并使用上面的代码而不是原来的代码,一切正常。我想我只是想知道为什么原始开发人员会以这种方式编写代码,而且从学生的角度来看,我想要一些见解。

1 个答案:

答案 0 :(得分:0)

原始开发者可以做到:

$thumbnail_attributes = $thumbnail;

您必须向模块开发人员询问为什么没有这样做。我把它写成脑屁。

相关问题