wp_trim_words not working on anything but ordinary strings - trim

I´m a bit out of my field here and I´m confused about this. Using wp_trim_field doesn't work for me except for regular strings.
This does not work, it returns the whole text:
<?php
$field = the_field('project_description');
$trimmedfield = wp_trim_words( $field, $num_words = 1, $more = '… ' );
echo '<p>' . $trimmedfield . '</p>';
?>
This however does work:
<?php
$field = 'this text does get trimmed';
$trimmedfield = wp_trim_words( $field, $num_words = 1, $more = '… ' );
echo '<p>' . $trimmedfield . '</p>';
?>
Echoing out the $field instead does echo out the text that I am trying to trim, but the trimming aint working. Any ideas as to why?
edit - I also tried this, same thing happens:
<?php
$length = 1;
$text = the_field('project_description');
$words = explode(' ', $text);
array_splice($words, $length);
$text = implode(' ', $words);
echo $text;
?>

use var_dump($field); wp_trim_words( $field,....) $field must be string type ...check if this is or not to test the datatype, if its not im sure you know what to do then.
Use typecast if its not.

You'll need to change the $field variable to this: $field = get_field('project_description');
the_field(); outputs the content, while get_field(); retrieves it. In order to pass it through a function, you'll need to retrieve it.
ACF documentation page that answers this question: https://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/

Related

How display the spell out of an amount with currency details in WooCommerce

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.

how to remove space in post url

I am using the below line to retrieve the permalink for a post type archive.
<?php get_post_type_archive_link( $post_type ); ?>
When I am using this code it shows the url like this:
http://mywebsite.com/about /
I need to remove space between about and '/'. So where i want to change this url.
Use following code to remove space in url
<?php
$url = $get_post_type_archive_link( $post_type );
echo str_replace(' ', '', $url);
?>
A quick fix would be to replace spaces with 'nothing' like in the following example.
$string = str_replace(' ', '', $string);
But I think a core WP method returning a string with spaces is a bit weird. I would check why it returns URL's with spaces, because that is not really normal behaviour.
You can solve it using two ways
1. str_replace
2. regex pattern
1. using str_replace
<?php
$url = "http://www.techiecode.com/ magic- methods-in-php.html ";
echo str_replace(" ", "", $url);
// Output: http://www.techiecode.com/magic-methods-in-php.html
?>
2. using regex pattern
<?php
$string = 'http://www.techiecode.com/ 222magic- methods-in-php.html ';
$pattern = '/( )*/';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
// Output: http://www.techiecode.com/222magic-methods-in-php.html
?>

Wordpress 'wp_get_archives()' as raw text?

in a special case I need the output of wp_get_archives(); to be just raw text instead of a link. Does anyone know how to do that? I recently checked the reference but also the 'format'=>'custom' option renders a link.
Thanks in advance!
---- Edit (#vard) ----
<?php
$args2 = array(type'=>'monthly','limit'=>'','format'=>'html','before'=>'','after'=>'','show_post_count'=> false,'echo'=>1,'order'=>'DESC');
echo "<ul>";
wp_get_archives( $args );
echo "</ul>";
?>
and code from zurb.com:
add_filter( "get_archives_link", "customarchives_link");
function customarchives_link( $x ){
$url = preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.-]*(\?\S+)?)?)?)#i', $x, $matches);
return $matches[4] == $_SERVER['REQUEST_URI'] ? preg_replace('#<li#', '<li class="current_page_item"', $x) : $x;
}

how to remove read more when full text is on in wordpress?

the code looks like this
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
there is an option in wp-admin>settings>reading>For each article in a feed, show
if this is set to full text
the excerpt() must return full length article instead of specified length.
how to do this?
Good question! The answer is simple: write your own function!
Open up functions.php in your favorite editor and mash random buttons on your keyboard until you get something like this:
function my_awesome_excerpt ($post_id = false, $full = false, $length = 22) {
if (!$post_id) global $post;
else $post = get_post($post_id);
$text = $post->post_content;
if ($full) return $text;
else {
$text_array = explode(' ', $text);
$return_string = array();
for ($i = 0; $i <= $length; $i++)
array_push($return_string, $text_array[$i]);
$new_awesome_string = '<p>';
$new_awesome_string .= implode(' ', $return_string);
$new_awesome_string .= '</p><p class="readmore">';
$new_awesome_string .= '<a href="' . get_permalink($post_id) . '">';
$new_awesome_string .= 'Read More' . '</a></p>';
return $new_awesome_string;
}
}
Now, you're ready for the cool part. Once you're in your loop, you can write out some magic like this:
echo my_awesome_excerpt();
and it will automagically spit out an excerpt. It's using the global post variable and everything! You can even use it outside of the loop:
echo my_awesome_excerpt($cpt->ID, 22);
and set your own special length!
Or maybe you just know in your heart that it's not worth it, you just want to show the whole thing. How's that look?
Inside the loop, you're going to have to give it a post ID, sorry about that.
echo my_awesome_script($post->ID, false);
I hope this helps. Have a great day!

Highlighting Word(s) Searched on in WordPress Search

I am using built-in WordPress search.php routines, is it possible to highlight the word searched on, in context with the search results retrieved?
For example, if I typed in "products", any page that returned this matching word would be highlighted to the user.
Thanks.
Here is a function you can add to functions.php that will highlight the searched term in the results.
/* Search Highlighting ********************************************/
// This highlights search terms in both titles, excerpts and content
function search_excerpt_highlight() {
$excerpt = get_the_excerpt();
$keys = implode('|', explode(' ', get_search_query()));
$excerpt = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $excerpt);
echo '<p>' . $excerpt . '</p>';
}
function search_title_highlight() {
$title = get_the_title();
$keys = implode('|', explode(' ', get_search_query()));
$title = preg_replace('/(' . $keys .')/iu', '<strong class="search-highlight">\0</strong>', $title);
echo $title;
}
To use this function it has to be added to your archive loop:
<?php if (is_search() ) {
search_excerpt_highlight(); } ?>
Here is a few WP Plug-in that highlights search words using jQuery:
http://wordpress.org/extend/plugins/highlight-search-terms/
http://urbangiraffe.com/plugins/search-unleashed/
or do it yourself:
http://www.livexp.net/wordpress/highlight-the-search-terms-in-wordpress-search-results.html

Resources