最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - Add PHP code after title in single post pages?

programmeradmin0浏览0评论

I tried to add PHP code after the post title in single post pages by adding a filter to functions.php, but this did not work:

function theme_slug_filter_the_content( $content ) {
    $custom_content = 'MY CODES';
    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'theme_slug_filter_the_content' );

I tried to add PHP code after the post title in single post pages by adding a filter to functions.php, but this did not work:

function theme_slug_filter_the_content( $content ) {
    $custom_content = 'MY CODES';
    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'theme_slug_filter_the_content' );
Share Improve this question edited Oct 19, 2015 at 14:21 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked Oct 19, 2015 at 6:14 AminAmin 11 bronze badge 1
  • 1 You are saying Add a php code after post title in post pages, have a look at the_title hook. – Mayeenul Islam Commented Oct 19, 2015 at 8:00
Add a comment  | 

2 Answers 2

Reset to default 1

You need to use the_title filter, not the_content. Also make sure you have the_title() function somewhere in your single post page. Here is the code:

function theme_slug_filter_the_content( $title ) {
    $custom_title  = 'MY CODES';
    $custom_title  .= $title ;
    return $custom_title ;
}
add_filter( 'the_title', 'theme_slug_filter_the_content' );

See the below code that you needs to write in content.php file

if ( is_single() ) :
    the_title( '<h1 class="entry-title">', '</h1>' );
else :
    the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );
endif;

Here after Or you can add your code.

I hope your single.php file load template function call this content.php file. you can check this on twentyfifteen theme also that comes with default wordpress installation.

发布评论

评论列表(0)

  1. 暂无评论