I have code that gives me the average of certain number which is fine however, it's giving me quite a long number e.g. 4.8571428571429
Is it possible to get it down to 2 decimal places e.g. 4.85
Hope it makes sense, my code so far is below
<?php // Get total number of posts in custom post type
$count_testimonials = wp_count_posts('testimonial');
$total_testimonials = $count_testimonials->publish;
$new_average = ($add) / ($total_testimonials);
echo $new_average;
?>
Try to use this: number_format((float)$new_average, 2, '.', '')
<?php
$count_testimonials = wp_count_posts('testimonial');
$total_testimonials = $count_testimonials->publish;
$new_average = ($add) / ($total_testimonials);
$new_average;
echo number_format((float)$new_average, 2, '.', '');
?>
Hope this will help you. Thanks.
Related
in word press I am trying to echo price like round last 3 digit.
Example
$price= 15.786.625;
I whant after echo price like this
price=15.786.000
How I can do it?
I find a way do it this.
Say
$price=4,316,028
$pricecarp=round($price/1000)*1000;
<?php
echo number_format($pricecarp, 0, '.', ',');
?>
Result= 4,316,000
i want to trim a single word i.e, Monday in wordpress, how can i trim this word?
$my_title = get_the_title();
echo wp_trim_words($my_title, 1, null );
the title coming from database Monday So i want to trim Monday to Mo or M.
wp_trim_words() will works easily without any problems. You can use it with below sample code:
echo wp_trim_words( get_the_title(), 1, '' );
But do note that it will trim the first character in whole post/page title, not regconize the date in your example.
I don't know that wp_trim_words() works that way. I'd recommend just using regex and preg_match():
$my_title = get_the_title();
to get the first character:
$regex = '/(.?)/';
or to get the first two characters, change the variable to this:
$regex = '/(.?)./';
Then get the matches for the regex pattern:
preg_match($regex, $my_title, $matches);
Echo out the first one:
echo $matches[0];
Is it possible to fetch a post with content under 140 characters or 25 words ?
if possible how to do it
here is my random post code
// Random post link
function randomPostlink(){
$RandPostQuery = new WP_Query(array('post_type'=>array('tip'),'posts_per_page' => 1,'orderby'=>'rand'));
while ( $RandPostQuery->have_posts() ) : $RandPostQuery->the_post();
echo the_permalink();
endwhile;
wp_reset_postdata();
}
Character count is easy, you can just add the condition AND CHAR_LENGTH(post_content) < 140 to your where clause.
Word count is more difficult because there is no built in MySQL function for counting words. You can find simple solutions that don't work in every use case as well as complete solutions that use stored functions. I'll use a simple solution for the sake of example.
What you need to do is add a filter to the where clause and apply your additional conditions there:
add_filter( 'posts_where', 'venki_post_length_limit' );
function venki_post_length_limit($where = '') {
remove_filter( 'posts_where', 'venki_post_length_limit' );
$where .= ' AND (
CHAR_LENGTH(post_content) < 140 OR
(LENGTH(post_content) - LENGTH(REPLACE(post_content, ' ', ''))+1) < 25
) ';
return $where;
}
Notice that I remove the filter as soon as the function is called. This is so you don't apply this same condition to every query.
You should also be aware that both of those conditions are costly compared to a simple lookup on a column value (especially the word count). Neither can utilize indexes. If you have a large number of posts you may run into performance issues if you're running this query frequently. A better solution might be to calculate the word and character count when the post is created/updated and store that as meta data.
I have created a custom post type for contests.
and 1 of the fields is a closing date , everything is going fine only i cant figure out how to output the date format on the way i want it.
with this code in my loop i get the date , but not as ( day-Month-year )
<?php echo get_post_meta($post->ID, "_closingdate", array("format" => 'd m, Y'), true); ?>
the output remains year-month-day
i have searched everywhere to find a solution but i cant find it or i am overlooking something
Do this
echo date('d m, Y', strtotime(get_post_meta($post->ID, "_closingdate")));
i would do
echo date('d m, Y', get_post_meta($post->ID, "_closingdate"));
i dont know what type you used for this, but if timestamp from time() then its ready to use. If not - then you might have to parse it as string to date 1st then parse it to date.
So i have a custom field that gives output as plain text in this format: dd. MM yy (09. October 2013). This is the loop that im using:
query_posts(array('category_name'=>'somecategory', 'posts_per_page'=>'5','paged' => get_query_var('paged')));
while (have_posts()) : the_post();
//stuff
the_field('mydate'); //the output is: dd. MM yy
//stuff
endwhile;
wp_reset_query();
//paginate_links code...
If i add this to query_posts:
'meta_key' => 'mydate', 'orderby'=>'mydate'
My posts get sorted only by day and year couse it seems it cant read the month couse its a string. And this kind of sorting is kinda usless. If the site was my i would change date format and it would be solved, but i am doing this site for someone and the designer already draw this date format in photoshop so it has to be this way. So how can i make this sort work properly?
Add below code before you query_posts:
add_filter('posts_where', 'filter_orderby');
function filter_orderby( $where = '' ) {
$where .= " AND $wpdb->postmeta.meta_key = 'mydate' ORDER BY mydate DESC";
return where;
}
hope it helps