Skip to main content

Post

  • the_post()
  • the_title()
  • the_content()
  • the_permalink()
  • get_the_ID()
  • wp_get_parent_post_id(id)
  • get_the_title(id)
  • get_permalink(id)
  • wp_list_pages(array)
  • wp_nav_menu(array)
  • is_page()

the_post()

  • Iterate the post index in the loop.
  • checks whether the loop has started and then sets the current post by moving, each time, to the next post in the queue.
<?while(have_posts()){
the_post();?>
<?}?>

the_title()

  • Display the current post title with optional HTML markup. It is accepted to use inside the WordPress Loop.
  • If the post is protected or private and you are on front-end, then "Protected" or "Private" will be displayed before the post title.
<?while(have_posts()){
the_post();?>
<h2><?the_title();?></h2>
<?}?>

the_content()

  • Display the post content. It is template tag and must be used inside the loop.
  • Besides using this function in the loop, it can be used on a single page like page.php, single.php.
<?while(have_posts()){
the_post();?>
<p><?the_content();?></p>
<?}?>
  • displays link (URL) to the post that is currently being processed in the Loop.
  • this template tag can be used only inside the Loop of WordPress.
<?while(have_posts()){
the_post();?>
<h2><a href="<?the_permalink();?>"><?the_title();?></a></h2>
<p><?the_content();?></p>
<?}?>

get_the_ID()

  • Retrieves the ID of the current post in the WordPress Loop.
<?while(have_posts()){
the_post();?>
echo get_the_ID();
<?}?>

wp_get_parent_post_id(id)

  • Returns the ID of the post’s parent.
<?while(have_posts()){
the_post();?>
echo wp_get_parent_post_id(get_the_ID());
<?}?>

get_the_title(id)

  • Retrieves the post title based on the id
<?while(have_posts()){
the_post();?>
echo get_the_title(get_the_ID());
<?}?>

get_permalink(id)

  • Retrieves the post title based on the id
<?while(have_posts()){
the_post();?>
echo get_permalink(get_the_ID());
<?}?>

wp_list_pages(array)

  • Retrieves or displays a list of pages (or hierarchical post type items) in list (li) format.
<?$theParent = wp_get_parent_post_id(get_the_ID());
if($theParent){
$findChildrenOf = $theParent
}else{
$findChildrenOf = get_the_ID();
}
wp_list_pages(array(
'title_li': NULL,
'child_of':$findChildrenOf
));?>
  • Before we can display menu, we need to register in the functions.php file
function my_features(){
register_nav_menu('headerMenuLocation','Header Menu Location');
}
add_action('after_setup_theme','my_features');
  • Then we can go into header.php file to add the following code in place where we want our menu to appear
<nav class="menu_navigation">
<?wp_nav_menu(array('theme_location'=>'headerMenuLocation'));?>
</nav>

is_page()

  • Check the current page's slug, so that we can add a class to it
<?if(is_page('about-us')) echo 'class="current-menu-item"';?>