PHP功能不能按预期工作
问题描述:
<?php
function getPosts($showposts,$tags, $thumb_key="thumb_300x166", $thumb_class, $thumb_width="300", $thumb_height="166") {
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('tag=$tags&showposts=$showposts');
while ($wp_query->have_posts()) {
$wp_query->the_post();
echo '<div class="entry"><div class="left">';
if (function_exists('get_the_image')) {
$defaults = array(
'custom_key' => array('$thumb_key'),
'image_class' => '$thumb_class',
'image_scan' => true,
'width' => '$thumb_width',
'height' => '$thumb_height'
);
get_the_image($defaults); // thumbnail
} // end if
echo '</div>
<div class="right">
<h3><a href="'.the_permalink().'">'.the_title().'</a></h3>'
.the_excerpt().'</div></div>';
} // end while
}
getPosts($showposts=5,$tags="news",$thumb_class="review-thumb");
?>
我不明白为什么这个wordpress查询功能不起作用。我根本没有返回/打印任何东西。PHP功能不能按预期工作
答
我从来没有使用Wordpress,但我看到一个问题可能是这个原因。
这是,如果你使用单引号,如以下行:
$wp_query->query('tag=$tags&showposts=$showposts');
$tags
和$showposts
不被处理,并插入到字符串字面。如果你希望你的字符串包含的$tags
和$showposts
值,使用双引号,像这样:
$wp_query->query("tag=$tags&showposts=$showposts");
同样为传递给get_the_image
阵列。
编辑:另外,你的函数调用看起来很奇怪。您正在使用类似的语法,当你的论点提供了默认值,但普通函数调用会是这个样子:
getPosts(5, "news", "review-thumb");
我们为什么不永远得到的问题,其中的功能** **是按预期工作...... – 2010-02-07 15:31:35
..因为没有人想在功能**工作时寻求帮助。 – 3zzy 2010-02-07 16:47:08