Adding html inside sprintf - php

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' ),
) );

Related

Change Wordpress date format for comments in Astra

my problem is very simple but I am not familiar with PHP. My goal is to change the date format for the comments in my Wordpress blog from "26th of February 2021 at 12:23" to "43 minutes ago".
I also found a code snippet that does this for me, but it doesn't render the markup like the code which is provided by the theme itself.
This is the code provided by Astra:
printf(
'<div class="ast-comment-time ast-col-lg-12"><span class="timendate"><time datetime="%2$s">%3$s</time></span></div>',
esc_url( get_comment_link( $comment->comment_ID ) ),
esc_attr( get_comment_time( 'c' ) ),
/* translators: 1: date, 2: time */
esc_html( sprintf( __( '%1$s at %2$s', 'astra' ), get_comment_date(), get_comment_time() ) ),
);
This is the code I found:
printf( _x( '%1$s ago', '%2$s = human-readable time difference', 'wpdocs_textdomain' ),
human_time_diff( get_comment_time( 'U' ),
current_time( 'timestamp' )
);
I'd be more than happy if someone could help me with this since I don't know where to search for a solution anymore...
Thank you all!
This revision to your code may work as desired:
printf(
'<div class="ast-comment-time ast-col-lg-12">
<span class="timendate"><time datetime="%2$s">%3$s</time>
</span>
</div>',
esc_url( get_comment_link( $comment->comment_ID ) ),
esc_attr( get_comment_time( 'c' ) ),
esc_html( __( human_time_diff( get_comment_time( 'U' ) ) . ' ago', 'astra' ) )
);

WordPress: Remove get_the_title from my content template

I'm running a variation of the _underscores theme for my clients site and we already have the page or post title displayed in a custom header element, so it doesn't need to be inside of the loop anymore on template-parts/content.php. I thought just deleting 'get_the_title' would eliminate seeing the title in the body of my post, but instead, I just got a variety of errors like 'unexpected ')'' or similar. So how do I get rid of the get_the_title reference and still make this valid? Here's what I have currently.
<div class="entry-content">
<?php if ( is_category() || is_archive() ) {
the_excerpt('');
} else {
the_content( sprintf(
wp_kses(
/* translators: %s: Name of current post. Only visible to screen readers */
__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'orchestra' ),
array(
'span' => array(
'class' => array(),
),
)
),
get_the_title()
) );
if ( is_category() || is_archive() ) {
echo '<p class="btn-cc">Read More</p>';
}
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'orchestra' ),
'after' => '</div>',
) );
}
?>
The formatting on this makes it relatively hard to read. So first I'd clean that up a bit. If you look at the documentation for the_content() you'll see that the first parameter is the $more_text_link. So lines 5 through 15 are adding a "Continue Reading [Post Title]" test.
If you don't need that at all, you can just use the_content() like so:
<div class="entry-content">
<?php
if( is_category() || is_archive() ){
the_excerpt('');
} else {
the_content();
if( is_category() || is_archive() ){
echo '<p class="btn-cc">Read More</p>';
}
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'orchestra' ),
'after' => '</div>',
) );
}
?>
Otherwise, you'll want to add in your own default text:
<div class="entry-content">
<?php
if( is_category() || is_archive() ){
the_excerpt('');
} else {
the_content( 'Continue Reading' );
if( is_category() || is_archive() ){
echo '<p class="btn-cc">Read More</p>';
}
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'orchestra' ),
'after' => '</div>',
) );
}
?>

How to resolve Strings should have translatable content in Wordpress?

