Search
Close this search box.
Search
Close this search box.

Basic WordPress functions

Here is a list of the wordpress functions I use the most.

The loop

while (have_posts()) : the_post();
.......
endwhile;

get_template_directory_uri

Retrieve template directory URI for the current theme.

Note: Does not return a trailing slash following the directory address.

<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/my_image.jpg">

get_the_title

This function will return the title of a post for a given post ID. If the post is protected or private, the word “Protected: ” or “Private: ” will be prepended to the title. It can be used inside or outside of The Loop. If used outside the loop an ID must be specified.

<?php echo get_the_title($ID); ?>

get_the_ID

Retrieve the numeric ID of the current post. This tag must be within The Loop.

<?php get_the_ID(); ?>

get_the_category

To show the first category name only use:

<?php
$category = get_the_category(); 
echo $category[0]->cat_name;
?>

get_the_category_by_ID

Retrieve category name based on category ID.

 <?php get_the_category_by_ID( $cat_ID ); ?>

How to get the current category

You can use this in category.php.

$cat = get_query_var('cat');
$yourcat = get_category($cat);
echo 'the category id is ' . $yourcat->term_id;

How to get the category url

$category = get_the_category();
 $category_link = get_category_link( $category[0]->term_id );

cat_is_ancestor_of

This Conditional Tag Check if a category is an ancestor of another category. This is a boolean function, meaning it returns either TRUE or FALSE.

<?php cat_is_ancestor_of( $cat1, $cat2 ); ?>

get_term

Get all Term data from database by Term ID.

$term = get_term( ’41’, ‘pa_syskevasia’ );

get_term_by

Get all Term data from database by Term field and data.

 <?php get_term_by( $field, $value, $taxonomy ) ?>

For example:

 <?php get_term_by( 'slug', 'term-slug', 'taxonomy-slug' ) ?>

get_terms

Retrieve the terms in a taxonomy or list of taxonomies.

$terms = get_terms($taxonomy, $args);

single_term_title

Displays or returns the term title for the current page.

single_term_title();

get_the_date

The get_the_date template tag retrieves the date the current $post was written.

<?php 
echo get_the_date('l, F j, Y');
// echoes Friday, September 24, 2004
?>

get_permalink

Returns the permalink to a post or page for use in PHP. It does NOT display the permalink and can be used outside of The Loop. On failure returns false.

<?php $permalink = get_permalink( $id ); ?>

the_post_thumbnail

Gets the Featured Image as set in post’s or page’s edit screen and returns an HTML image element representing a Featured Image, if there is any, otherwise an empty string.

<?php the_post_thumbnail('large'); ?>

get_the_post_thumbnail

The same as the_post_thumbnail, but needs an id for it to work.

<?php 
$atts = array( 
    'class' => 'custom_class_1 custom_class_2',
    'attribute_1' => 'attribute_value'
);
$featured_image = get_the_post_thumbnail($id, 'large', $atts);
echo $featured_image;
// echoes <img class="custom_class_1 custom_class_2 attachment-large wp-post-image" attribute_1="attribute_value" width="..." height="..." alt="..." src="...">
?>

wp_get_attachment_image_src

Returns an array with the image attributes “url”, “width” and “height”, of an image attachment file.

<?php wp_get_attachment_image_src( $attachment_id, $size, $icon ); ?>

For example:

<?php 
$image = wp_get_attachment_image_src( 56, 'thumbnail'); 
$image = wp_get_attachment_image_src( 56, 'medium');
$image = wp_get_attachment_image_src( 56, 'large');
$image = wp_get_attachment_image_src( 56, 'full');
$image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full'); // use this for current post

For just the image src, use the first element in the returned array.

<img src="<?php echo $image[0] ?>">

In order to find the ID of the image attachment file, you can use this snippet.

How to get an image title, caption and description

$image_title = get_post($attachment_id)->post_title;
$image_caption = get_post($attachment_id)->post_excerpt;
$image_description = get_post($attachment_id)->post_content;

get_page_children

$page_childen_query = new WP_Query();
$pages = $page_childen_query->query(array('post_type' => 'page', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'post_parent' => get_the_ID()));
$page_childen = get_page_children(get_the_ID(), $pages);
foreach ($page_childen as $child) {
    echo $child->ID; // do something with the child
}

 next_post_link

next_post_link('%link', 'My custom text');

Add titles to previous_post_link & next_post_link

$p = get_adjacent_post(false, '', true);
if(!empty($p)) echo '<div class="prev"><a href="' . get_permalink($p->ID) . '" title="' . $p->post_title . '">' . $p->post_title . '</a></div>';

$n = get_adjacent_post(false, '', false);
if(!empty($n)) echo '<div class="next"><a href="' . get_permalink($n->ID) . '" title="' . $n->post_title . '">' . $n->post_title . '</a></div>';

How to check if the current user is an administrator

if (current_user_can('manage_options')) {
// do something
}

 How to check if post/page has a gallery

if( get_post_gallery() ){
echo 'has gallery';
} else {
echo 'has no gallery';
}

How to get images from a post/page gallery

Retrieves an array of image URLs that belong to the first gallery added to the specified post. Notice that it returns the thumbnails.To get larger images look here.

$post_id = get_the_ID();
$gallery_images = get_post_gallery_images( $post_id );
foreach ($gallery_images as $img) {
    ?>
    <img src="<?php echo $img; ?>">
    <?php
}

How to test if the current browser runs on a mobile device

The wp_is_mobile function, returns a true in tablets, so you should better not use it. You can try instead the mobble plugin and use this function instead:

if ( is_mobile() ) {
     // if mobile, run some code here
}