问题
guys. I have some situation when my shortcode don't work correctly, if I add shortcode in header, because first of all load header.php and only after that load my shortcode. How can I run shortcode before loading header.php ? I try to use hoocks, but not find solution. Best regards.
回答1:
If you want to run your code just before your header.php
is loaded you can use the get_header
hook:
function run_before_header( $name ) {
do_shortcode...
}
add_action( 'get_header', 'run_before_header' );
回答2:
You have a Widget in the Backend of your WP-Admin and the Widget is called: "my_specials" and you want to display this Widget in the header, right? OK; try doing something like this within the header.php:
<?php
// FILENME: header.php
ob_start();
dynamic_sidebar('my_specials');
$mySpecials = ob_get_clean();
// NOW YOU HAVE THE CONTENT OF THE 'my_specials' WIDGET IN THAT VARIABLE.
// ANY YOU CAN NOW ECHO IT AT THE SECTION OF YOUR CHOOSING LIKE SO:
echo $mySpecials
It is imagined that this is what you meant and also hoped that this might help a bit, though...
Good-Luck to you, Mate...
来源:https://stackoverflow.com/questions/37612905/how-run-shortcode-before-loading-header-php-in-wordpress