简体   繁体   中英

Adding html inside sprintf

I have some code

'next_text' => sprintf( esc_html__( '%s', 'mytheme' ), '%title' ),

and I need to add html inside it. Basically I want to add

<span>Previous article</span>

before the title. How can I do this?

EDIT: The full function is

function mytheme_single_post_navigation() {
if ( ! is_singular( 'post' ) ) {
    return;
}

if ( ! intval( mytheme_get_theme_mod( 'blog_single_navigation' ) ) ) {
    return;
}

the_post_navigation( array(
    /* translators: %s: title syntax. */
    'prev_text' => sprintf( esc_html__( '%s', 'mytheme' ), '%title' ),

    /* translators: %s: title syntax. */
    'next_text' => sprintf( esc_html__( '%s', 'mytheme' ), '%title' ),
) );
}
endif;

If you want to edit the wordpress post navigation in the single.php of your posts, you can use:

<span> <?php previous_post_link( '%link', '‹ Previous article' ); ?> </span>
<span> <?php next_post_link( '%link', 'Next article ›' ); ?> </span>

If you want to make it with setting 'next_text', you can also put your outpout in a string and use this:

<?php $next_post = '<span>Previous article</span>'; ?>

and then assign it to the 'next_text':

'next_text' => $next_post

If you read sprintf() function https://www.w3schools.com/php/func_string_sprintf.asp you can add html tags with no problem. So it could also work with:

'next_text' => sprintf( esc_html__( '%s', 'mytheme' ), '<span> %title </span>' ),

But you do not want to have the title, so you don't actualy need this, if I got your question right.


EDIT:

If you want to show the span before the name of your post, you can use:

<?php previous_post_link( '%link', '<span>Previous article</span>'.' %title' ); ?>

With the dot. you are connecting the span element with the title.


So in your code, your the_post_navigation can look like:

the_post_navigation( array(
    /* translators: %s: title syntax. */
    'prev_text' => sprintf( esc_html__( '%s', 'mytheme' ), '<span>Previous article</span>'.' %title' ),

    /* translators: %s: title syntax. */
    'next_text' => sprintf( esc_html__( '%s', 'mytheme' ), '<span>Next article</span>'.' %title' ),
) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM