• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to user navigation

PopWP

WordPress and Genesis Tutorials

  • Get Started
  • About
  • Archive
  • Services
  • Membership
  • My Account

Shortcode for Post Title in Genesis and WordPress

Last Updated on August 21, 2019 Favorited: 0 times

This tutorial provides the steps to create and add a shortcode for post title using WordPress and Genesis way.

Use Only one shortcode as per your requirement.

For WordPress Themes

Type 1. Output Post title as Text

add_shortcode( 'custom_post_title', 'custom_post_title_shortcode' );
function custom_post_title_shortcode() {
	return get_the_title();
}

Type 2. Output Post title with additional HTML

add_shortcode( 'custom_post_title', 'custom_post_title_shortcode' );
function custom_post_title_shortcode() {
	return '<h4 class="custom-post-title">'.get_the_title().'</h4>';
}

Type 3. Output Post title with Post link and additional HTML

add_shortcode( 'custom_post_title', 'custom_post_title_shortcode' );
function custom_post_title_shortcode() {
	return '<h4 class="entry-title" itemprop="headline"><a href=" '.get_permalink().'">'.get_the_title().'</a></h4>';
}

or

add_shortcode( 'custom_post_title', 'custom_post_title_shortcode' );
function custom_post_title_shortcode() {
	return '<h4 class="entry-title" itemprop="headline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>';
}

Usage:

[custom_post_title]

You can restrict the shortcode to work on specific post and location by following WordPress conditional Tags.

Bonus:

/* title to get the post title  */
function my_custom_post_title() {
	global $wp_query;
	return get_the_title($wp_query->post->ID);
  }
  
/* Add shortcode */
add_shortcode('wp_custom_post_title', 'my_custom_post_title');

Usage:

[wp_custom_post_title]

For Genesis Framework

You can output post title in Genesis Framework using genesis_do_post_title();

in any Type of mentioned in for WordPress themes.

or

Use this in functions.php

/**
 * Post title shortcode.
 * `[post_title]` to use in the loop. Or `[post_title id="123"]` to get a specific post's title, by ID
 * 
 * @return  string  The post's title.
 */
add_shortcode( 'post_title', function( $atts ) {
    $atts = shortcode_atts( array(
        'id' => get_the_ID(),
    ), $atts, 'post_title' );
    return get_the_title( absint( $atts['id'] ) );
});

 

Related Posts

  • Deal custom post type container with custom fields in WordPress
  • Callout Box design in WordPress

Categories: Free Content, Genesis Tutorials, WordPress Tutorials Tags: post title, shortcode

Reader Interactions

Primary Sidebar

Search

WPEngine WordPress Hosting, Perfected.

Hosting You are looking for?.
Perfect solution for small business to global enterprise.

Learn more

StudioPress Genesis Theme Framework

The No.1 Theme Provider.
Creative, SEO rich Theme for all niche projects.

Learn more

Categories

  • Free Content
  • Genesis Tutorials
  • Premium Content
  • Snippets
  • What's New?
  • WordPress Tutorials

Tag Cloud

Archive Background Section blog canvas menu center logo columns conditional tags CSS CSS Grid custom Customizer custom post type Custom Post Types custom template Custom Widget effect eNews Extended Featured Image front-page Genesis Genesis Sample header right hero section Image Background js layout left menu Logo menu Navigation Menu newsletter post page related posts responsive menu search search widget Shrinking Logo site header slide in-out Stylesheet Template Utility Bar Video Background widgets WordPress

Built with Genesis Framework + WordPress by Aryan Raj

  • Contact
  • FAQ
  • Disclaimer
  • Privacy Policy
  • Copyright Policy
  • Terms of Service