Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cobaltapps/7b0b4c3c776ebf20da95eff536c2969f to your computer and use it in GitHub Desktop.
Save cobaltapps/7b0b4c3c776ebf20da95eff536c2969f to your computer and use it in GitHub Desktop.
An example of how to use WordPress Conditional Tags inside of custom functions.
add_action( 'theme_before_content', 'my_banner_ad' );
/*
* This conditional example ensures that the banner ad only
* displays on the front page by returning nothing if it
* is NOT the front page.
*/
function my_banner_ad() {
if ( ! is_front_page() )
return;
echo '<img src="http://example.com/link-to-my-banner.png">';
}
add_action( 'theme_before_content', 'my_banner_ad' );
/*
* This conditional example ensures that the banner ad only
* displays on the front page by only echoing the image code
* if it IS the front page. (same result as above, but different method)
*/
function my_banner_ad() {
if ( is_front_page() ) {
echo '<img src="http://example.com/link-to-my-banner.png">';
}
}
add_action( 'theme_before_content', 'my_banner_ad' );
/*
* This conditional example ensures that the banner ad only
* displays on the front page OR a single page by only echoing the image code
* if it IS the front page OR it is a single page.
*/
function my_banner_ad() {
if ( is_front_page() || is_page() ) {
echo '<img src="http://example.com/link-to-my-banner.png">';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment