Template is not defined.

Top 20 WordPress Functions to remember as a newbie WordPress Developer

Bloginfo() Function

The bloginfo() function is used to retrieve information about your current WordPress site.

body_class() Function – Dynamically generate HTML body tag CSS class

This function displays the class names for the body element. This is very important during theme development styling amongst other things

<body <?php body_class(); ?>>

The actual HTML output will look similar to this the code below, you can then style classes accordingly:

<body class="page page-id-18 page-parent page-template-default logged-in">

in your stylesheet, you can proceed to style the class

.page {
    /* styles for all posts within the page class */
}
.page-id-18 {
    /* styles for only page ID number 18 */
}
.logged-in {
    /* styles for all pageviews when the user is logged in */
}

You can also add custom classes to the body_class() function,

<body <?php body_class( 'class-name' ); ?>>

the output would be:

<body class="page page-id-2 page-parent page-template-default logged-in class-name">

you can also add a specific class to the body of a particular page:

add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
    if ( is_page_template( 'page-example.php' ) ) {
        $classes[] = 'example';
    }
    return $classes;
}

the result will something like this:

<body class="page page-id-7 page-template page-template-page-example page-template-page-example-php example">

add_theme_support() – for Adding theme features

Dynamic Menu Implementation with register_nav_menu() and wp_nav_menu()

The wp_nav_menu() function is used to display the navigation menu in a defined theme location. The function takes an array of arguments wp_nav_menu( array $args = array() ). However, before you can use it, you must either add_theme_support('menu') or you can register_nav_menu().

Basically, after registering your theme menu on the function hooked to the after_setup_theme of your WordPress add_action, i.e.

<?php
/*
 * You must add theme support for dynamic menu navigation in order 
 * to use the wp_nav_menu function. So in your Functions.php 
 */
function theme_features() {
    register_nav_menu(array(
        'primaryNavMenu'=>'Primary Menu',
        'footerColOneMenu'=>'Footer First Colunm Menu'
    ));
}
add_action('after_setup_theme', 'theme_features');

You can proceed to the navigation area where you want to place the menu

<!--
 * Now, in your Header.php file you can proceed to replace or comment out previous static navigation
 * with the wp_nav_menu() function
-->
<div class="site-header__menu group">
   <nav class="main-navigation">
       <?php wp_nav_menu( array(
            'theme_location'=>'primary_menu'
          ) ); ?>
       <!-- Remove or Comment out the Static Navigation list
        <ul>
            <li><a href="#">About Us</a></li>
            <li><a href="#">Programs</a></li>
            <li><a href="#">Events</a></li>
            <li><a href="#">Campuses</a></li>
            <li><a href="#">Blog</a></li>
        </ul>
       -->
   </nav>
</div>

is_Page() – Check if current page

If the is_page function is called, it checks if the query is for one of the pages specified in the parameters and returns a Boolean value.

The function syntax: is_page($page) ; where

$page can be (int|string|int[]|string[]) with a parameter like Page ID, title, slug, or an array() of such to check against. the default value is ' '

For example, if you have an unordered list and you only want to display it in the about is page, you can use the is_page function to check if the page is the ‘About us’ page. the

/*
 * Displays the unordered list if the page slug is 'about-us'.
 */
<?php if (is_page('about-us')) { ?>            
<ul>
    <li><a href="#">About Us</a></li>
    <li><a href="#">Programs</a></li>
    <li><a href="#">Events</a></li>
    <li><a href="#">Campuses</a></li>
    <li><a href="#">Blog</a></li>
</ul>
<? } ?>

is_page() can also be used to specify multiple pages

<?php if 
/*
 * Returns true when the Pages displayed is either post ID 42,
 * or post_name "about-us", or post_title "Contact".
 */
if ( is_page( array( 42, 'about-us', 'contact', 'management' ) ) ) {
     // either in about us, or contact, or management page is in view
} else {
     // none of the page about us, contact or management is in view
}
?>

paginate_links() – Add pagination to archive

Table of Contents