Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Anthony Hind
Anthony Hind
5,715 Points

PHP Advice

The code below is to add a custom block in Woo Themes Canvas theme not sure if I have been staring at this piece of code for to long but I just cannot figure out the problem it is in my functions.php file to display a custom block on the home page only when I add this code I get a blank page.

function woo_my_custom_block() {
if ( !( is_home() || is_front_page()  ) { return; }
?>
<div id="block1">
<div class="col-full">
hi!
</div>
</div>
<?php
}
add_action( 'woo_content_before', 'woo_my_custom_block');

3 Answers

Have you tried something like this :

<?php 

function woo_my_custom_block() { 
   if (is_front_page()){
          get_template_part( 'partials/section', 'block-1' ); 
   }
 }

?>
Anthony Hind
Anthony Hind
5,715 Points
function woo_my_custom_block() { 
   if (is_front_page()){
          get_template_part( 'partials/section', 'block-1' ); 
   }
 }

add_action( 'woo_content_after', 'woo_my_custom_block');

OK that sorted it your a star :) the functions.php file didnt require the php tags that was breaking my code I now have a nice full width content block on my theme homepage

Great, glad I could help :-)

Hi Anthony - It looks like you are missing a closing curly brace in the function at the top. You close the if statement, but not the function. Try this:

function woo_my_custom_block() {
if ( !( is_home() || is_front_page()  ) { return; }
}
Anthony Hind
Anthony Hind
5,715 Points

Hi,

Thanks I tried that but I still get a blank page after I use the code I can get it to work with the following code

/*-----------------------------------------------------------------------------------*/
/* You can add custom functions below */
/*-----------------------------------------------------------------------------------*/


function woo_my_custom_block() {
?>


<?php get_template_part( 'partials/section', 'block-1' ); ?>

<?php
}


add_action( 'woo_content_after', 'woo_my_custom_block');

Unfortunately I am working locally but using the code above displays my custom block on every page but I would like it on the homepage only.