• Resolved Guido

    (@guido07111975)


    Hi guys and girls,

    I’m updating a theme and came across unclear postmetadata code to display post author + link to the post author archive page:

    <?php printf( __( 'By %s', 'mytextdomain' ), sprintf( '<a href="%1$s">%2$s</a>', esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), esc_html( get_the_author() ) ) ); ?>

    So I worked on it and think this is much better:

    <?php printf( __( 'By %s', 'mytextdomain' ), '<a href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ). '</a>' ); ?>

    Any thoughts? Better this way?

    Guido

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Guido

    (@guido07111975)

    Wait, I understand the ‘unclear’ postmetadata:

    %1$s =

    esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) )

    %2$s =

    esc_html( get_the_author() )

    Not so unclear anymore ??

    But what’s the best of those 2?

    Guido

    Moderator bcworkz

    (@bcworkz)

    IMO, it doesn’t matter much in this example. Whichever is clearer to you is best because you are the one who will maintain your code.

    For strings, sprintf() has no clear advantage over simple concatenation, though it’s important to understand its use because similar constructs are required for things like $wpdb->prepare(). When it comes to a mix of strings and numbers sprintf() has the advantage because you can specify the numeric format desired. Using it means several rows of numeric data will remain with columns neatly aligned. Concatenating real numbers will likely be a complete mess in comparison.

    Thread Starter Guido

    (@guido07111975)

    Hi BC,

    Thanks again! I stay with the printf version, now I fully understand how it works. Besides this it’s commonly used in other themes as well.

    Guido

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Unclear postmetadata’ is closed to new replies.