I am trying to fix the linting issue here is code.
function get_the_breadcrumb() {
if ( ! is_front_page() ) {
// Start the breadcrumb with a link to your homepage.
echo '<div class="o__breadcrumb">';
echo '<a href="';
echo esc_html( get_option( 'home' ) );
echo '"> Home';
echo '</a> <span> ';
echo esc_html( Load::atom( 'icons/breadcrumb_arrow' ) );
echo '</span>';
// Check if the current page is a category, an archive or a single page. If so show the category or archive name.
if ( is_category() || is_single() ) {
the_category( 'title_li=' );
} elseif ( is_archive() || is_single() ) {
if ( is_day() ) {
/* translators: %s: text term */
printf( esc_html( __( '%s', 'text_domain' ) ), esc_html( get_the_date() ) );
} elseif ( is_month() ) {
/* translators: %s: text term */
printf( esc_html( __( '%s', 'text_domain' ) ), get_the_date( _x( 'F Y', 'monthly archives date format', 'text_domain' ) ) );
} elseif ( is_year() ) {
/* translators: %s: text term */
printf( esc_html( __( '%s', 'text_domain' ) ), get_the_date( _x( 'Y', 'yearly archives date format', 'text_domain' ) ) );
} else {
esc_attr_e( 'Blog Archives', 'text_domain' );
}
}
// If the current page is a single post, show its title with the separator.
if ( is_single() ) {
echo '<span>';
echo esc_html( Load::atom( 'icons/breadcrumb_arrow' ) );
echo '</span>';
the_title();
}
// If the current page is a static page, show its title.
if ( is_page() ) {
echo the_title();
}
// if you have a static page assigned to be you posts list page. It will find the title of the static page and display it. i.e Home >> Blog.
if ( is_home() ) {
global $post;
$page_for_posts_id = get_option( 'page_for_posts' );
if ( $page_for_posts_id ) {
$post = get_page( $page_for_posts_id );
setup_postdata( $post );
the_title();
rewind_posts();
}
}
echo '</div>';
}
}
Linting response
FOUND 3 ERRORS AFFECTING 3 LINES
----------------------------------------------------------------------
193 | ERROR | Strings should have translatable content
196 | ERROR | Strings should have translatable content
199 | ERROR | Strings should have translatable content
Line number 193
printf( esc_html( __( '%s', 'text_domain' ) ), esc_html( get_the_date() ) );
Line number 196
printf( esc_html( __( '%s', 'text_domain' ) ), get_the_date( _x( 'F Y', 'monthly archives date format', 'text_domain' ) ) );
Line number 199
printf( esc_html( __( '%s', 'text_domain' ) ), get_the_date( _x( 'Y', 'yearly archives date format', 'text_domain' ) ) );
It's because you have %s as the text inside the translation function call __(...), which is not translatable.
Instead you should have your printf() call inside the translation call, so the translator can actually see what the heck you're trying to translate.
But, you shouldn't be trying to translate the date this way. It won't work because the date is always changing and the way __() translation works is by matching the exact string passed in to a translation. According to this answer, you should use date_i18n
And why are you trying to translate the date formatting strings you are passing to get_the_date, those are code values used by php, they don't change based on where you are. Translating these can only cause you problems.
You also call esc_html twice on line 193.
So, instead you should write your lines of code like so:
Line number 193
esc_html( date_i18n( get_the_date() ) );
Line number 196
esc_html( date_i18n( get_the_date('F Y') ) );
Line number 199
esc_html( date_i18n( get_the_date( 'Y' ) ) );
Note, I don't think the esc_html calls are actually necessary here, since the internals is just WordPress functions that only return dates... no html should be in there

Wordpress - Add post Title to entry_meta function

I would like to add the post title to the entry_meta() function in the TwentySixteen Wordpress theme, in the template-tags.php file. I would like it to print before the author info. but after the avatar image.
basically just before the "screen-reader-text" span.
The function part:
if ( 'post' === get_post_type() ) {
$author_avatar_size = apply_filters( 'twentysixteen_author_avatar_size', 205 );
printf( '<span class="byline"><span class="author vcard">%1$s<span class="screen-reader-text">%2$s </span> <a class="url fn n" href="%3$s">%4$s</a></span></span>',
get_avatar( get_the_author_meta( 'user_email' ), $author_avatar_size ),
_x( 'Author', 'Used before post author name.', 'twentysixteen' ),
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
get_the_author()
);
}
Adding the_title() in the printf doesn't really work. So not sure how to approach this.
Thank you for anyone thats willing to help.
Figured it out.
added the get_the_title() and called it in with %5$s
if ( 'post' === get_post_type() ) {
$author_avatar_size = apply_filters( 'twentysixteen_author_avatar_size', 205 );
printf( '<span class="byline"><span class="author vcard">%1$s<h1 class="entry-title">%5$s</h1><span class="screen-reader-text">%2$s </span> <a class="url fn n" href="%3$s">%4$s</a></span></span>',
get_avatar( get_the_author_meta( 'user_email' ), $author_avatar_size ),
_x( 'Author', 'Used before post author name.', 'twentysixteen' ),
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
get_the_author(),
get_the_title()
);
}

WordPress: how do you detach the link from post date?

How do I detach the link from the code below in my (child) functions.php? I want my dates to display as regular text.
<?php
if ( ! function_exists( 'twentyeleven_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time and author.
* Create your own twentyeleven_posted_on to override in a child theme
*
* #since Twenty Eleven 1.0
*/
function twentyeleven_posted_on() {
printf( __( '<time class="entry-date" datetime="%3$s" pubdate>%4$s</time><span class="by-author"> <span class="sep"> by </span> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>', 'twentyeleven' ),
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' ) ) ),
sprintf( esc_attr__( 'View all posts by %s', 'twentyeleven' ), get_the_author() ),
esc_html( get_the_author() )
);
}
endif;
?>
Thanks
are you just trying to get the date of a post? Use the function: the_date('y-m-d');
The string parameter denotes how you want the date to be displayed. You can learn more about this function at the Wordpress Codex

Categories