Below is the following code in a pagenation function I am trying to understand the part I dont understand is the if(!pages){ $pages =1)}
what are they refering to. if what is not the $pages variable. What i am trying to say is what is the Pages variable being compared if not what. I am confused
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
Here is the full function, I am trying to understand it piece by piece so that I can rewrite without the numbers in the pagenation just have right and left.
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 what is not pages fun the code inside the if statement huh.
Your code "globalizes" the $wp_query
object so that it is accessible in the scope of the function. Then sets $pages
to $wp_query->max_num_pages
. Then checks that value to determine whether to execute the if
. How that works is a bit tricky but here you go.
PHP considers several things to be FALSE
If none of those match $pages
will be true, which would ordinarily trigger the if
to execute but the match is reversed with the !
. So, if $pages
is set to some true value then the if
condition is false
and the if
does not run-- leaving $pages
alone. If $pages
is set to some false
value, the if
condition is true
, it executes, and pages gets set to 1.
There are a couple of parts to this:
True
and False
are values that can be saved in variables. This type of value is called Boolean. Instead of if ($a == $b) { ... }
you can say $c = ($a == $b);
and then later do if ($c) { ... }
. This concept of boolean is common to most if not all programming languages. if
, or combine it with and
or or
) it will not be an error, and the variable will get converted using certain rules. Basically most things convert to true (they are "truthy"), apart from 0, NULL, Empty Arrays, Empty strings, and a few other things, which convert to false (they are "falsy"). So the if (!$pages) { $pages = 1; }
if (!$pages) { $pages = 1; }
in your example is saying if the number of pages is zero, make sure there is at least one. It's a shorter way of saying if ($pages == 0)
.
The condition checks if $pages
is 0
. The boolean values true
and false
are usually defined by the binaries 1
and 0
respectively. Thus, if(!$pages)
means "if the value of $pages
evaluates to false
(or 0
) then execute the condition body and set $pages
to 1
". Conversely, if($pages)
would mean execute the condition body if $pages > 0
.
I'm not sure what language this is, but it should clearly define what counts as "true" and "false" where boolean expressions are expected. For example, in C, any non-zero integer value is "true"; anything else is "false". Some languages will also define what string values are considered "true" or "false". This is exactly what the if statement in your code is doing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.