WordPress Functions Cheat Sheet. Functions That You Must Know

Posted by TotalDC

There is a lot of default WordPress functions you can use to return information that you need dynamically. Here is the list of useful default functions in WordPress.

  • Get post content -> the_content();
  • Get the post Excerpt -> the_excerpt();
  • Get the title of the post -> the_title();
  • Display post link -> the_permalink();
  • Display category of the post -> the_category(‘, ‘);
  • Display author of the post -> the_author();
  • Display post ID -> the_ID();
  • Edit post link -> edit_post_link();
  • Display next page URL -> next_post_link(‘ %link ‘);
  • Display previous page URL -> previous_post_link(‘ %link ‘);
  • Retrieve blogroll links -> get_links_list();
  • Retrieve all pages -> wp_list_pages();
  • Retrieve archive for the site -> wp_get_archives();
  • Retrieve all categories -> wp_list_cats();
  • Show the built-in WordPress calendar -> get_calendar();
  • Show register link -> wp_register();
  • Display login / logout links (for registered users) – > wp_loginout();

Include functions

These functions are used to include templates to your theme.

  • Include header.php and display its content -> <?php get_header(); ?>
  • Include sidebar.php -> <?php get_sidebar(); ?>
  • Include the footer.php –> <?php get_footer(); ?>
  • Include template for comments -> <?php comments_template(); ?>

Other useful header functions

  • Get WordPress site url -> site_url();
  • Get page title -> wp_title();
  • Get blog name -> bloginfo(‘name’);
  • Get blog description -> bloginfo(‘description’);
  • Get stylesheet directory URI -> get_stylesheet_directory_uri();
  • Get pat template folder -> bloginfo(‘template_url’);
  • Get Atom feed URL -> bloginfo(‘atom_url’);
  • RSS 2.0 URL -> bloginfo(‘bloginfo(‘rss2_url’);

The LOOP

The LOOP is PHP code used by WordPress to return posts. It loops through your posts and displays them on the current page. It also formats the post according to specified parameters.

<?php if ( have_posts() ) : ?>
 <?php while ( have_posts() ) : the_post(); ?>

 <?php endwhile; ?>
<?php endif; ?>

WordPress menu and sidebars

Default Navigation Menu ->

<?php wp_nav_menu(); ?>

Specific Navigation Menu ->

<?php wp_nav_menu( array('menu' => My Navigation' )); ?>

Category Based Navigation ->

<ul id="menu">
<li <?php if(is_home()) { ?> class="current-cat" <?php } ?>>
<a href="<?php bloginfo('home'); ?>">Home</a></li>
<?php wp_list_categories('title_li=&orderby=id');?>
</ul>

Page Based Navigation ->

<ul id="menu">
<li <?php if(is_home()) { ?> class="current-page-item" <?php } ?>>
<a href="<?php bloginfo('home'); ?>">Home</a></li>
<?php wp_list_pages('sort_column=menu_order&depth=1&title_li=');?>
</ul>

Registering new sidebar

You can add the following code to your functions.php file to register new sidebar.

add_action( 'widgets_init', 'theme_slug_widgets_init' );
function theme_slug_widgets_init() {
 register_sidebar( array(
 'name' => __( 'My Sidebar', 'theme-slug' ),
 'id' => 'sidebar-1',
 'description' => __( 'Description', 'theme-slug' ),
 'before_widget' => '<li id="%1$s" class="widget %2$s">',
 'after_widget' => '</li>',
 'before_title' => '<h2 class="widgettitle">',
 'after_title' => '</h2>',
 ) );
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: