Learn WordPress Template Tags

Introduction

  1. Just the template tags I actually think are interesting…This page is primarily based on Bans Unvers’ The Tuts+ Guide to Template Tags.
  2. Parameters which are optional are italicized.
  3. Why are there two very similar template tags frequently? (Usually) the one with get_ returns the value while the one without get_ echoes the value.
  4. There are regular template tags (usually look like get_the_blank() or the_blank()) and then there are conditional tags (usually look like is_blank() or has_blank()).[ref]You cannot use conditional tags in functions.php as it is loaded before the query and thus has no data to run against; unless you create a function that hooks into an action/filter that triggers later.[/ref]
  5. Items are organized alphabetically after one has dropped the prefixes, e.g. get_the_author() is alphabetized as author(); this does result in some duplication, since in this case both get_the_author() and the_author() become author(). In these cases those with get_ have preceded those without.
  6. The following template tags must be within the loop:
    1. the_content()
    2. the_excerpt()
    3. next_post()
    4. previous_post()

Template Tags

  • get_archives_link()
  • the_attachment_link()
  • get_the_author()
  • the_author()
  • get_bloginfo($show) – Options include url, description, rss_url, and so on. See Unvers’ list for more.
  • bloginfo() – Parameters – ‘name’ – Name of blog; ‘version’
  • get_body_class($class
  • body_class()
  • get_calendar()
  • the_category($separator, $parents, $post_ID) – Categories associated with a particular post/page.
  • get_the_category_list($separator, $parents, $post_ID– $parents is a separator when the category has a parent, for example one could use a colon as a separator.
  • get_the_category_rss($feed_type)
  • the_category_rss($feed_type)
  • category_description($category_ID)
  • comments_open() – Comments allowed on current post.
  • get_the_content($more_link_text, $strip_teaser)
  • the_content($more_link_text, $strip_teaser)
  • get_the_content_feed($feed_type)
  • the_content_feed($feed_type)
  • current_theme_supports() – Check if theme supports a specific feature.
  • get_the_date($date_format, $post_ID) – Of the post, not at this moment.
  • the_date($date_format, $before, $after, $echo)[ref]Note that like the_title() this can return or echo.[/ref] – Same as above.
  • edit_tag_link($link, $before, $after, $tag) – $link is the text to be displayed for the link; $tag default is null.
  • get_edit_tag_link($tag_ID, $taxonomy) – $taxonomy default is post_tag.
  • get_the_excerpt()
  • the_excerpt()
  • the_excerpt_rss()[ref]Seems to me this should be called the_excerpt_feed?[/ref]
  • get_feed_link()
  • the_feed_link()
  • get_footer()
  • get_header() –  For this and for get_sidebar and get_footer you can create custom files – header-{custom_template}.php – which can then be called like so: get_header( ‘header-custom-template’ );
  • has_excerpt()
  • has_nav_menu() – Has a registered nav menu location have a menu assigned to it.
  • has_tag() – Must be used inside loop, same parameters as is_tag.
  • has_term() – Used liked, has_term(‘green’, ‘color’) where green is the term and taxonomy is color, can use array, e.g. has_term( array(‘green’, ‘orange’, ‘blue’), ‘color’)
  • the_ID() – Of the post/page.
  • in_the_loop()
  • is_404()
  • is_admin() – If an admin is logged in and one is viewing an admin interface.
  • is_archive() – Includes category, tag, author, and date archive pages.
  • is_attachment()
  • is_author() – Parameters: ID, nickname, nicename, or array (array is OR based not AND).
  • is_category() – Can take parameters for ID, category slug, category name, or array combination of these.
  • is_child_theme()
  • is_comments_popup()
  • is_date() – A date-based archive page (month, year, daily, time).
    • See also is_year(), is_month(), is_day(), is_time(), is_new_day()
  • is_feed()
  • is_front_page() – Either blog posts page or static front page.
  • is_home() – Only occurs for the blog posts page, not on a static front page.
  • is_main_site() – In a multisite network.
  • is_multi_author()
  • is_multisite()
  • is_page() – See is_single for parameters.
  • is_page_template() – Was it a custom page template used for this page? Determine if a specific page template is being used, e.g. is_page_template( ‘about-page.php’ ).
  • is_plugin_active()
  • is_post_type_archive()
  • is_post_type_hierarchical( $post_type )
  • is_preview() – Post is in draft mode.
  • is_search() – Search results page.
  • is_sidebar_active()
  • is_single() – Can take parameters – e.g., post ID, title, or slug, can also take an array of these.
  • is_singular() – True for any is_single, is_page, or is_attachment.
  • is_sticky()
  • is_super_admin – In multisite.
  • is_tag() – Can take a specific tag or array of tags.
  • is_tax() – Can take parameters of taxonomy, slug, or array of these.
  • is_trackback()
  • the_meta() – Custom fields associated with the post or page. Displayed in an unordered list.
  • get_the_modified_author()
  • the_modified_author()
  • get_the_modified_date($date_format)
  • the_modified_date($date_format, $before, $after, $echo) – Can return or echo.
  • get_page_template() – Returns path of template used by page.
  • get_page_template_slug() – Returns value of _wp_page_template, a value held if the page is a custom template.
    • This value is from the wp_postmeta table.
    • Custom fields starting with _ do not display in edit screen’s custom fields module.
  • get_permalink(ID) – Used to get the permalink for a specific page by using its ID, which doesn’t change even if the permalink changes.
  • pings_open()
  • get_next_posts_link()
  • next_posts_link()
  • next_post_link()
  • get_posts_nav_link()
  • posts_nav_link()
  • get_the_post_thumbnail()
  • the_post_thumbnail()
  • post_type_archive_title()
  • get_post_type() – Not truly conditional tag.
  • post_type_exists()
  • get_previous_posts_link()
  • previous_posts_link()
  • previous_post_link()
  • get_search_form()
  • get_search_link()
  • get_search_query()
  • the_search_query()
  • the_shortlink() – Uses URL of site and ID of post/page.
  • get_sidebar()
  • single_cat_title($prefix$echo) – Used on archive page; $prefix is text you want prepended to the title.
  • single_tag_title($prefix, $echo)
  • single_term_title($prefix, $echo)
  • get_stylesheet_directory_uri(); – Gets the full path to the directory of the theme, including if it is a child theme.
    • See get_template_directory_uri();
  • the_tags()
  • tag_description($tag_ID)
  • get_the_tag_list($before, $sep, $after)
  • the_tag_list($before, $sep, $after) – $sep is for separator.
  • taxonomy_exists()
  • get_template_directory_uri(); – Gets the full path to the directory of theme, unless it is a child theme, in which case it gets the parent theme directory.
    • See get_stylesheet_directory_uri()
  • the_terms($post_ID, $taxonomy, $before, $sep, $after)
  • term_description($term_ID, $taxonomy)
  • get_the_term_list($post_ID, $taxonomy, $before, $sep, $after)
  • get_template_part() – Used for other files you want to include besides header, sidebar, and footer.
    • slug-template.php
    • e.g. content-product.php
    • ex. get_template_part( ‘content’, ‘product’ );
      •  These can be placed in subdirectories, but then it becomes: get_template_part( ‘directory/content’, ‘product’ );
  • get_the_time($time_format, $post_ID) – Of the post, not at this moment.
  • the_time($time_format) – When post was made.
  • get_the_title($post_ID)
  • the_title($before, $after, $echo) – Title of page or post. Can return or echo.
  • the_title_attribute($args) – Escapes characters which would cause issues for HMTL.
    • The $args here can be an array containing ‘before’, ‘after’, ‘echo’, and ‘post’ – the last being the ID of the post to pull the title from.
  • the_title_rss()[ref]Seems like this should be called the_title_feed?[/ref]
  • the_taxonomies($args)
    • $args is an array, takes arguments: post (this is post_ID), before, sep, after, template (how the taxonomy should be formatted, default is: ‘%s: %t’ where %s is the taxonomy label and %t is the list of term links
    • Displays taxonomies associated with post as well as the terms of the taxonomies.
  • user_trailingslashit()
  • wp_dropdown_categories($args) – $args is an array: show_option_all, show_option_none, option_none_value (what value to use when no category selected), orderby, order, show_count, hide_empty, child_of, exclude, echo, depth, tab_index, name (name of select element), id, class, selected (default category), taxonomy.
  • wp_generate_tag_cloud($tags, $args)
    • $tags is an array of tags
    • $args is an array: smallest, largest, unit, number, format, separator, orderby, order, topic_count_text, topic_count_text_callback, topic_count_scale_callback, filter
  • wp_get_archives()
  • wp_get_attachment_image()
  • wp_get_attachment_image_src()
  • wp_get_attachment_link()
  • wp_get_attachment_metadata()
  • wp_get_theme() ->get_page_templates() – Lists all custom templates currently available to theme.
  • wp_link_pages($args) – Used in association with nextpage.
    • $args is an array: ‘before’, ‘after’, ‘link_before’, ‘link_after’, ‘next_or_number’, ‘sep’ (separator text), ‘nextpagelink’ (text), ‘previouspagelink’ (text), ‘pagelink’, ‘echo’
  • wp_list_cats()
  • wp_list_categories($args)
    • $args is an array: show_option_all (links to home page), show_option_none (text if no categories),  orderby (Options: name, ID, slug, count, term_group), order (DESC, ASC (default)), style (list = unordered list, none = separated by br), show_count (post count next to each category link), hide_empty (categories), use_desc_for_title (description used for title), child_of (display only if child of x category), feed (show feed links next to regular links), feed_type (rss, rss2, atom, rdf), feed_image (give a path to show an image instead of text for image link), exclude (comma separate categories), exclude_tree (must use if hierarchical argument set to 1), current_category (add a class to tags which fall into the current category, category name: current_cat, e.g., to allow CSS formatting), hierarchical (should be show hierarchically?), title_li (give a title before list), echo, depth, taxonomy
  • wp_list_pages()
  • wp_login_form()
  • wp_loginout()
  • wp_login_url()
  • wp_logout_url()
  • wp_lostpassword_url()
  • wp_nav_menu($args)
  • wp_register()
  • wp_tag_cloud($args)
    • $args is an array: smallest (text size), largest, unit (e.g. pt), number (how many tags?), format (flat = by spaces, list = unordered list, array = PHP array), separator, orderby, order, include, exclude, taxonomy, link, post_type, echo
  • wp_title($sep, $echo, $seplocation)

Bibliography

ToDo:

  • Run  through all of Unvers’ Template Tags articles again.
  • Move wp_ in line with get_ and the_.
  • Determine why naming variation prefix of wp_ as opposed to get_ or the_.