admin管理员组文章数量:1122832
I want to show an iframe
at the top of a specific Tag page, so once I have is_tag('my_tag')
, I will add the iframe
before the title
of the page.
I've tried this code to show the iframe before the content of the page, but it didn't work:
// Show the iFrame at the beginning of the page
function show_iframe($content) {
if ( is_tag('my_tag') ) {
$before = '<iframe src="" sandbox="allow-scripts allow-same-origin"></iframe>';
return $before . $content;
}
return $content;
}
add_filter('the_content', 'show_iframe');
Is there any hook that runs before printing the title of the page? or any way to echo
an HTML
code before a chosen div
in the page?
I want to show an iframe
at the top of a specific Tag page, so once I have is_tag('my_tag')
, I will add the iframe
before the title
of the page.
I've tried this code to show the iframe before the content of the page, but it didn't work:
// Show the iFrame at the beginning of the page
function show_iframe($content) {
if ( is_tag('my_tag') ) {
$before = '<iframe src="https://www.example.com" sandbox="allow-scripts allow-same-origin"></iframe>';
return $before . $content;
}
return $content;
}
add_filter('the_content', 'show_iframe');
Is there any hook that runs before printing the title of the page? or any way to echo
an HTML
code before a chosen div
in the page?
3 Answers
Reset to default 0First, in WordPress lingo 'content' does not include 'title'. (Also, be careful with 'title'. Sometimes it means 'the heading above the main content, and below the site navigation and/or masthead', and sometimes it means the html title, which is a piece of text the browser shows at the top of the window or tab, in its history, etc. In this case, it sounds like you're referring to the former.
Second, in this case there isn't a hook to modify the title. Instead, modify a template. You might worry that you'd have to modify several template files, since web pages for the site will look different. Fortunately, usually, for consistency the top part of different kinds of web pages will look the same. So, you probably just have to modify header.php
or some template part (probably in template-parts/header
).
Use javascript in the browser. Here's a jQuery solution. Note the details depend on the theme details, your tags, desired iframe url, etc.
In functions.php
add
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_scripts' );
function my_theme_enqueue_scripts() {
wp_enqueue_script('title-tweak',
get_stylesheet_directory_uri() . '/js/title-tweak.js',
array('jquery'));
}
and make the file (in your theme) js/title-tweak.js
, containing
(function( $ ) {
$(document).ready(
function(){
if ($("body").hasClass("tag-my_tag")) {
$(".page-title").before ('<iframe src="http://www.gravatar.com/avatar"></iframe>');
}
} );
})(jQuery);
Addendum I did it a slightly more complicated way, because at one time the simpler way didn't work. But (at least on my browser) it does now.
(function( $ ) {
$(document).ready(
function(){
$(".body.tag-my_tag page-title").before ('<iframe src="http://www.gravatar.com/avatar"></iframe>');
} );
})(jQuery);
This is really going to depend on your theme.
If your theme is using the core the_archive_title() function to display the tag title you can hook into the get_the_archive_title filter to modify the output.
Example:
add_filter( 'get_the_archive_title', function( $title ) {
if ( is_tag('my_tag') ) {
return '<iframe src="https://www.example.com" sandbox="allow-scripts allow-same-origin"></iframe>' . $title;
}
return $title;
} );
If your theme is not using this core function for the title you will need to share the theme you are using (if it's free) so we can look at it and see how it's coded. Or you will need to reach out to the theme developer if it's a premium theme.
本文标签: hooksAdd HTML code before the title of the Tag page
版权声明:本文标题:hooks - Add HTML code before the title of the Tag page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290018a1928426.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
iframe
does not depend on any dynamic data. – Max Yudin Commented Mar 25, 2019 at 11:56