I would like to understand how does twenty eleven title page no works.. sprintf( __( 'Page %s', 'twentyten' ), max( $paged, $page ) );

I dont understand the above part as in the __() function the arguments are $text and $domain what is $domain and how does %s print's no. Below is the whole code of title thank you

 * Print the <title> tag based on what is being viewed.
global $page, $paged;

wp_title( '|', true, 'right' );

// Add the blog name.
bloginfo( 'name' );

// Add the blog description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) )
    echo " | $site_description";

// Add a page number if necessary:
if ( $paged >= 2 || $page >= 2 )
    echo ' | ' . sprintf( __( 'Page %s', 'twentyten' ), max( $paged, $page ) );

  • can you explain in more detail what the problem is? – Michael Apr 4 '12 at 11:16
  • Hi Michael, Actually there is no problem I wanted to understand How twentyten & twentyeleven are adding page number and why twentyten was being passed in __() But Thanx to Dunc I understood. – tjrocks Apr 4 '12 at 12:26

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.

  • Hi Thanx For explaining but why there is twentyten written since I am making my own theme should that be my themename instead of twentyten... sprintf( 'Page %s', 'twentyten', max( $paged, $page ) ); – tjrocks Apr 4 '12 at 12:19
  • Hi I understood after reading 3 to 5 times sorry for being ignorant thanx for explaining... – tjrocks Apr 4 '12 at 12:23
  • Sorry tjrocks - I've just realised my last function was wrong, which might've made things confusing. twentyten in that example is simply the $domain argument for the localization function. I'll correct my post now :) – dunc Apr 4 '12 at 15:28
  • No problem got that things sorted...Thanx...by your explaination lots of things got clear... – tjrocks Apr 13 '12 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.