The number of mobile users across globe is forecast to reach an astonishing 2.87 billion, & based on socPub 57% of users say they will not recommend a business with a poor quality mobile responsive site.
Fortunately, creating a fully responsive website is ease with WordPress, particularly when you go for latest WordPress themes.
WordPress is the most popular content management system (CMS) for many websites as it is highly user-friendly and customizable, making it a popular choice at ColorWhistle for client projects.
If you’re familiar with custom WordPress theme development, you know things can get messy, especially if you’re starting from the scratch.
So, how should you go about doing it?
We ColorWhistle WordPress web development company in India are going to save you the trouble by providing some common WordPress functions list used in custom theme development.
With the help of our WordPress developers, I have provided a basic overview of how to incorporate those functions into a WordPress website.
Also Read
WordPress Functions List
How to show individual (Single) post content in front-end?
<?php
if(have_posts()) : while (have_posts()) : the_post();
the_title();
echo '<div class="entry-content">';
the_content();
if(get_post_meta($post->ID, 'key-name', true)){
echo get_post_meta($post->ID, 'key-name', true);
}
echo '</div>';
endwhile; endif;
?>
How to list custom post-type?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
SpostsPerPage = 4;
$args = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => $postsperpage
);
$loop = new wp_query($args);
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post(); ?>
<a href="<?php the permalink() ?>"><?php the_post_thumbnail( 'full' ); ?></a>
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
<?php echo wp_trim_words ( get_the_content (), 40, ...'); ?>
<a href="<?php the permalink() ?>">Read More</a>
<?php endwhile; wp_reset_query(); ?>
<div class="navigation">
<?php
the_posts_pagination ( array(
'prev_text' =>_('Previous page', 'cbc'),
'next_text' =>_('Next page', 'cbc'),
'before_page_number' => '<span class="meta-nav screen-reader-text">'._( 'Page', 'cbc' ).'</span>',
) );
?>
</div>
<?php
// If no content, include the "No posts found" template.
else:
get_template_part('content', 'none');
endif;
?>
For example, if you require a post type as 'travel', you have to add, 'post_type' =>
'travel'.
How to show individual page content in front-end?
<?php
while(have_posts()):the_post();
get_template_part('template-parts/content', 'page');
if(comments_open() || get_comments_number()){
comments_template();
}
endwhile;
?>
How to list child pages?
<?php
$child_pages = new Wp_Query(array(
'post_type' => 'page', //set the post type to page
'posts_per_page' => 1, //number of posts(pages) to show
'page_id' => 81, //enter the post ID of the parent page
'no_found_rows' =>true,
//no pagination necessary so improve efficiency of loop
));
if($child_pages->have_posts()) : while($child_pages->have_posts()) : $child_pages()->the_post();
the_title('<h2 class="entry_title">', '</h2>');
get_template_part('template-parts/content', 'page');
endwhile;endif;
wp_reset_postdata();
?>
How to use ‘User meta’ function?
add_user_meta
<?php add_user_meta($user_id, $meta_key, $meta_value, $unique);?>
Ex:
<?php
$user_id = 1;
$awesome_level = 1000;
add_user_meta($user_id, '_level_of_awesomeness_', $awesome_level);
?>
update_user_meta
<?php update_user_meta($user_id, $meta_key, $meta_value, $prev_value);?>
Ex:
<?php
$user_id = 1;
$website = 'http://wordpress.org';
update_user_meta($user_id, 'user_url', website);
?>
delete_user_meta
<?php delete_user_meta($user_id, $meta_key, $meta_value);?>
Ex:
<?php
$user_id = 9;
$website = 'http://wordpress.org';
delete_user_meta($user_id, 'disabled');
?>
get_user_meta
<?php get_user_meta ($user_id, $key, $single); ?>
Ex:
<?php
$user_id = 9;
$key = 'last_name';
$single = true;
$user_last = get_user_meta( $user_id, $key, $single );
echo '<p>The '. $key . value for user id' . $user_id . ' is: '. $user_last. '</p>';
?>
$user_id
- (integer) (required) user ID
- Default: None
$meta key
- (string) (required) Metadata name.
- Default: None
$meta value
- (mixed) (required) Metadata value. (Don’t serialize it yourself, WordPress will do it for you.)
- Default: None
$unique
- (boolean) (optional) Whether the same key should not be added.
- Default: false
$prev value
- (mixed) (optional) Previous value to check before removing.
- Default:’ ‘
$single
- (boolean) (optional) If true return value of metadata field, if false return an array. This parameter has no effect if $key is left blank.
- Default: false
$before
- (string) (optional) Text to place before the date.
- Default: None
$after
- (string) (optional) Text to place after the date
- Default: None
$echo
- (boolean) (optional) Display the date (TRUE), or return the date to be used in PHP (FALSE).
- Default: TRUE
How to include date/time function?
<p>Posted: <?php the_date('F j, Y'); ?> at <?php the_time('g:i a'); ?></p>
F j, Y g:i a - November 6, 2010 12:50 am
F j, Y - November 6, 2010
F, Y - November, 2010
g:i a - 12:50 am
g:i:s a - 12:50:48 am
1, F js, Y - Saturday, November 6th, 2010
M j, Y @ g:i - Nov 6, 2010 @ 0:50
Y/m/d \a\t g:i A - 2010/11/6 at 12.50 AM
Y/m/d \a\t g:i a - 2010/11/6 at 12.50 am
Y/m/d g:i:s A - 2010/11/6 12.50:48 AM
Y/m/d 2010/11/06
Date as Year, Month, Date in Heading
<?php the_date('Y-m-d', '<h2>', '</h2>'); ?>
How to use tags in WordPress website?
- <?php the_title(); ?> – Displays the posts/pages title
- <?php the_content();?> – Displays the content of the post/page
- <?php the_excerpt();?> – Displays the excerpt of the current post/page
- <?php the_time();?> – Displays the time of the current post/page
- <?php the_date();?> – Displays the date of a post or set of post/page
How to create a Taxonomy?
Registering a taxonomy:
Here is an example of registering a “people” taxonomy
function people_init() {
// create a new taxonomy
register_taxonomy (
'people',
'post',
array(
'label' => _('people'),
'rewrite' => array('slug' => 'person'),
'capabilities' => array(
'assign_terms' => 'edit_guides',
'edit_terms' => 'publish_guides',
)
)
);
}
add_action('init', 'people_init');
Here is an example of adding the term John to post id number 555 in the "person" taxonomy
wp_set_object_terms(555, 'John', 'person');
How to display terms?
The following is an example of the terms() condition
<?php the_terms($post_ID, 'category', 'categories:','/'); ?>
How to add login/logout links to WordPress menu?
To add a signup and login/logout links to your WordPress menu, add the code snippet below to your theme’s functions.php file.
function add_login_logout_register_menu( $items, $args) {
if ($args->theme_location != 'primary' ) {
return $items;
}
if(is_user_logged_in()){
$items .= '<li><a href="'.wp_logout_url().'">'._('Logout').'</a></li>';
}
else{
$items .= '<li><a href="'.wp_login_url().'">'._('Login In').'</a></li>';
$items .= '<li><a href="'.wp_registration_url().'">'._('Sign Up').'</a></li>';
}
return $items;
}
add_filter('wp_nav_menu_items', 'add_login_logout_registration_menu', 199, 2);
How to add categories?
get_cat_ID
<?php
function get_cat_ID($cat_name){
$cat = get_term_by('name', $cat_name, 'category');
if($cat)
return $cat->term_id;
return 0;
}
?>
get_cat_name
<?php
function get_cat_name($cat_id){
$cat_id = (int) $cat_id;
$category = get_term($cat_id, 'category');
if(!category || is_wp_error($category))
return '';
return $category->name;
}
?>
get categories
<?php $args = array("hide_empty" => 0
"type" => "post",
"orderby" => "name",
"order" => "ASC");
$types = get_categories($args);
?>
get_category
<?php $categories = get_categories(array('hide_empty' => false, 'child_of' => 10));>
get_the_category_list
<?php get_the_category_list($separator, $parents, $post_id);?>
in_category
<?php if(in_category($vegetarian)):?>
You're in the vegetarian category.
<?php endif;?>
is_category
<header class="archive-header">
<?php if(is_category('Featured')):?>
<h1 class="archive-title">Featured Articles:</h1>
<?php else:?>
<h1 class="archive-title">Category Archive:<?php single_cat_title();?></h1>
<?php endif;?>
</header>
How to insert the post manually or custom?
Post insertion/removal
wp_delete_post
<?php wp_delete_post($post_id, true);?>
wp_insert_post
<?php
if(isset($_POST['new_post])=='1'){
$post_title = $_POST['post_title'];
$post_cat = $_POST['cat'];
$post_content = $_POST['post_content'];
$new_post = array(
'ID' => '',
'post_author' => $user=>ID,
'post_category' => array($post_category),
'post_content' => $post_content,
'post_title' => $post_title,
'post_status' => 'publish'
);
$post_id = wp_insert_post($new_post);
//This will redirect to the newly created post.
$post = get_post($post_id);
wp_redirect($post->guid);
}
?>
Others
get_the_ID
<?php $postid = get_the_ID();?>
<?php echo '<input type="hidden" name="activepost" id="activepost" value="'.get_the_ID().'"/>';
?>
get_the_content
<?php
$content = get_the_content('Read more');
print $content;
?>
the_content and get_the_title
<?php the_content('Continue reading' .get_the_title());?>
the_title
<?php the_title('<h3>','</h3>');?>
Shortcodes
Add shortcode
function fun_name($atts){
return "foo={$atts['foo']}";
}
add shortcode('shotcode_name', 'fun_name');
Do shortcode
<?php echo do_shortcode('[contact-form-7 id="91" title="quote"]');?>
Menu
How to register a menu?
// Register navigation menus
function theme_register_nav_menus() {
register_nav_menus( array(
'primary-menu' => __( 'Primary Menu', 'theme-text-domain' ),
'footer-menu' => __( 'Footer Menu', 'theme-text-domain' ),
// Add more menu locations as needed
) );
}
add_action( 'after_setup_theme', 'theme_register_nav_menus' );
How to display a menu?
// Display primary menu
wp_nav_menu( array(
'theme_location' => 'primary-menu',
// Add more parameters as needed
) );
How to display the site URL?
<?php
// Retrieve the URL of the current site's homepage
$site_url = site_url();
// Retrieve the URL of a specific file within the site (e.g., style.css)
$style_url = site_url('/style.css');
?>
load_theme_textdomain()
This function loads the theme’s text domain for translation, allowing for the localization and translation of theme strings.
// Load theme text domain for translation load_theme_textdomain('customtheme', get_template_directory() . '/languages');
add_theme_support()
This function adds support for various features to the theme.
// Add theme support for post thumbnails
add_theme_support( 'post-thumbnails' );
add_action()
It is a vital function in WordPress used to attach custom functionality to specific points, known as action hooks, within the WordPress execution process.
add_action('after_setup_theme', 'customtheme_setup');
add_filter()
It is a used to add a filter to a specific hook. Filters in WordPress allow you to modify or manipulate data before it is displayed or used elsewhere in WordPress.
add_filter('the_content', 'my_content_filter_function');
get_template_directory()
This function retrieves the absolute path to the current theme’s directory on the server. It returns the directory path without the trailing slash.
// Retrieves the path to the template directory
$template_directory = get_template_directory();
get_template_directory_uri()
This function retrieves the URI (Uniform Resource Identifier) of the current theme’s directory. It returns the URI path (web address) to the theme’s directory, including the protocol (http or https).
// Retrieves the URI of the current theme's directory $template_directory_uri = get_template_directory_uri();
get_page_by_title()
This function retrieves a post object of a page based on its title. It returns the post object if a page with the specified title is found, otherwise, it returns null.
// Retrieves a page given its title
$page = get_page_by_title('Page Title');
remove_query_arg()
It’s used to remove a specific query parameter from a given URL.
$modified_url = remove_query_arg('param_to_remove', 'http://example.com/page?param1=value1¶m_to_remove=value_to_remove');
get_query_var()
It’s used to retrieve the value of a variable in the WP_Query class.
$paged = get_query_var('paged');
wp_upload_dir()
It is used to retrieve information about the upload directory, such as its path and URL. This function returns an associative array containing various details about the upload directory.
// Retrieve upload directory information
$upload_dir = wp_upload_dir();
// Display upload directory path and URL
echo "Upload directory path: " . $upload_dir['path'] . "<br>";
echo "Upload directory URL: " . $upload_dir['url'] . "<br>";
Register a script
It is a function used to register a script with WordPress to be enqueued later.
// Register a script
function register_custom_script() {
wp_register_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'register_custom_script');
wp_enqueue_script()
It is used to enqueue JavaScript files into WordPress pages.
function enqueue_custom_script() {
wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), '1.0', true); }
// Hook the function to the wp_enqueue_scripts action
add_action('wp_enqueue_scripts', 'enqueue_custom_script');
wp_enqueue_style()
It is used to enqueue CSS stylesheets into WordPress pages.
function enqueue_custom_style() {
wp_enqueue_style('custom-style', get_template_directory_uri() . '/css/custom-style.css', array(), '1.0', 'all');
}
// Hook the function to the wp_enqueue_scripts action
add_action('wp_enqueue_scripts', 'enqueue_custom_style');
Also Read
Winding Up Our WordPress Functions List
We hope this article helped you learn some useful tricks on commonly used WordPress functions list in custom theme development. Make sure to treat your functions with care and it will help your theme development.
If you need any help with custom theme development, our developers at ColorWhistle, a WordPress Development Company, we are always ready to help. If you decide to work with us, you’ll get an attractive responsive design with peak functionality.
Hire WordPress developers if you’re bored of the standard themes available in the market and looking out for something unique and exciting.
Here’s an infographic that sums up the entire post.
Looking for WordPress Development Services?
Seize and experience the transformative impact of WordPress solutions with ColorWhistle.
In quest of the Perfect WordPress Solutions Buddy?
Be unrestricted to click the other trendy writes under this title that suits your needs the best!
- Large WordPress Website Development For Businesses – A Master Guide
- Best Practices for WordPress Plugin Development
- Top WordPress eLearning Plugins for EduTech Websites
- Smart Branding and Lead Generation with WordPress Websites
- Building WordPress Kitchen Visualizer for Lead Generation
- WordPress + WP Engine + SSO – How to Build and Make Use of Possibilities
- WordPress Maintenance Mode and it’s Significance
What’s Next?
Now that you’ve had the chance to explore our blog, it’s time to take the next step and see what opportunities await!