I am editing code in template tags to show the last modified date on my WordPress website, what I mean is if I update the post, it should conditionally show update and ignore "Published", and if I do not, it should leave as is.
I got it to work, the problem I have is "Published On" appearing on all my post: http://prntscr.com/nn9hfl, this is my code so far
function chicken_wings_posted_on() {
/**
* Function to show last updated date
*/
$u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 86400) {
echo '<p class = "last-updated-up">Last updated on ';
the_modified_time('F jS, Y');
echo "</p> "; }
else {
echo '<p class = "entry-date published">Published on ';
the_time('F jS, Y');
echo "</p> "; }
$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() )
);
$posted_on = sprintf(
/* translators: %s: post date. */
esc_html_x( 'Published on %s', 'post date', 'chicken-wings' ),
'' . $time_string . ''
);
I expect the result to be showing Updated Date only if a post is updated only
I can spot the problem is between the $posted_on = sprintf(, and I can see that is including permalink on the actual date, how do I correct the code to include the permalink in the updated date if post updated, and the actual date if not updated.
Thanks.
I have reformatted your code to display the post date and updated date with conditional. Also I have added correct link for the published date and also updated date. Please check following.
function chicken_wings_posted_on() {
$u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 86400) {
echo '<p class="last-updated-up">Last updated on ';
echo '<a href="' . esc_url( get_day_link( get_the_modified_time( 'Y' ), get_the_modified_time( 'm' ), get_the_modified_time( 'd' ) ) ) . '">';
the_modified_time('F jS, Y');
echo '</a>';
echo "</p> ";
} else {
echo '<p class="entry-date published">Published on ';
echo '<a href="' . esc_url( get_day_link( false, false, false ) ) . '">';
the_time('F jS, Y');
echo '</a>';
echo "</p> ";
}
}
Related
I've taken some code from https://gist.github.com/banago/5603826 so that I am able to do previous/next posts for a specific post on my WordPress site which is a custom post type of 'product'.
<?php
if( get_adjacent_post(false, '', true) ) {
previous_post_link('%link', '← Previous Post');
} else {
$first = new WP_Query('post_type=products&posts_per_page=1&order=DESC'); $first->the_post();
echo '← Previous Post';
wp_reset_query();
};
if( get_adjacent_post(false, '', false) ) {
next_post_link('%link', 'Next Post →');
} else {
$last = new WP_Query('post_type=products&posts_per_page=1&order=ASC'); $last->the_post();
echo 'Next Post →';
wp_reset_query();
};
?>
However it doesn't actually infinitely loop, if it's on the first or last post, the link outputs the page it's already on. An example can be found here - http://s860623395.websitehome.co.uk/products/bespoke-build/
Any solutions to this? Thanks!
Was just working on this myself the other day for a portfolio.
Here is what worked for me (adjusted for your product loop)
It should show a single next product indefinitely (infinite loop)
if( get_adjacent_post(false, '', true) ) {
$previous_link_url = get_permalink( get_previous_post() );
echo '← Next Product';
} else {
$first = new WP_Query('post_type=product&posts_per_page=1&order=DESC');
$first->the_post();
echo 'Next Product →';
wp_reset_query();
};
Just a note, next post is actually the previous post, It's a confusing thing, but when you view the latest post, be it portfolio, product or whatever, the next one is actually the one before, otherwise the next post after the most recent published post would then be the oldest one. This seems to be how most have done it within an infinite loop.
Like you, I tried many variations, none of which really worked, but this one did. Currently I'm using it in the Genesis framework for a portfolio CPT with a background image.
Here is the full working code below taken from a child theme:
add_action( 'genesis_after_content_sidebar_wrap', 'go_prev_next_post_nav_cpt', 999 );
/**
* Enable next link in portfolio.
*
* #since 1.0.0
*/
function go_prev_next_post_nav_cpt() {
echo '<div class="adjacent-entry-pagination">';
echo '<div class="wrap">';
if( get_adjacent_post(false, '', true) ) {
$prevThumb = get_the_post_thumbnail_url( get_previous_post(), 'full' );
$previous_link_url = get_permalink( get_previous_post() );
$prevTitle = get_the_title( get_previous_post() );
echo '<a class="post-link bg-image bg-overlay" href="' . $previous_link_url . '" style="background-image:url(' . $prevThumb . ')"><div class="next-description"><p class="mast">Next project</p><h3 class="display-2">' . $prevTitle . '</h3></div></a>';
} else {
$first = new WP_Query('post_type=portfolio&posts_per_page=1&order=DESC');
$first->the_post();
$bg_image = get_the_post_thumbnail_url();
echo '<a class="post-link bg-image bg-overlay" href="' . get_permalink() . '" style="background-image: url(' . $bg_image . ')"><div class="next-description"><p class="mast">Next project</p><h3 class="display-2">' . get_the_title() . '</h3></div></a>';
wp_reset_query();
};
echo '</div>';
echo '</div>';
}
From memory, I used this as a starting point (lot a trial and error!). https://gist.github.com/banago/5603826
I am trying to change the way post dates are displayed in my Wordpress child theme. As of now the time shown is the last modified, and I want it to be the date I manually set in each post (i.e. original publication date).
I have found a file in the parent theme that I think might contain the code culprit. I admit I don't quite understand what this code does though, and I've been trying by trial and error to make it work in a different way, even to the point of deleting it entirely, but to no avail. Whatever I do, the output on my page remains the same. There haven't even been any error messages, either. I have a suspicion that the file in question, which I copied to the child theme's folder, isn't somehow properly queued and thus no changes are reflected. The file is called 'template-tags.php' and it resides in a folder called 'inc'. I have created an analoguous folder in my child theme's directory and copied the file there for editing.
if ( ! function_exists( 'writings_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time and author.
*/
function writings_posted_on() {
$time_string = '<time class="entry-date published updated" 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 class="entry-date published" datetime="%1$s">%2$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() )
);
$posted_on = sprintf(
/* translators: %s: post date. */
esc_html_x( 'Posted on %s', 'post date', 'writings' ),
'' . $time_string . ''
);
$byline = sprintf(
/* translators: %s: post author. */
esc_html_x( 'by %s', 'post author', 'writings' ),
'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);
echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
}
I expect the output on the page to be the original publication date, and not last modified date as it is now
You can copy the main function to the child theme and customize as per your requirement. I have modified the function to ignore updated date in the post meta. You can try following. Copy following code in the functions.php of your child theme and paste it.
function writings_posted_on() {
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() )
);
$posted_on = sprintf(
/* translators: %s: post date. */
esc_html_x( 'Posted on %s', 'post date', 'writings' ),
'' . $time_string . ''
);
$byline = sprintf(
/* translators: %s: post author. */
esc_html_x( 'by %s', 'post author', 'writings' ),
'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);
echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
}
I'm trying to remove the order date from the woocommerce emails, but as I'm not that confident in PHP I'm finding myself a bit stuck.
Below is some code I've found to a similar question (instead removing the order number but leaving the date...I want to do the opposite.
<?php
// Targetting specific email notificatoins
$email_ids = array('new_order', 'customer_on_hold_order');
$date = sprintf( '<time datetime="%s">%s</time>', $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) );
// Displaying order number except for "New Order" and "Customer On Hold Order" notifications
if( ! in_array($email->id, $email_ids) ){
$order_number = sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() );
$date = '('.$date.')';
} else {
$date = __('Order date:', 'woocommerce') . ' ' . $date;
$order_number = '';
}
if ( $sent_to_admin ) {
$before = '<a class="link" href="' . esc_url(
$order->get_edit_order_url() ) . '">';
$after = '</a> ';
} else {
$before = '';
$after = ' ';
}
?>
<h2><?php echo $before . $order_number . $after . $date; ?></h2>
Managed to figure this one out myself...(although not sure it's good practice...but it works!)
Simply added a class to the h2 tag within your-child-theme/woocommerce/emails/email-order-details.php, like below:
<h2 class="fbc-time">
<?php
if ( $sent_to_admin ) {
$before = '<a class="link" href="' . esc_url( $order->get_edit_order_url() ) . '">';
$after = '</a>';
} else {
$before = '';
$after = '';
}
/* translators: %s: Order ID. */
echo wp_kses_post( $before . sprintf( __( 'Order #%s', 'woocommerce' ) . $after . ' <time datetime="%s">%s</time>', $order->get_order_number(), $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ) );
?>
Then selected the class in email-styles.php and used display: none
Let me say about the condition I need to ask:
I display the start sale and end sale for a product on WooCommerce. The code on loop like this:
<?php $thepostid = get_the_ID();
if ( $date = get_post_meta( $thepostid, '_sale_price_dates_from', true ) ) {
$sale_price_dates_from =
'<div class="fromsale">' .
'<span class="promodesc">' . esc_html__( 'Sale start:', 'mytheme' ). '</span>' .
'<span class="m">' . date_i18n( 'M', $date ) . '</span> ' .
'<span class="d">' . date_i18n( 'j', $date ) . '</span> ' .
'</div>'
;
} else {
$sale_price_dates_from =
'<div class="endsale">' .
'<span class="promodesc">' . esc_html__( 'No Schedule', 'mytheme' ). '</span> ' .
'</div>'
; }
echo $sale_price_dates_from ;
?>
<?php $thepostid = get_the_ID();
if ( $date = get_post_meta( $thepostid, '_sale_price_dates_to', true ) ) {
$sale_price_dates_to =
'<div class="endsale">' .
'<span class="promodesc">' . esc_html__( 'Sale end:', 'mytheme' ). '</span>' .
'<span class="m">' . date_i18n( 'M', $date ) . '</span> ' .
'<span class="d">' . date_i18n( 'j', $date ) . '</span> ' .
'</div>'
;
} else {
$sale_price_dates_to =
'<div class="endsale">' .
'<span class="promodesc">' . esc_html__( 'No Schedule', 'mytheme' ). '</span> ' .
'</div>'
; }
echo $sale_price_dates_to ;
?>
The result is like this:
Sale start: May 13
Sale end: May 22
I am curious, how to get times range between the start and the end? In this case I want to display the countdown like this (for example):
Sale will be end: 7days 20hours 33minutes 22seconds.
Can anyone help me?
Thank for any kind of helps.
I print a line consisted of the post date and the number of views (use the plugin wp-PostViews). However, I get 26 [icon] 2016-01-09 [icon] views, instead of [icon] 2016-01-09 [icon] 26 views (wrong position of the number of views) as shown below:
Here are the source codes:
$time_string_published = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
$time_string_published = sprintf( $time_string_published,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date( 'Y-m-d' ) )
);
printf( '<span class="posted-on">%1$s</span><span class="views-link">%2$s</span>',
sprintf( '%2$s',
esc_url( get_permalink() ),
$time_string_published
),
sprintf('%1$s views', the_views())
);
The related css settings are:
.entry-meta .views-link:before {
display: inline-block;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font: normal 22px/1 'Genericons';
vertical-align: top;
}
.entry-meta .views-link:before { content: '\f403'; }
BTW, the following codes work correctly, displaying as [icon] 26 (the right position).
<span class="views-link">
<?php if(function_exists('the_views')) { echo the_views(); } ?>
</span>
Is it possiable that the plugin wp-PostViews (contains the function the_views()) is loaded after excuting the above codes?
I'm not sure about anyone else - but this seems a bit like overkill on trying to do this. Why not try:
$url = esc_url( get_permalink() );
$time = $time_string_published;
$views = the_views();
$out = <<<EOD
<span class="posted-on"><a href="$url"
rel="bookmark">$time</a></span><span class="views-link">$views</span>
EOD;
printf( "%s", $out );
The above does the same thing but is a bit clearer. Sorry! Thought I had it but I got it wrong originally. I've now corrected the generated code. So NOW I say - have you checked what $time_string_published is giving you?
BTW: If your echo of the_views() is giving you "ico 26 views" why not just do this:
$views = substr( the_views(), 4 );
To get the "26 views" part. It would then automatically have the "views" stuck on the end of it. Just an idea.
Update : 8:57pm
Here is the reworked code. It works for me.
<?php
$time_string_published = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
$time_string_published = sprintf( $time_string_published,
date( 'c', time() ),
date( 'Y-m-d', time() )
);
printf( '<span class="posted-on">%1$s</span><span class="views-link">%2$s</span>',
sprintf( '%2$s',
( get_permalink() ),
$time_string_published
),
sprintf('%1$s views', the_views())
);
$date1 = date('c', time() );
$date2 = date('Y-m-d', time() );
$time_string_published = "<time class='entry-date published' datetime='$date1'>$date2</time>";
$url = get_permalink();
$view = the_views();
$s = "<a href='$url' rel='bookmark'>$time_string_published</a>";
echo "<p><span class='posted-on'>$s</span> <span class='views-link'>$view views</span>";
function get_permalink()
{
return "http://www.google.com";
}
function the_views()
{
return 26;
}
?>
The output is:
2016-01-1226 views
2016-01-12 26 views
And the page source code is:
<span class="posted-on"><time class="entry-date published" datetime="2016-01-12T02:55:41+00:00">2016-01-12</time></span><span class="views-link">26 views</span><p><span class='posted-on'><a href='http://www.google.com' rel='bookmark'><time class='entry-date published' datetime='2016-01-12T02:55:41+00:00'>2016-01-12</time></a></span> <span class='views-link'>26 views</span>
Note that the second line has a space between the first SPAN and the second one. If you don't get the same output then there is something going on with either WordPress or some other part of the overall program. About all the help I can provide. :-)
Finally, I tracked it. The reason is that the function the_views() prints something directly, instead return a variable. Here is the source code:
### Function: Display The Post Views
function the_views($display = true, $prefix = '', $postfix = '', $always = false) {
$post_views = intval( get_post_meta( get_the_ID(), 'views', true ) );
$views_options = get_option('views_options');
if ($always || should_views_be_displayed($views_options)) {
$output = $prefix.str_replace( array( '%VIEW_COUNT%', '%VIEW_COUNT_ROUNDED%' ), array( number_format_i18n( $post_views ), postviews_round_number( $post_views) ), stripslashes( $views_options['template'] ) ).$postfix;
if($display) {
echo apply_filters('the_views', $output);
} else {
return apply_filters('the_views', $output);
}
}
elseif (!$display) {
return '';
}
}
To solve it, pass the parameter false to the_views to return as a variable, i.e., the_views(false).
printf( '<span class="posted-on">%1$s</span><span class="views-link">%2$s</span>',
sprintf( '%2$s',
esc_url( get_permalink() ),
$time_string_published
),
sprintf('%1$s views', the_views(false))
);