I have WordPress theme and I am using get_the_date function to get the publish date of post :
<?php echo get_the_date( 'M-d-y' ); ?>
But it seems it will be better to use get_option( 'date_format' ).
How can I display post publish date using get_option function?
Doesn't need to use get_option for getting post published date. You can get that by using get_the_date function properly.
Here is the solution:
Make the date appear as Monday January 11, 2017, use
$post_date = get_the_date( 'l F j, Y' ); echo $post_date;
To make the date appear as Wed Jan 9, use
$post_date = get_the_date( 'D M j' ); echo $post_date;
Read more about get_the_date and check date formatting options here, in wordpress docs.
Related
I use ACF pro for my wordpress.
My datepicker field is a repeater field.
I need to return only the day.
my code :
<?php
if( have_rows('dates') ):
while ( have_rows('dates') ) : the_row();
echo get_sub_field('date')."</br>";
endwhile;
else :
echo __( 'No dates available.','mywebsite' );
endif;
?>
You can set the return value of the date field.
EDIT
Ok, so if you want to display two dates, one full and one is only day, you first need the return value to be the full date, for this example lets day it d/m/Y
$full_date = get_sub_field('date'); // the full date (format d/m/Y);
$day_from_date = DateTime::createFromFormat('d/m/Y', $full_date)->format('d'); // will get the day from the $full_date
This will get you the result you need.
See DateTime::createFromFormat for more information about the method
The get_sub_field() function would typically return a string depending on the field type. This instance it will return a timestamp as as string eg '2021-06-28 10:28:00'
If you want to return the day only you could use PHP's strtotime function which will then return you a datetime epoch integer - this can then be used in conjunction with php's date function to print as the day. Here is a list of the formats you can use for the date function : PHP DateTime::format
Example :
<?php
if (have_rows('dates')):
while (have_rows('dates')) : the_row();
$dateTime = strtotime(get_sub_field('date'));
echo("Day : " . date('l', $dateTime) . "</br>");
endwhile;
else :
echo __('No dates available.', 'mywebsite');
endif;
?>
As another solution which is within your setup rather than your code - change the 'Return Format' of the actual field.
See : ACF Date Time Picker Docs
Examples would be :
'd' - Day of the month, 2 digits with leading zeros
'j' - Day of the month without leading zeros
'D' - A textual representation of a day, three letters
'l' - A full textual representation of the day of the week
Then your original code will output the correct format :
<?php
if( have_rows('dates') ):
while ( have_rows('dates') ) :
the_row();
echo('Day: ' . get_sub_field('date') . "</br>"); //This will now output your 'Return Format' in ACF setup
endwhile;
else :
echo __( 'No dates available.','mywebsite' );
endif;
?>
My client has asked "I would like to be able to print a listing of all products by SKU (sorted alphanumerically) with description, and by description (alpha) with the associated SKU." I think the closest I could get for him is to have the SKU sorted alphanumerically but not both, though I could be wrong.
Looking at this question: Woocommerce: get a list for all sku product, I wonder if I could build a table instead of a list and pull the description in next to the SKU?
Here's the code I'm thinking of based on the link above (without sorting):
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
query_posts( $args );
if( have_posts() ):
echo '<table>';
while ( have_posts() ) : the_post();
echo '<tr><td>'. $product->get_sku() . ' - ' . $product->get_title() . '</td><td>' . $product->get_description() . '</td></tr>';
endwhile;
echo '</table>';
endif;
I don't think the description is the correct way to call it, but I didn't see one to call in the short description.Also, I'm not sure how I would have it sort by SKU. My PHP knowledge is limited.
Even if that code worked, I'm not sure where I would put it to call this in. Can I make it on an admin page or would it be better to create a regular page and use a different template and put this directly into the PHP of that template file?
Any suggestions would be great!
I've set up some fields using the advanced custom fields.
I’ve created a custom field and a post that uses that custom field. I’m trying to display it on a page like this:
<?php
$args = array( 'post_type' => 'Portfolio Item' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<p>' . the_title() . '</p>';
echo '' . the_field('portfolio_url') . '';
endwhile;
?>
The title displays no problem, but the custom field does not i.e. the output is just:
The name ‘portfolio_url’ is the ‘Field Name’.
Can anyone help with what I’m doing wrong?
Maybe you should try and send in smaller snippets of code.
Or give an online example.
Basically if you add a the_field('bottom_quote') function on your page it should echo out the current pages' "bottom_quote" field.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
<?php the_field( 'bottom_quote', $post->ID );
Also note that $post should either be global or in a foreach loop.
I don't think the post_type parameter is allowed to have a space. Check that you're using the correct slug for that first.
I am not to familiar with this specific plugin but you may need to call in the global $variable that I know when using a class like WPAlchemy you need to call $meta
Check here http://codex.wordpress.org/Function_Reference/get_post_meta
I'm using a custom query to retrieve only posts published in the last 30 days for a custom post type.
I have wp-paginate installed and am using it for pagination. The page itself is working fine, but wp-paginate seems to be showing enough pages for all posts, whether they were returned or not but the custom query.
For example, there are 35 published posts, but only 12 of them in the last 30 days. WP-Paginate should only display 2 pages for all 12 posts in the last 30 days, but it's showing 4 pages, with page 3 and 4 being blank.
My code for the query is:
<?php
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;
function filter_where($where = '') {
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
$jobPosts = null;
add_filter('posts_where', 'filter_where');
$jobPosts = new WP_Query('post_type=job_boards&paged=' . $current_page);
remove_filter('posts_where', 'filter_where');
while ($jobPosts -> have_posts()) : $jobPosts -> the_post();
// Display stuff
endwhile; wp_reset_postdata();
if (function_exists('wp_paginate')) wp_paginate();
?>
I seem to have solved it, though it's way more complicated than I think it needs to be. I'm passing the number of pages and the current page to display into the wp_paginate function:
wp_paginate(array('pages' => $num_pages, 'page' => $current_page));
While using custom query try below
For wp pagenavi plugin
if (function_exists('wp_pagenavi')):
wp_pagenavi( array( 'query' => $jobPosts ) );
endif;
For wp paginate plugin
if (function_exists('wp_paginate')):
wp_paginate(false,$jobPosts);
endif;
Now the wordpress is displaying the blog archives as following
Janauary 2012
But I want this
Jan (2)
where 2 is the total number of posts in the month of January.
How will I do this?
You have to modify the template and replace this <?php the_time('F Y'); ?> with this <?php the_time('M'); ?> (<?php $current_month = date('m'); $count = get_posts("monthnum=$current_month" ); echo count($count); ?>) and it should work.
WordPress links to archives using the "m" argument to index.php with the year first and the month second. So, for example, January 2012 would be index.php?m=201201. So, to link this, we need to create the link like this:
...