我创建了一个函数来获取我的帖子缩略图和后退图像。
<?php
function png_thumb($class=null,$thumbsize=null,$no_thumb,$imgclass=null,$extras=null,$hover_content=null){
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => $imgclass
); ?>
<div class="<?php echo $class ?>">
<a href="<?php the_permalink(); ?>" title="<?php //the_title(); ?>">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail($thumbsize, $title_attr);
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/<?php echo $no_thumb ?>" alt="<?php the_title(); ?>" class="<?php echo $imgclass; ?>" <?php echo $extras; ?> />
<?php } ?>
</a>
<?php if($hover_content != "") { ?>
<a href="<?php the_permalink(); ?>"><div class="hovereffect"><?php echo $hover_content; ?></div></a>
<?php } ?>
</div>
<?php } ?>
但我相信传递阵列会比这更好。但我不知道如何创建可以通过预定义键传递的功能。与$ title_attr分配的array()相同。或者wordpress $ args如何运作。
答案 0 :(得分:5)
“使用预定义键传递数组”不是PHP理解的概念。你可以这样做:
function png_thumb(array $args = array()) {
$args += array('class' => null, 'thumbsize' => null, 'no_thumb' => null, 'imgclass' => null, 'extras' => null, 'hover_content' => null);
echo $args['class'];
...
此函数接受一个数组,并填充所有未使用默认值传递的键。你使用它像:
png_thumb(array('thumbsize' => 42, ...));
答案 1 :(得分:4)
你也可以尝试这个
function png_thumb($args=array()) {
$default= array('class' => null, 'thumbsize' => null, 'no_thumb' => null, 'imgclass' => null, 'extras' => null, 'hover_content' => null);
$settings=array_merge($default,$args);
extract($settings); // now you can use variables directly as $class, $thumbsize etc, i.e
echo $class; // available as variable instead of $settings['class']
echo $thumbsize; // available as variable instead of $settings['thumbsize']
...
}