WordPress Functions List

WordPress Functions list for Custom Theme Development

Updated : July 11, 2018

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 are going to save you the trouble by providing some of the 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.

WordPress Functions list

  1. 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;
    ?>
  2. How to list custom post-type?

    <?php
    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $postsPerPage = 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’,
  3. How to show individual page content in front-end?

    <?phpwhile ( have_posts() ) : the_post();
    get_template_part( ‘template-parts/content’, ‘page’ );
    if ( comments_open() || get_comments_number() ) {
    comments_template();
    }
    endwhile;
    ?>
  4. 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();
    ?>
  5. 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;
      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
  6. 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
    l, 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/06 at 12:50 AM
    Y/m/d \a\t g:ia – 2010/11/06 at 12:50am
    Y/m/d g:i:s A – 2010/11/06 12:50:48 AM
    Y/m/d – 2010/11/06Date as Year, Month, Date in Heading
    <?php the_date(‘Y-m-d’, ‘<h2>’, ‘</h2>’); ?>
  7. 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/pageFor more tags refer this pdf
  8. How to create a Taxonomy?

    Registering a taxonomy:

    Here is an example of registering a “people” taxonomyfunction 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’ );
  9. How to display Terms?

    The following is an example of the_terms() function:<?php the_terms( $post->ID, ‘category’, ‘categories: ‘, ‘ / ‘ ); ?>
  10. 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() . ‘”>’ . __( ‘Log Out’ ) . ‘</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_register_menu’, 199, 2 );
  11. 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>
  12. How to insert the post manually or custom?

    Post insertion/removal

    • wp_delete_post

      <?php wp_delete_post( $postid, true); ?>
    • Wp_insert_post

      <?php
      if(isset($_POST[‘new_post’]) == ‘1’) {
      $post_title = $_POST[‘post_title’];
      $post_category = $_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 you to the newly created post
      $post = get_post($post_id);
      wp_redirect($post->guid);
      }
      ?>
    • 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>’ ); ?>
  13. 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 404 "Not Found"]‘ ); ?>

Winding Up 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 are always ready to help. If you decide to work with us, you’ll get an attractive responsive design with peak functionality.

Hire our 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.

WordPress Functions List Infographic

About the Author
Sankarnarayan

Sankar

The founder and mastermind behind ColorWhistle is Sankarnarayan, a professional with over twelve years of experience and a passion for website design services and digital marketing services.

Our team has a wide range of skills and expertise and we always put our clients’ satisfaction first.

This is what sets us apart from the competition – an eye for detail and the best website development services from the start to the completion of your project.

Post Categories

Get Our Blog Updates And Special Offers At Your Inbox