Examples
This page contains examples of how to use the plugin. In the following examples, the secondary title has been set to "Chapter 1" and the title is "Introduction".
Automatically
The "Title Format" setting allows you to specify the way the title is displayed on your site. You can adjust this on Secondary Title's settings page. Use the following placeholders to customize the output:
%secondary_title%
- The secondary title%title%
- The original title of the post/page
1. Default
%secondary_title%: %title%
Example result: Chapter 1: Introduction
2. With HTML
You can use HTML tags to style and format the output as you like.
<span style="color:#f0f;">%secondary_title%:</span> %title%
Example result: Chapter 1: Introduction
Note
For security reasons, certain HTML tags, such as the <script>
tag, are not allowed in the title format and will be stripped.
PHP Function
You can use the get_secondary_title()
function to retrieve the secondary title in your theme or plugin files. For a full list of all functions, their parameters and how to use them, see the functions reference.
1. Basic
Use echo
to display the secondary title:
echo get_secondary_title();
Or use the_secondary_title()
:
the_secondary_title();
2. With Parameters
You can pass the post ID, prefix, and suffix as parameters to the function. The post ID is optional and defaults to the current post.
echo get_secondary_title(521, '<strong>', '</strong>');
Example result: <strong>Chapter 1</strong>
Avoid Fatal Error
To avoid the PHP error "Fatal error: Call to undefined function get_secondary_title()" when the plugin isn't installed or activated, use PHP's function_exists()
function to check if the function is available:
if (function_exists('get_secondary_title')) {
echo get_secondary_title();
}
Or via PHP's shorthand ternary operator:
function_exists('get_secondary_title') ?: get_secondary_title();
As Shortcode
You can also use the secondary title as a shortcode in your posts and pages. The shortcode [secondary_title]
will display the secondary title of the current post or page.
See the shortcodes reference for available parameters and examples.