Trying to show the published date, and the update date if it differs from published date, I tried this code in my wordpress template:
<p class="card-text">Published <?php the_date();?>.
<?php
if ( the_date( 'U' ) !== the_modified_date( 'U' ) ) {
echo "Updated " . the_modified_date('F j, Y');
}
?>
However i do net get any output. Possibly simple, but still I only get a very long number as output.
You should use get_the_date and get_the_modified_date because they return the value and not printing it out.
if ( get_the_date( 'U' ) !== get_the_modified_date( 'U' ) ) {
echo "Updated " . get_the_modified_date('F j, Y');
}
Related
I am trying to spell out WooCommerce order total amount, in my invoice.php template file can reach order total amount.
First I tried:
$total = $order->get_total();
<?php echo ( $total ); ?> -
<?php
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format($total); ?>
The order total displayed is 225.00 and the spell out display is: two hundred twenty-five
Edit:
I found the following solution:
<?php $number = $order->get_total() ;
$formatter = new NumberFormatter('tr', NumberFormatter::SPELLOUT);
$formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%financial");
echo $formatter->format($number); ?>
But the result shows like that: two hundred twenty-five
The desired display should be: two hundred twenty-five turkish liras , zero penny.
How can i do this ?
The NumberFormatter SPELLOUT constant doesn't handle the decimals.
You can use the following custom function to display a float number amount spell out like (with the currency details):
function wc_spellout_amount( $amount, $country_code = 'tr' ) {
$formatter = new NumberFormatter($country_code, NumberFormatter::SPELLOUT);
$formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%financial");
$amounts = explode('.', (string) $amount); // Separating decimals from amount
$output = $formatter->format($amounts[0]);
$output .= ' ' . _n('turkish lira', 'turkish liras', $amounts[0], 'woocommerce');
$output .= ', ' . $formatter->format($amounts[1]);
$output .= ' ' . _n('penny', 'pennies', ( $amounts[1] > 0 ? $amounts[1] : 1 ), 'woocommerce');
return $output;
}
Code goes in functions.php file of the active child theme (or active theme).
Usage: Then you will use it as follows in your code:
echo wc_spellout_amount( $order->get_total() );
Tested and works.
I am trying to display a line of text if a specific category (in this case: advisories) is empty, but the code I am trying to use does not display anything.
<?php
if ( in_category( 'advisories' )->count > 0 ) {
echo test;
}
?>
Try this one,
Solution
$custom_terms = get_terms();
foreach ($custom_terms as $term) {
if ($term->count != 0 && $term->name == "Uncategorized") {
echo $term->name . " - blank" . '<br>';
}
}
Screenshot
Hope this will helps you.
For more example, please visit
get_terms Wordpress
WordPress to test for an empty term or category
You can try this one
$category = get_category($id);
$count = $category->category_count;
if( $count > $something ) {
// stuff
}
I am using Wordpress for my online shop.
I have a problem. On the page with the product, I added a button for comparing the products. All is well but I want that after pressing the button, it becomes invisible.
<?php
$session_data = $be_product_compare->session->get_session_cookie();
if( is_array( $session_data ) && in_array( get_the_ID(), $session_data ) )
{ echo BE_Compare_Products::display_button( ''.get_the_ID() . '' );
}
else
{
echo ' disabled=disabled ';
}
?>
And I get
Uncaught Error: Call to a member function get_session_cookie() on null
in
Can someone help me finish with this Button?
I'm trying to get Wordpress to execute some code if the post date is later than a particular date. When I use the following, it almost works...except Worpress shows/echos the date instead of just evaluating it.
<?php $date1 = '2013-03-22';
$date2 = the_date();
if ($date2 >= $date1) { ?>
//code to execute
<?php } ?>
Any ideas?
It's simply how the_date() works:
Displays or returns the date of a post, or a set of posts if published on the same day
Example usage:
<?php the_date( $format, $before, $after, $echo ); ?>
So you have to pass false to the function to not print the date, because it defaults to true. For example:
<?php
$date1 = '2013-03-22';
$date2 = the_date('', '', '', FALSE);
if ($date2 >= $date1): ?>
Hello world!
<? endif; ?>
Ended up using something like this:
$date2 = the_date('','','',FALSE);
if (strtotime($date2) >= strtotime($date1)) { code to execute }
You can also try this :
$date1 = '2013-03-22';
$date2 = get_the_date();
if ($date2 >= $date1) { ?>
//code to execute
<?php } ?>
get_the_date() just returns the date while the_date() prints the post date.
I need to query a category in home page. in index.php file I used this script
$all_featured_posts = query_posts(array('category_name'=>'featured-programs'));
Then in the header.php file I need to change the title
<title>
<?php
if ( is_home() ) {
echo 'My site name' ;
} elseif (is_404()) {
echo '404 Not Found';
} elseif (is_category()) {
echo ' Category' . wp_title('',0).' | My site name' ;
}
?>
The problem is when I query a category in the index file then the is_home return false ( Tried with is_front_page() also ) Then it alway show the title with the name of the category which I query.
How I can fix it? Thanks you!
I might be wrong, but I think because you use query_posts(), all your is_* functions change their values. And, well, because you do query a category, is_home() should return false.
What you can do to solve it, is use new WP_Query(), and get all the posts from it. This way, you will not be affecting the original WP_Query, and thus the is_* functions.
The code should look like this:
$query = new WP_Query('category_name=featured-programs');
while ( $query->have_posts() ) : $query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();