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.
Related
This is a Word Press/PHP question (very beginner, I'm not a programmer). Trying to find were is the code that display the date in posts page because it's showing the today date instead of the post published one. In the post-info.php I found this code displaying date:
else {
if ($info_parts['date']) {
$post_date = apply_filters('themerex_filter_post_date', $post_data['post_date'], $post_data['post_id'], $post_data['post_type']);
$post_date_diff = themerex_get_date_or_difference($post_date);
?>
<span class="post_info_item post_info_posted"><?php echo (in_array($post_data['post_type'], array('post', 'page', 'product')) ? esc_html__('Posted', 'themerex') : ($post_date <= date('Y-m-d') ? esc_html__('Started', 'themerex') : esc_html__('Will start', 'themerex'))); ?> <a href="<?php echo esc_url($post_data['post_link']); ?>" class="post_info_date<?php echo esc_attr($info_parts['snippets'] ? ' date updated' : ''); ?>"<?php echo ($info_parts['snippets'] ? ' itemprop="datePublished" content="'.esc_attr($post_date).'"' : ''); ?>><?php echo esc_html($post_date_diff); ?></a></span>
<?php
}
Where to correct this code to show the published post date?
I'm using the bookshelf wordpress theme of themerex, verison 1.6.9
Thanks in advance!
Solved! Modified this line
<?php echo esc_html($post_date_diff); ?>
like this:
<php echo esc_html($post_date); ?>
Here is the my code, which is not working.
global $post;
echo get_the_title( wp_get_post_parent_id( $post->post->ID ) );
but this is not working.
thank you in advance.
For parent page id
$post->post_parent;
For current page title
$post->post_title;
For parent page id
<?php
echo wp_get_post_parent_id(get_the_ID());
?>
In Gutenberg:
wp.data.select('core/editor').getEditedPostAttribute('parent')
Hope will be helpful to someone
If you want i.e: create a link to the post parent:
<a href="<?= get_permalink($post->post_parent) ?>">
<?= get_the_title($post->post_parent) ?>
</a>
→ <?= the_title() ?>
which will result in i.e:
Latest News → Some news title
For Astra theme and for page template look.php I did this:
$post->post_parent; will nbot work cause in my case function is out of the loop. I run it via functions.php. $post->post_parent works perfectly when inserting it in page template, but not when editing theme function ;)
function add_script_before_header() {
$current = $post->ID;
$parent = $post->post_parent;
$grandparent_get = get_post($parent);
$grandparent = $grandparent_get->post_parent;
if ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) {
echo get_the_title($grandparent);
}
$after = $parent;
if ( is_page_template( 'look.php' ) ) {
echo $after . ' - ';
}
}
I'm trying to get the current ID of the category page I'm viewing.
I had checked the_category_ID
But this echo'd my result when I used
<?php $catID = the_category_ID(); ?>
Is there a way to get it to return the value to the variable so its hidden?
The current category ID is in the global $cat variable, when you are in a category page.
You can test it with:
<?php echo "Current Category ID is: " . $cat ;?>
when you are in for example this page http://example.com/category/test
Try the following
$catID = get_query_var( 'cat' );
$category= get_queried_object();
echo $category->term_id;
the_category_ID was deprecated in 2003.
Try this:
if (is_category()) {
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
}
Function the_category_ID is deprecated. You need to use get_the_category() function instead. E.g.:
$category = get_the_category();
echo $category[0]->cat_name;
See more at wordpress codex: get_the_category
This code current to get Category ID:
<?php
$category = get_the_category();
echo $category[0]->cat_ID;
?>
It's work for me, today 18 Oct 2016.
This writes the variable instead of echoing
<?php $catID = the_category_ID($echo=false);?>
You will get current category id in variable,
<?php $catID = the_category_ID($echo);?>
This not print directly, whenever give print statment that time only print.
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];
I'm using the advanced custom field plugin for Wordpress to show the results of checkboxes. I have what I want working, I just want to tidy up the code and add the following:
Remove the underscore from the social media tag (some kind of stripping out???).
If possible I'd like to show a comma after each "tag" but not if it's the last one.
Here's my test page, they're the blue "tags" under the discipline section.
Here's my code:
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){
echo ''.strtoupper($name).'';
}
}
?>
Well it is pretty basic, you just have to do a loop. I could have write something better with more information... anyway this should do exactly what your code did but in a loop.
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){ //I don't know what this is suppose to do
echo ''.strtoupper($name).'';
}
}
?>
Try this out:
<?php foreach( get_field('categories') as $category ): ?>
<?php echo ucwords($category) ?>
<?php endforeach; ?>
Ok this should be better
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
$theID = get_cat_ID($name); // get the ID of each category
echo ''.$theID->name.'';
}
?>