0

I have the following code in one of the PHP files in my WordPress site:

<?php if ( count( get_the_category() ) ) : ?>
        <span class="cat-links">
            <?php printf( __( '<span class="%1$s">&bull; Posted in</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
        </span>
    <?php endif; ?>

What it does is output a comma-separated, linked list of all the categories that a specific post belongs to. For example, the output looks like this:

Posted in Category1, Category2, Category3

What I want to do is modify the beginning of this line to also include the author. According to the WordPress documentation, the author name can be retrieved using:

get_the_author()

But I'm not familiar with PHP syntax well enough to figure out how to add this to what I've got. Here's how I want it to read instead:

Posted by author_username in Category1, Category2, Category3

Any help would be greatly appreciated!

2 Answers 2

4

Try this,

 <?php printf( __( '<span class="%1$s">&bull; Posted by %2$s in</span> %3$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_author(), get_the_category_list( ', ' ) ); ?>
Sign up to request clarification or add additional context in comments.

7 Comments

This works perfectly - thanks so much! One last question...it prints the author name, but doesn't link it like it links the Categories...how would I go about having it do that?
You can use get_the_author_link() instead of get_the_author()
It seems like that should work, but I still can't get it to link the author name. Does it have anything to do with it being inside an if statement? Here's what I've got now (I used your suggestion to also add the date to the post line) <?php printf( __( '<span class="%1$s">&bull; Posted on %2$s by %3$s in</span> %4$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_date(), get_the_author_link(), get_the_category_list( ', ' ) ); ?>
What is the output you geeting in browser.? Which version of Wordpress you have using?
The output looks exactly correct, aside from the fact that only the category titles are being linked (not the author). I'm using the latest version of WP, I believe it's 3.8.
|
0
 <?php printf( __( '<span class="%1$s">&bull;Posted By ' . get_the_author() . '  Posted in</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>

1 Comment

Will work, but defeats the object of using printf (which is to separate language and variables, in this case for internationalization)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.