我正在构建一个插件,它需要在任何给定页面/帖子的主标题之后插入一些链接。
如果我使用post_title过滤器,我的标记就会添加到H1中,例如.不是在那之后。
function so_title_filter( $title, $id = null ) {
if (in_the_loop()) {
$title = $title . "<p>Custom markup!</p>";
}
return $title;
}
add_filter( 'the_title', 'so_title_filter', 10, 2 );是否有方法在标题的标记之后添加自定义标记?
发布于 2018-09-07 00:35:30
不用钩子是不可能的。如果您查看the_title的源代码,您会注意到before和after参数上没有过滤器,只有函数get_the_title()。主题实现标题的方式也不一致,例如,它们可以使用类似the_title( '<h1>', '</h1>' );或<h1><?php the_title(); ?></h1>之类的东西,所以这不是您可以期望通过钩子实现的东西。
不过,你可以用一些JavaScript来做。
PHP (向注入元素中添加类)
function so_title_filter( $title, $id = null ) {
if (in_the_loop()) {
$title = $title . '<p class="moveme">Custom markup!</p>';
}
return $title;
}
add_filter( 'the_title', 'so_title_filter', 10, 2 );JS
jQuery( document ).ready( function() {
jQuery( '.moveme' ).each( function() {
jQuery( this ).insertAfter( jQuery( this ).parent() );
});
});https://stackoverflow.com/questions/52186740
复制相似问题