Showing Current Date, Day, Or Time In WordPress

WordPress does not come with a default widget or block to display current date or time.

However, you can still display current date or time using some very simple code.

You can add this simple code in your WordPress theme’s template files where you want to display the time.

1<?php echodate(get_option('date_format')); ?>

This code simply prints the current date using the date format set in your WordPress settings. You can change the date format by visiting the Settings » General page.

You can also use your own formatting tags to output the date in any other format. For instance, using the following code you can print the date in month, day, and year format.

1<?php echodate('F j, Y'); ?>

This method allows you to directly add the code into WordPress theme files, but it is not very flexible. What if you wanted to display current date and time inside a WordPress post, page, or a sidebar widget?

This next method allows you to add date and time anywhere on your site.

Source

Getting Theme URL In WordPress

If You wish to retrieve a template’s directory URI for the active theme, use

get_template_directory_uri()

Using get_template_directory_uri() to link a static image with its correct path in html :

1<img src="<?php echo get_template_directory_uri(); ?>/images/logo.png"width=""height=""alt=""/>

Source

How To Include A PHP File In WordPress

This little “PHP include” guide, will show you how to include a PHP-file in your WordPress theme (using a relative path). This snippet isn’t so much a “WordPress snippet”, but really just the PHP include function, using WordPress get_template_directoryto get the relative path for the file. Why you should …

This little “PHP include” guide, will show you how to include a PHP-file in your WordPress theme (using a relative path). This snippet isn’t so much a “WordPress snippet”, but really just the PHP include function, using WordPress get_template_directoryto get the relative path for the file.

Why you should choose a relative path and not an absolute/static path

When you want to include a PHP file (or image file, HTML file, etc.), it is required to specify a specific path that instructs the webserver, on where the webserver should locate the specific file. There are multiple ways to go about this, such as:

  • Using a absolute/static path (not recommended) – specifying the location of a file or directory from the root directory(/).
  • Using a relative path (best practice) – the path related to the present working directly(pwd), starting at your current directory and never starts with a “/”.

The WordPress get_template_directory() function

The build-in WordPress function get_template_directory, retrieves the current theme directory by returning an absolute server path (eg: /home/user/public_html/wp-content/themes/my_theme), and not a URI.

In case you are using a WordPress child theme

In case you are using a child theme, the absolute path to the parent theme directory will be returned – and this will not work. If you are using a child theme, then you would have to use the WordPress function: get_stylesheet_directory() instead, to get the absolute path to the child theme directory.

How to include a PHP file in a WordPress theme

In this PHP include example, we are going to use a relative path, using the WordPress build in function: get_template_directory.

<?php include get_template_directory() . '/inc/yourfile.php'; ?>

How to include a PHP file in a WordPress child theme

In this PHP include example, we are going to use a relative path, using the WordPress build in function: get_stylesheet_directory().

<?php include get_stylesheet_directory() . '/inc/yourfile.php'; ?>