AndreaNobili
AndreaNobili

Reputation: 42957

Some problems calling a function into sprintf()

I am new in PHP and WordPress and I am personalizing an existent template and I have the following problem.

In the function.php file of the template I have the following function:

function admired_posted_on() {
    printf( __( '<span class="sep">Posted on </span>
                 <a href="%1$s" title="%2$s" rel="bookmark">
                    <time class="entry-date" datetime="%3$s" pubdate>%4$s</time>
                 </a>
                 <span>%5$s</span>
                 <span class="by-author"> 
                    <span class="sep"> by bla</span> 
                    <span class="author vcard">
                        <a class="url fn n" href="%6$s" title="%7$s" rel="author">%8$s</a>
                    </span>
                 </span>
                 ', 'admired' ),

    esc_url( get_permalink() ),
    esc_attr( get_the_time() ),
    esc_attr( get_the_date( 'c' ) ),
    esc_html( get_the_date() ),
    sprintf('Views: ', get_PostViews(get_the_ID())),
    esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    sprintf( esc_attr__( 'View all posts by %s', 'admired' ), get_the_author() ),
    esc_html( get_the_author() )
    );
}

As you can see this function create some HTML code that will print in a specific part of my template (inside a loop).

Ok, as you can see this line:

sprintf('Views: ', get_PostViews(get_the_ID())),

should print the string "Views:" followed by the value returned by the function get_the_ID() (that rappresent the number of person that read a post)

As you can see this function is the fifth called in the list of called function, so this value should be put instead of the %5$s placeholder, into the following span tag:

<span>%5$s</span>

The proble is that whem I go to execute my page in this span appear only the value: Views: but don't appear the output of the get_PostViews() function.

If instead of the original line:

sprintf('Views: ', get_PostViews(get_the_ID())),

I use this line:

sprintf(get_PostViews(get_the_ID())),

it work well but so I can't preappend the explaination text: "Views:"

Why? what can I do to print the "Views" text followed by the returned value of my get_PostViews function?

Tnx

Andrea

Upvotes: 0

Views: 1250

Answers (2)

Robert Lee
Robert Lee

Reputation: 1561

As hank has mentioned you have missed the placeholder inside your text.

You can do this as well so that you can use that same placeholder multiple times within the text.

sprintf('Views: %1$d', get_PostViews(get_the_ID()));

Upvotes: 0

hank
hank

Reputation: 3768

sprintf('Views: %d', get_PostViews(get_the_ID()))

The first argument to sprintf should contain placeholders for the following arguments, the "%d" format tells sprintf that this should be a integer

More about this method here

Upvotes: 1

Related Questions