使用php使数组动态


Making an array dynamic with php

我正在开发一个基于Wordpress的网站,单个帖子模板使用下面的php行调用特色图像。

<?php $img = array('class' => 'enigma_img_responsive', 'alt' => 'the alt', 'title' => 'the title' );
    if(has_post_thumbnail()): 
    the_post_thumbnail('home_post_thumb',$img);
endif; ?>

我正试图让这段代码从媒体库中为所有特色图像动态提取alt和title。根据我所看到的,您不能在数组中使用其他php,我可以在其中列出属性和值。我尝试过以下方法作为一种潜在的解决方案,但它不起作用。

    <?php $img = array('class' => 'enigma_img_responsive', 'alt' => 'featured imag for the_title();', 'title' => 'the_title();' );
    if(has_post_thumbnail()): 
    the_post_thumbnail('home_post_thumb',$img);
endif; ?>

上面代码的输出实际上在alt和title属性中显示了"The_title();"。

有没有解决办法,似乎找不到。我想是因为WordPress很受欢迎。

Vincent G.在评论中给出的解决方案是将the_title()替换为get_the_title()