Title how does page no work in Twenty Eleven

OK, so…

sprintf( __( 'Page %s', 'twentyten' ), max( $paged, $page ) );

First off – sprintf. Returns a formatted string, using variables and a type specifier to display information.

For example,

$house = "Mansion";
$house_number = 49;
sprintf( 'My house is a %s and its number is %d', $house, $house_number );

Will print My house is a Mansion and its number is 49.

Like you see in the Twenty Eleven theme, you can add functions to these variables:

$house = "Mansion";
$house_number = 49;
sprintf( 'My house is a %s and its number is %d', strtolower( $house ), $house_number );

Will print My house is a mansion and its number is 49.


Now, in the code you want to understand, there are two variables: paged and page, which are global variables defined by other WordPress scripts. One of those variables will return the current page number in a set of results – i.e. if you’re on the fifth page of results, paged or page will be 5.

However, I think there has been some discrepancy in the past between which of these variables gets used. This is why the max function is being used – the theme is looking to see which of those variables are actually set, then it displays it. max simply looks at an array of values and returns the highest. In this case, only one of these global variables will be set, and as such the max function is a quick and easy way of seeing which one is being used.

sprintf( __( 'Page %s', 'twentyten' ), max( $paged, $page ) );

%s in this sprintf function indicates that the value of max( $paged, $page ) should be displayed as a string.

If $paged was 4, $page wouldn’t be set, so that line would display Page 4. Alternatively, if $page was being used, $paged wouldn’t be set.

The __( ) function is part of WordPress localization. Basically it allows for translations of your web-page to be written. This specific function simply returns the translated string; the _e( ) function actually echoes it. The twentyten string of this argument is the $domain. I won’t go into any more detail about that as I don’t actually fully understand it myself.

If you remove the localization function, you’ll see that the function works in exactly the same way:

sprintf( 'Page %s', max( $paged, $page ) );


Hope that helps – sorry for the “bitty” explanation.