I am trying to use the following shortcode in the wordpress post title. The shortcode looks like the following:
//Use [year] in your posts.
function year_shortcode() {
$year = date('Y');
return $year;
}
add_shortcode('year', 'year_shortcode');
Any suggestions how to execute this shortcode in the post title?
I appreciate your replies!
You can absolutely use a shortcode in a title. You just need to use the WordPress hooks system to run the shortcode when the title is called. So if you want to have a shortcode [year] that spits out the current year, you'll create the shortcode:
add_shortcode( 'year', 'sc_year' );
function sc_year(){
return date( 'Y' );
}
Then, hook into the filter for the_title() to run your shortcode:
add_filter( 'the_title', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
That takes care of the Post/Page title, but you'll also want to run it for the single_post_title hook which is used in wp_head on the title tag on your site. That way, the browser will show the proper title as well:
add_filter( 'single_post_title', 'my_shortcode_title' );
Note: You don't need a separate function here because it's running the exact same code. So your total code would look something like this:
add_shortcode( 'year', 'sc_year' );
function sc_year(){
return date( 'Y' );
}
add_filter( 'single_post_title', 'my_shortcode_title' );
add_filter( 'the_title', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
I don't think you can apply (save in admin post edit screen) a shortcode in the post title with a shortcode. The post_title is sanitize to avoid tag or shortcode, the post title is used by many function that a shortcode can break.
To make modification on the post_title, you can use the filter the_title
add_filter('the _title', 'yourfunction');
function yourfunction($title){
global $post;
// if you set a custom field on the post where you want to display the year
if(get_post_meta($post->ID, 'display_year', true) == 1){
$title = $title. ' '. date('Y');
}
return $title;
}
Hope it helps
Please add this code in 'function.php'. Try this.
<?php
function TitleFunction($title)
{
global $post;
$title = $title. ' ' .get_the_date('Y');
return $title;
}
add_filter( 'the_title', 'TitleFunction', 10, 2 );
?>
Related
I used some do_shortcode functions to work shortcode in post title and description. but i am unable to use shortcode in notification post title or sharing preview title. Here you can see my sample posts.
I want to add automatic date in my WordPress posts and posts title. I used Following codes to adding current date in WordPress posts.
add_filter( 'wpseo_title', 'do_shortcode' ); // activate shortcode in Yoast Title
add_filter( 'wpseo_metadesc', 'do_shortcode' ); // activate shortcode in Yoast Meta Description
add_filter( 'the_title', 'do_shortcode' ); // activate shortcode in WP Title
add_shortcode( 'current_date', 'mth_footer_date2' );
function mth_footer_date2() {
ob_start();
echo date("l, jS F Y");
return ob_get_clean();
}
Above code is working fine.. but I unable to see date in Sharing preview or Notification Title. Kindly provide solution.
On my competitor's website, I seen that they also adding current date using shortcode. They use another code to done this. on those website, date appearing in both sharing preview and notifications. But, when I used that, I getting some errors. Here is that code.
function wpb_date_today($atts, $content = null) {
extract( shortcode_atts( array(
'format' => ''
), $atts ) );
if ($atts['format'] == '') {
$date_time .= date(get_option('date_format'));
} else {
$date_time .= date($atts['format']);
}
return $date_time;
}
add_shortcode('date-today','wpb_date_today');
They use [date-today] to represent current date everywhere.
I want to display first word of posts title only on frontpage of my website.
My Problem: I have the below coding, but it does not apply to the posts title of frontpage, but it effect the website menu title and blog main title, while i want to target only posts title on my website frontpage.
add_filter( 'the_title', 'wpse_75691_trim_words' );
function wpse_75691_trim_words( $title )
{
// Limit the title to exactly one word only on the frontpage.
if (is_front_page()) {
return wp_trim_words( $title, 1, '' );
}
// Otherwise return the full title.
return $title;
}
What i want to do:
1: The above code should not apply to the menu title or blog title
2: The above code should display only first word of post title on my frontpage.
My posts title on frontpage looks like this in inspect element:
Hint: I think, if the above code target only h3 title, then it will effect only the posts title and no other title tags on my frontpage. So, can anyone modify the coding to apply the function only to the post title and it should work.
For trimming the title only for "post" title on the "front page", then you could do something like this:
add_filter( 'the_title', 'your_them_custom_title', 10, 2 );
function your_them_custom_title( $title, $post_id )
{
if( is_front_page() && 'post' === get_post_type( $post_id ) && !is_admin() ) {
return wp_trim_words( $title, 1, '' );
}
return $title;
}
And also if you want to keep the naviagtion menu title intact then you could do something like this:
add_filter( 'nav_menu_item_title', 'your_them_nav_title', 10, 4 );
function your_them_nav_title( $title, $item, $args, $depth )
{
# Removing your custom title for the nav menu
remove_filter( 'the_title', 'your_them_custom_title', 10, 2 );
$title = get_the_title( $item->object_id );
# Adding your custom title back
add_filter( 'the_title', 'your_them_custom_title', 10, 2 );
return $title;
}
UPDATE:
If you're NOT sure which page you're referring to, then you can run this conditional check to see which page you need to check to run the wp_trim_words on.
"Copy/past" the following code to the functions.php to determine the right page you're referring to:
if ( is_front_page() && is_home() ) {
echo "default index page which is also the front page";
} elseif ( is_front_page()){
echo "custom front page";
} elseif ( is_home()){
echo "default index page";
} else {
echo "neither front nor blog index";
}
I have a little piece of code that makes a shortcode in my WordPress theme function file.
This is the code:
function myshort_title($atts){
$title = get_the_title();
$short_title = wp_trim_words( $title, 2, '' );
return $short_title;
}
add_shortcode( 'short_title', 'myshort_title' );
and I use it like this:
[short_title]
Ok...what I want is the result of that shortcode to be written permanently into my WordPress posts so that when I edit my posts I don't want to see that shortcode, I want to see the result.
I am just working on filter titles in pages and posts and everything works as expected but when I view the single.php whereas it shows the actual post as should be, the function hook filter i am working on inside functions.php of my plugin is filtering every title of each post of my blog, I was expecting to filter just the title of the current post:
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
if((in_the_loop())){
/**/
}
}
any help is grateful.
Thanks ;)
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
if((some condition true)){
$add_this = 'some string';
$new_title = $title.' '.$add_this;
return $new_title;
}
return $title;
}
when you view any post it is shown through single.php so every post becomes current post when you view it
I understand now that for the filter title hook it is the same viewing a single post from single.php than viewing the whole blog. would there be a way of filtering with a condition to only retrieve the title of the current post in single.php?, I did try "get_the_title(get_the_ID())" like this but obviously I cant nest it.
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
if((in_the_loop())){
if(is_single()){
$title=get_the_title(get_the_ID());
}
}
}
I have this plugin installed on my WordPress:
http://wordpress.org/plugins/put/
I’m trying to make a plugin that uses the UI Tabs plugin inside my own plugin.
My plugin code so far:
function load_jquery(){
echo '<link rel=\'stylesheet\' id=\'jquery-ui-tabs-css\' href=\'http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/smoothness/jquery-ui.css?ver=1.9.2\' type=\'text/css\' media=\'all\' />';
}
add_action('wp_head','load_jquery');
function print_tabs(){
echo do_shortcode('[tab name="Tab"]-[/tab]');
echo do_shortcode('[end_tabset]');
}
add_shortcode('print_tabs', 'print_tabs');
Now if I use the [print_tabs] shortcode in a new page, it should look like this:
http://img835.imageshack.us/img835/4905/workingp.png
But it’s not working, and it looks like this:
http://imageshack.us/a/img62/9772/notworkingm.png
What could be the problem here?
The problem, from what I can see in put.php in the Post UI Tabs plugin is that the shortcodes are only added during the "the_content" filter in a function called "on_the_content".
add_filter( 'the_content', array( $this, 'on_the_content' ), 7 ); // Priority 7 - before wpautop
(line 96 of put.php)
And that function looks like:
public function on_the_content( $content ) {
$this->has_tabs = (bool) apply_filters( 'put_decide_has_tabs', $this->has_tabs );
if( !$this->has_tabs )
return $content;
global $shortcode_tags;
// Backup current registered shortcodes and clear them all out
$orig_shortcode_tags = $shortcode_tags;
$shortcode_tags = array();
add_shortcode( 'tab', array( $this, 'on_tab_shortcode' ) );
add_shortcode( 'end_tabset', array( $this, 'on_tab_end_shortcode' ) );
// Do the shortcode(only the tab shortcode is registered at this point)
$content = do_shortcode( $content );
// Put the original shortcodes back
$shortcode_tags = $orig_shortcode_tags;
return $content;
}
(starting at line 118 of put.php)
So, given how this plugin is written by modifying the content with a filter which in turn adds the shortcodes when that filter is run, what you're seeing is probably happening because when you call "do_shortcode" those shortcodes don't actually exist.
What echoing do_shortcode is doing then, is just coughing up the text.
Unfortunately, because of the way the Post UI Tabs plugin is written, you may not be able to do what you're trying to do.