php - Use wordpress shortcode in post title -
i trying use following shortcode in wordpress post title. shortcode looks following:
//use [year] in posts. function year_shortcode() { $year = date('y'); return $year; } add_shortcode('year', 'year_shortcode');
any suggestions how execute shortcode in post title?
i appreciate replies!
you can absolutely use shortcode in title. need use wordpress hooks system run shortcode when title called. if want have shortcode [year]
spits out current year, you'll create shortcode:
add_shortcode( 'year', 'sc_year' ); function sc_year(){ return date( 'y' ); }
then, hook filter the_title()
run shortcode:
add_filter( 'the_title', 'my_shortcode_title' ); function my_shortcode_title( $title ){ return do_shortcode( $title ); }
that takes care of post/page title, you'll want run single_post_title
hook used in wp_head
on title tag on site. way, browser show proper title well:
add_filter( 'single_post_title', 'my_shortcode_title' );
note: don't need separate function here because it's running exact same code. total code 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 ); }
Comments
Post a Comment