Search code examples
phpwordpresscodex

What does the value of '%5$s' mean within Wordpress category listing?


I'm currently editing a Wordpress multisite to remove/repair a broken link. The index page lists all the posts from all of the child blogs, as tiles, on the homepage. The link that is applied to the author's name is incorrect.

I've tracked it down and within content-single.php page there is some code generated to retrieve the data and the value is %5$s

I thought it might be a regex expression but I can't confirm. The value looks more like a shorthand expression for retrieving a variable from the database. Did a google search and still can't find out. I came across this codex page https://codex.wordpress.org/Function_Reference/get_the_category_list

I still can't find out what the values mean. What do they mean?

So I did a traceback on the PHP files within the theme. First, I saw this function in the 'content-single.php' page that generates the content with the bad links in it <?php slqblogs_posted_on(); ?>

<div class="entry-meta">
    <?php slqblogs_posted_on(); ?>
    <span class="sep">|</span>
    <!-- AddThis Button BEGIN -->
    <div class="addthis_toolbox addthis_default_style ">
    <a href="http://www.addthis.com/bookmark.php?v=250&amp;pubid=ra-4f836b545f19f0e0" class="addthis_button_compact">Share</a>
    </div>
    <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4f836b545f19f0e0"></script>
    <!-- AddThis Button END -->
    <div class="clear"></div>
</div><!-- .entry-meta -->

So obviously, I went looking in the 'theme-functions.php' file and I can see the function in all it's glory:

if ( ! function_exists( 'slqblogs_posted_on' ) ) :
/**
 * Prints HTML with meta information for the current post-date/time and author.
 * Create your own slqblogs_posted_on to override in a child theme
 *
 * @since slqblogs 1.2
 */
function slqblogs_posted_on() {
    printf( __( '<span class="posted-on"><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date published" datetime="%3$s" pubdate>%4$s</time><time class="updated">%8$s</time></a></span><span class="sep">|</span><span class="byline"> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>', 'slqblogs' ),
        esc_url( get_permalink() ),
        esc_attr( get_the_time() ),
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
        esc_attr( sprintf( __( 'View all posts by %s', 'slqblogs' ), get_the_author() ) ),
        esc_html( get_the_author() ),
        esc_attr( get_the_modified_date() )
    );
}
endif;

So this is where I come across the value in the span class of byline:

<span class="byline"> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>

Just can't figure out how to access the values of %5$s or the other keywords/expressions or whatever they are.

I'll be honest, this kind of Wordpress PHP scripting is totally out of my depth and comfort zone. Any assistance would be greatly appreciated.


Solution

  • It's for "swapping" arguments in a sprintf/printf format descriptor, i.e. you can access the parameters in whatever order you like, e.g.

    <?php
    $p1 = 'a';
    $p2 = 'b';
    $p3 = 'c';
    $p4 = 'd';
    
    printf('%1$s %2$s %3$s %4$s'."\r\n", $p1, $p2, $p3, $p4);
    printf('%4$s %3$s %2$s %1$s'."\r\n", $p1, $p2, $p3, $p4);
    

    prints

    a b c d
    d c b a
    

    even though the parameters p1-p4 have been passed in the same order for both printfs. see also: http://docs.php.net/manual/en/function.sprintf.php#example-5403