我在 wordpress if(empty($paged)) $paged = 1;
的函数中有以下行,我不明白为什么它没有开头 {
和结束 }
。是不是自开自闭,不知道是不是一个名词
这是我正在使用的函数
<?php
function kriesi_pagination($pages = '', $range = 2)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class='pagination'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
}
?>
最佳答案
如果你想在 if
条件之后运行的代码只有一行,你可以省略括号。我觉得这很困惑,所以我不这样做,但 it is valid PHP .
那么,您的代码的作用是:
if(empty($paged)) $paged = 1;
这也是:
if(empty($paged))
$paged = 1; // semi-colon marks the end of the line
这不会:
if(empty($paged)) $paged = 1; $othervar = 2;
这也不会:
if(empty($paged))
$paged = 1;
$othervar = 2;
$othervar
,在这两种情况下,都会在条件之外“执行”——即,它总是被设置为 2。
这里还有一个线程:PHP - If/else, for, foreach, while - without curly braces?
关于php - 不明白为什么 if 语句没有 {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14790778/