How can I get iso code language in WordPress?
This function :
get_bloginfo('language');
return me the languge like this : en-EN
I create a function like this :
<?php
function pr_language() {
$lang = get_bloginfo('language');
$pos = stripos($lang, '-');
$lang = substr(get_bloginfo('language'),0,-($pos+1)); // retourne "f"
return $lang;
}
?>
Is it correct? I want to display en not en-EN
I hope I understood your question. It seems like all you want to display is en-US.
According to WordPress,
Usage:
<?php bloginfo( $show ); ?>
Parameters:
language
So,
EXACT CODE:
<?php bloginfo('language'); ?>
Will output:
en-US
To show only en, just display first two characters.
function show_short_language() {
<?php echo substr( get_bloginfo ( 'language' ), 0, 2 );?>
}
Just a note for anyone in the future:
<?php echo substr( get_bloginfo ( 'language' ), 0, 2 );?>
Produced:
en
and
<?php echo substr( bloginfo ( 'language' ), 0, 2 );?>
Produced:
en-US
There are language codes with 3 letters rather than 2, so you shouldn't use substr. I did:
$lang = explode('-', get_bloginfo('language'));
$lang = $lang[0];
Related
I recently bought a new Wordpress theme but all my custom excerpts are being cut-off based on character count rather than word count. I've found the excerpt.php file that the theme is using and I think the issue down comes down to the use of substr, but I can't figure out how to change it to fix it. I tried replacing it with wp_trim_words but then I just saw a number rather than text in my excerpts.
Would be grateful if someone can point me in the right direction!
The code:
$excerpt = get_the_excerpt();
if ( ! empty( $excerpt ) ) {
$excerpt_length = healthfirst_get_search_page_excerpt_length();
$new_excerpt = ( $excerpt_length > 0 ) ? substr( $excerpt, 0, intval( $excerpt_length ) ) : $excerpt;
?>
<p itemprop="description" class="qodef-e-excerpt">
<?php echo esc_html( strip_tags( strip_shortcodes( $new_excerpt ) ) ); ?>
</p>
<?php }
You can replace this line..
$new_excerpt = ( $excerpt_length > 0 ) ? substr( $excerpt, 0, intval( $excerpt_length ) ) : $excerpt;
.. with.
$new_excerpt = wp_trim_words( $excerpt, 10 );
You do not need the condition since you are trying to override the excerpt length. You can actually remove this line afterwards.
$excerpt_length = healthfirst_get_search_page_excerpt_length();
Unless it is still used somewhere else in that file or function.
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;
}
I have two "Custom Fields" assigned to a post I have. Both of these "Custom Fields" have the same name, but different "Value". At the moment, my code below only presents one of the links. I am trying to get it to display both. So anytime I add another "Custom Field" with the name of "Featured-Blog", it will continue display all of them.
Custom Field's
1) Name: Featured-Blog and Value: 704 (704 is the postID)
2) Name: Featured-Blog and Value: 699 (699 is the postID)
Code being used to display a link to each of the posts. (can only get one of the custom fields to display)
Screenshot of output
Code being used
<?php $related = get_post_meta($post->ID, "Featured-Blog", $single=true);
$related=explode(',',$related);
$args = array_merge( array('post__in' => $related, $wp_query->query ) );
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="<?php the_ID(); ?>">
<p class="caption"><?php the_title(); ?></p>
</div>
<?php endwhile; else: ?>
<p>no related</p>
<?php endif; wp_reset_query();?>
Now below is an older code that I was originally trying to use, but didn't end up using. This one actually does pull both of my "Custom-Fields". You can see it's obviously coded different because you can see it says "Title" instead of the posts title. But I am just using this code as an example to show you that more than one "Custom Field" can be displayed, unless there is an easy fix for the code below?. Maybe some of the code form that can be incorporated into my working script above. Both the above code, and this bottom one are very close to what I'm trying to do. It seems like one, has something the other one needs.
Screenshot of output
<div id="related-posts">
<?php
$custom_fields = get_post_custom($post_id); //Current post id
$my_custom_field = $custom_fields['Featured-Blog']; //key name
foreach ( $my_custom_field as $key => $url )
echo $key ="<a href='".$url."'>TEST</a><br /><br /><br/>";
?>
You simply have to pass false instead of true when using get_post_meta():
$related = get_post_meta( $post->ID, "Featured-Blog", false );
var_dump( $related );
With the var_dump you'll be able to see the raw contents of the variable. Anyway, you will receive an array, so you can simply do:
$related = get_post_meta( $post->ID, "Featured-Blog", false );
$args = array_merge( array('post__in' => $related, $wp_query->query ) );
get_post_custom, on the other hand, grabs ALL custom fields of a post and produces the same result at the end, it just takes an extra command for you to the the values.
Note that you should not be using query_posts.
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´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/