Adding category title to WordPress theme file - php

In my archives.php file in my custom WordPress theme, I have the following code:
<div class="page-title">
<?php
if ( is_day() ) :
printf( __( 'Daily Archives: %s', 'sturd' ), '<span>' . get_the_date() . '</span>' );
elseif ( is_month() ) :
printf( __( 'Monthly Archives: %s', 'sturd' ), '<span>' . get_the_date( _x( 'F Y', 'monthly archives date format', 'sturd' ) ) . '</span>' );
elseif ( is_year() ) :
printf( __( 'Yearly Archives: %s', 'sturd' ), '<span>' . get_the_date( _x( 'Y', 'yearly archives date format', 'sturd' ) ) . '</span>' );
else :
_e( 'Archives', 'sturd' );
endif;
?>
</div>
I want there to be another statement so that if this is a category page, it displays just the category title in the page title div, and not just the word Archives (as it does currently).
I hope this is posted in the correct section.
Thanks in advance.
Sam

First of all, I suggest you get aquainted with the WP template hierarchy. I would usually suggest using a separate category.php template file for displaying categories, and using single_cat_title() for displaying the category name.
If you want to use the main archive.php template, you can add an is_category() clause and then use single_cat_title() to get the name.

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

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

Woocommerce display taxonomy title in current taxonomy archive page

So, I have in the menu a link to a taxonomy archive that shows every product associated with that taxonomy.
Wordpress shows the correct page-title in category/tag archive, instead it shows the latest product title in the taxonomy page, that is nonsense.
I know that there's something about archive.php
elseif ( is_category() )
$title = sprintf( __( 'Category Archives: %s', 'mirage' ), '<span>' . single_cat_title( '', false ) . '</span>' );
elseif ( is_tag() )
$title = sprintf( __( 'Tag Archives: %s', 'mirage' ), '<span>' . single_tag_title( '', false ) . '</span>' );
elseif ( is_day() )
$title = sprintf( __( 'Daily Archives: %s', 'mirage' ), '<span>' . get_the_date() . '</span>' );
I tried to add an is_tax() etc. but nothing seems to reflect on the frontend.
I need to get the current taxonomy name and put it in the same page title.
Hope I made it clear. Thanks.
You can use:
elseif ( is_product_taxonomy() )
single_term_title();
endif;

Removing Date/Time/Author Name in WordPress Posts

I am currently developing a WordPress site and would like to know what to change in the template-tags.php file to remove the date, time and author name.
Here is the code which I believe I need to change although each time I do I am greeted with the dreaded screen of death.
if ( ! function_exists( 'makewp005_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time and author.
*/
function makewp005_posted_on() {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) )
$time_string .= '<time class="updated" datetime="%3$s">%4$s</time>';
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
printf( __( '<span class="posted-on">%1$s</span> <span class="byline"> %2$s</span>', 'makewp005' ),
sprintf( '%2$s',
esc_url( get_permalink() ),
$time_string
),
sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s">%2$s</a></span>',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_html( get_the_author() )
)
);
}
endif;
Thanks!
Why don't you use the CMS / API to remove them?
I believe this is the option to remove post info;
Larger: http://puu.sh/8rzux.png
So basically you want remove everything this function does.
if ( ! function_exists( 'makewp005_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time and author.
*/
function makewp005_posted_on() {}
endif;

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