Fügen Sie HTML direkt nach dem <body> -Tag
3 Antworten
- Stimmen
-
- 2016-03-07
Twenty Twelve hat keine Haken,die unmittelbarnach dem Öffnen des
<body>
-Tags ausgelöst werden.Kopieren Sie daherin Ihrem untergeordneten Thema,das das übergeordnete Twenty Twelve-Themaerweitert,die Datei
header.php
in Ihr untergeordnetes Themenverzeichnis.Öffnen Sie die Datei
header.php
in Ihrem untergeordneten Thema undfügen Sie unmittelbarnach dem Öffnen des Body-Tagseinen Aktions-Hook hinzu,an den Sie sich dann über Ihre Dateifunctions.php
anschließen können .Zum Beispielin Ihrer
twenty-twelve-child/header.php
:<body <?php body_class(); ?>> <?php do_action('after_body_open_tag'); ?>
Dannin Ihrer
twenty-twelve-child/functions.php
:function custom_content_after_body_open_tag() { ?> <div>My Custom Content</div> <?php } add_action('after_body_open_tag', 'custom_content_after_body_open_tag');
Dies wird dannin Ihrem HTML wiefolgtgerendert:
<body> <div>My Custom Content</div>
Empfohlene Lektüre:
https://developer.wordpress.org/reference/functions/do_action/
UPDATE: JULI 2019
Wie von Junaid Bhura aus WordPress 5.2eine neue Themenhilfefunktion
wp_body_open
wurdeeingeführt,diefür die Verwendungmit anderen Hilfsfunktionenwp_head
undwp_footer
vorgesehenist.Zum Beispiel:
<html> <head> <?php wp_head(); ?> </head> <body <?php body_class(); ?>> <?php wp_body_open(); ?> <!-- BODY CONTENT HERE --> <?php wp_footer(); ?> </body> </html>
In Ihrer Themefunctions.php-Datei (oderentsprechend anderswo)
function custom_content_after_body_open_tag() { ?> <div>My Custom Content</div> <?php } add_action('wp_body_open', 'custom_content_after_body_open_tag');
WICHTIG
Sie sollten sicherstellen,dass der Hookinnerhalb des Themas vorhandenist,in das Sieeinfügenmöchten,da dies von der Communitymöglicherweisenochnicht weit verbreitetist.
Wenn NICHT ,müssen Sie immernoch dem Prinzipfolgen,das Thema umein untergeordnetes Thema zuerweitern,mit der Ausnahme,dass SIE verwenden würden :
<?php wp_body_open(); ?>
... anstelle von ODER zusätzlich zu:
<?php do_action('after_body_open_tag'); ?>
Empfohlene Lektüre:
https://developer.wordpress.org/reference/functions/wp_body_open/
Twenty Twelve does not have any hooks that fire immediately after the opening
<body>
tag.Therefore you in your child theme which extends the parent Twenty Twelve theme, copy the
header.php
across to your child theme directory.Open the
header.php
file in your child theme and just after the opening body tag add an action hook which you can then hook onto via yourfunctions.php
file.For example in your
twenty-twelve-child/header.php
file:<body <?php body_class(); ?>> <?php do_action('after_body_open_tag'); ?>
Then in your
twenty-twelve-child/functions.php
file:function custom_content_after_body_open_tag() { ?> <div>My Custom Content</div> <?php } add_action('after_body_open_tag', 'custom_content_after_body_open_tag');
This will then render in your HTML as:
<body> <div>My Custom Content</div>
Recommended reading:
https://developer.wordpress.org/reference/functions/do_action/
UPDATE: JULY, 2019
As commented by Junaid Bhura from WordPress 5.2 a new theme helper function
wp_body_open
has been introduced that is intended for use as per the likes of other helper functionswp_head
andwp_footer
.For example:
<html> <head> <?php wp_head(); ?> </head> <body <?php body_class(); ?>> <?php wp_body_open(); ?> <!-- BODY CONTENT HERE --> <?php wp_footer(); ?> </body> </html>
In your theme functions.php file (or suitably elsewhere)
function custom_content_after_body_open_tag() { ?> <div>My Custom Content</div> <?php } add_action('wp_body_open', 'custom_content_after_body_open_tag');
IMPORTANT
You should ensure that the hook exists within the theme that you are wanting to inject-into as this may not be widely adopted by the community, yet.
If NOT, you will still need to follow the principle of extending the theme with a child theme with the exception that YOU would use:
<?php wp_body_open(); ?>
...instead of OR in addition to:
<?php do_action('after_body_open_tag'); ?>
Recommended reading:
https://developer.wordpress.org/reference/functions/wp_body_open/
-
Vielen Dank.Ich werdees versuchen. Gibteseine andere Möglichkeit,ohne die Datei header.php hinzuzufügen? Wieetwasmit preg_replace?Thanks. I will try this. Any other way to do it without adding the header.php? Such as something using preg_replace?
- 0
- 2016-03-07
- Ramanan
-
Sie können diesbeispielsweisetun,wenn Sie sich an "template_include" oder ähnlichem anhängen. Es wirdjedoch dringend davon abgeraten,da diesnichtnurineffizientist,sondern auch sehr unzuverlässig sein kann,wenn sich aufgrundeiner Aktualisierungetwasim übergeordneten Element ändert.Das Erweitern des übergeordneten Themasmithilfeeines untergeordneten Themasist diebeste Vorgehensweise. Esist vorhersehbar und wirderwartet. Außerdemerhalten Sie viel Kontrolle,z. B. anhand des obengezeigten Beispiels.Wenn Siejedoch "preg_replace" verwendenmöchten,liegtes an Ihnen ...You could, if for example you hook onto `template_include` or similar, however it is strongly advised against doing that because not only is it inefficient, it could be very unreliable if something changes in the parent due to an updatge. Extending the parent theme using a child theme is the best practice, it is predictable and expected, plus it gives you a great deal of control, such as using the example shown above. However if you want to use `preg_replace` that's up to you...
- 1
- 2016-03-07
- Adam
-
Nocheinmal vielen Dank. Ichbenutze Ihren Code undfunktioniertgut. Eigentlich warmeine Fragenur aus untergeordneter Sicht,abereinfachmit functions.php des untergeordneten Themas. Abertrotzdem,nachdemich Ihren Codeimplementiert habe,stelleichfest,dasser unkompliziert,leicht undeinfachist.Thanks once again. I am using your code and works nice. Actually my question was from a child viewpoint only but simply using functions.php of the child theme. But anyway after implementing your code, I realize it's straightforward, lightweight and simple.
- 0
- 2016-03-07
- Ramanan
-
Seit WordPress 5.2istjetztein Standard-Tag verfügbar: wp_body_open (): https://developer.wordpress.org/reference/functions/wp_body_open/Since WordPress 5.2, a standard tag is now available: wp_body_open() : https://developer.wordpress.org/reference/functions/wp_body_open/
- 2
- 2019-07-26
- Junaid Bhura
-
@JunaidBhura dankefür deinen Vorschlag zu diesem alten Thread.Ich habe das obige Beispiel aktualisiert,um dieneue Kernhilfefunktion und den Haken zuerläutern,diein Themen vorhanden sein und hoffentlichin Zukunft weit verbreitet sein werden.@JunaidBhura thank you for your suggestion on this old thread. I have updated the example above to elaborate as to the new core helper function and hook that mau be present in themes and hopefully widely adopted in the future..
- 0
- 2019-07-26
- Adam
-
- 2017-07-21
Eine sehr,sehr,sehr schmutzige Lösung wäre:
/* Insert tracking code or other stuff directly after BODY opens */ add_filter('body_class', 'wps_add_tracking_body', PHP_INT_MAX); // make sure, that's the last filter in the queue function wps_add_tracking_body($classes) { // close <body> tag, insert stuff, open some other tag with senseless variable $classes[] = '"><script> /* do whatever */ </script><noscript></noscript novar="'; return $classes; }
A very, very, very dirty solution would be:
/* Insert tracking code or other stuff directly after BODY opens */ add_filter('body_class', 'wps_add_tracking_body', PHP_INT_MAX); // make sure, that's the last filter in the queue function wps_add_tracking_body($classes) { // close <body> tag, insert stuff, open some other tag with senseless variable $classes[] = '"><script> /* do whatever */ </script><noscript></noscript novar="'; return $classes; }
-
+1 dankefür den Trick+1 thanks for trick
- 0
- 2019-10-02
- Vaibhav Gupta
-
- 2016-07-11
Fügen Sie diesen Codein functions.php
hinzufunction my_function() { echo'<div id="from_my_function"></div>'; } add_action('wp_head', 'my_function');
Add this code in functions.php
function my_function() { echo'<div id="from_my_function"></div>'; } add_action('wp_head', 'my_function');
-
Dies wirdim ``not` `ausgegeben.This will output inside the `` not ``.
- 13
- 2016-07-11
- cjbj
-
Nun,wp scheint kluggenug zu sein,es wirdtatsächlich dem Körper hinzugefügt!Wennes sich umein Meta handelt,wirdesin den Headereingefügt,andernfalls wirdesim Body angezeigt.Well wp seems to be smart enough, it is actually added to the body! If it's a meta then it will be injected inside the header otherwise it will be displayed in the body.
- 0
- 2018-02-01
- Mohamed Salem Lamiri
-
Ichglaube,@cjbjist richtig.Es sieht so aus,als obes der Browserist,deres auf den Körper verschiebt,nicht WordPress.Zeigen Sie die Quelle aufeiner Seite an,auf der Sie siein den Kopfeingefügt haben,und dortfinden Sie sie.I believe @cjbj is correct. It's looks like it's the browser that's moving it to the body, not WordPress. View source on a page where you've inserted in to the head and that's where you'll find it.
- 1
- 2018-03-29
- Danger
Ich verwende das Wordpress-Thema Twenty Twelve (genauergesagtein Kind davon).
Ichmöchte wissen,wieman HTML direktnach dem Öffnen des Körpersin nurfunctions.phpeinfügt undnicht header.php verwendet.
Ist dasmöglich?