1

Taking a clue from older WordPress stock themes I created a meta function to display information about each post.

function my_entry_meta() {
// Translators: used between list items, there is a space after the comma.
$categories_list = get_the_category_list( __( ', ', 'ritualHealing' ) );

// Translators: used between list items, there is a space after the comma.
$tag_list = get_the_tag_list( '', __( ', ', 'ritualHealing' ) );

$date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>',
    esc_url( get_permalink() ),
    esc_attr( get_the_time() ),
    esc_attr( get_the_date( 'c' ) ),
    esc_html( get_the_date() )
);

$author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
    esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    esc_attr( sprintf( __( 'View all posts by %s', 'ritualHealing' ), get_the_author() ) ),
    get_the_author()
);

//Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author's name.
?>
 <div class="blog_entry"> 
     <span class="author">Posted by --<a href="#" class="normaltip" title="Posted By">  <?php the_author(); ?></a></span>
     <span class="categories">-- Category --<?php __('%1$s') ?></span> 
     <span class="date">-- <?php __('%3$s') ?> --</span>
     <span class="comments"><a href="single_blog.html" class="normaltip" title="Comments"><?php comments_number( 'no responses', 'one response', '% responses' ); ?></a></span> 
     <span class="tags"><?php __('%2$s') ?></span>
 </div><?php

printf(
    $utility_text,
    $categories_list,
    $tag_list,
    $date,
    $author
);
 }
 endif;

For some reason, the category and the date doesn't display. The author name and comment number displays just fine. Am I missing something?

0

Well, it doesn't work for a very god reason.

<div class="blog_entry"> 
    <span class="author">Posted by --<a href="#" class="normaltip" title="Posted By">  <?php the_author(); ?></a></span>
    <span class="categories">-- Category --<?php __('%1$s') ?></span> 
    <span class="date">-- <?php __('%3$s') ?> --</span>
    <span class="comments"><a href="single_blog.html" class="normaltip" title="Comments"><?php comments_number( 'no responses', 'one response', '% responses' ); ?></a></span> 
    <span class="tags"><?php __('%2$s') ?></span>
</div>

This part of your code just doesn't make any sense at all. You try to output categories and dates by printing out format strings and not the values (<?php __('%1$s') ?> - where is the value in here? And to be more precise, you even don't print anything in there - __ function returns it's value and doesn't print anything).

Below you can find correct code

function my_entry_meta() {
    // Translators: used between list items, there is a space after the comma.
    $categories_list = get_the_category_list( __( ', ', 'ritualHealing' ) );

    // Translators: used between list items, there is a space after the comma.
    $tag_list = get_the_tag_list( '', __( ', ', 'ritualHealing' ) );

    $date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>',
        esc_url( get_permalink() ),
        esc_attr( get_the_time() ),
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() )
    );

    /* You don't use $author variable anywhere in your ouput (you use the_author tag instead), so it's redundant and should be deleted */
    $author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
        esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
        esc_attr( sprintf( __( 'View all posts by %s', 'ritualHealing' ), get_the_author() ) ),
        get_the_author()
    );

    // This comment is wrong - I'm guessing you copied this part of code from a theme that was using sptrinf/printf function to ouput this HTML?
    //Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author's name.
    ?>
    <div class="blog_entry"> 
        <span class="author">Posted by --<a href="#" class="normaltip" title="Posted By">  <?php the_author(); ?></a></span>
        <span class="categories">-- Category --<?php echo $categories_list; ?></span> 
        <span class="date">-- <?php echo $date; ?> --</span>
        <span class="comments"><a href="single_blog.html" class="normaltip" title="Comments"><?php comments_number( 'no responses', 'one response', '% responses' ); ?></a></span> 
        <span class="tags"><?php echo $tag_list; ?></span>
    </div><?php

    /* I'm pretty sure you don't need the printf call from next line */
    printf(
        $utility_text,
        $categories_list,
        $tag_list,
        $date,
        $author
    );
}

endif;

  • This is working. I took the code from the 2012 theme. I used it the same way in my theme. That is why I thought it would work. All I did was add my html. – Jamie Apr 19 '14 at 14:51
  • You've also changed the way it generated output - original function used printf with format strings. Your method prints HTML (and inside this HTML you tried to use format strings - it couldn't work like that). – Krzysiek Dróżdż Apr 19 '14 at 15:16

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.