• Resolved mirquintilianus

    (@mirquintilianus)


    Hi guys,

    I’m a newcomer to WordPress and I’ve been trying to figure out how the code for woocommerce’s storefront theme works.
    More specifically, I am trying to see how the homepage is structured and what is actually the place of a file called template-homepage.php in the bigger picture of wordpress’s template hierarchy?
    I tried to look for code references to the file, places using get_template_part() for example, but I couldn’t find any such that would make much sense to a newcomer. I have the same issue with files like content-homepage.php and template-fullwidth.php, they seem to be kind of hanging in the air…
    Any clues are welcome.
    A big thank you in advance!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @mirquintilianus,

    Welcome to WordPress! A lot of us got our starts like you; digging into a theme’s code to figure out how everything works. Let’s take a quick tour of the Storefront theme’s home page.

    To understand which template file is loaded, I would suggest taking a look at this guide. I’m going to skip over this section and just look at what happens once the template-homepage.php file is loaded.

    https://developer.wordpress.org/themes/basics/template-hierarchy/

    Let’s see what happens inside the file when it loads.

    
    		<main id="main" class="site-main" role="main">
    
    			<?php
    			/**
    			 * Functions hooked in to homepage action
    			 *
    			 * @hooked storefront_homepage_content      - 10
    			 * @hooked storefront_product_categories    - 20
    			 * @hooked storefront_recent_products       - 30
    			 * @hooked storefront_featured_products     - 40
    			 * @hooked storefront_popular_products      - 50
    			 * @hooked storefront_on_sale_products      - 60
    			 * @hooked storefront_best_selling_products - 70
    			 */
    			do_action( 'homepage' );
    			?>
    
    		</main><!-- #main -->
    

    What’s happening is we have an action firing called “homepage.” The developers have added notes to let us know what functions are being called here. Take the first one, storefront_homepage_content for example. If we search the code in the theme, we find this function is located inside the storefront-template-functions.php file. Here’s what it looks like:

    
    if ( ! function_exists( 'storefront_homepage_content' ) ) {
    	/**
    	 * Display homepage content
    	 * Hooked into the <code>homepage</code> action in the homepage template
    	 *
    	 * @since  1.0.0
    	 * @return  void
    	 */
    	function storefront_homepage_content() {
    		while ( have_posts() ) {
    			the_post();
    
    			get_template_part( 'content', 'homepage' );
    
    		} // end of the loop.
    	}
    }
    

    This starts a WordPress loop that will ultimately display the content that’s been entered into the page editor for the home page. If you keep following these functions by searching them out, you’ll finally end up here:

    
    if ( ! function_exists( 'storefront_page_content' ) ) {
    	/**
    	 * Display the post content
    	 *
    	 * @since 1.0.0
    	 */
    	function storefront_page_content() {
    		?>
    		<div class="entry-content">
    			<?php the_content(); ?>
    			<?php
    				wp_link_pages(
    					array(
    						'before' => '<div class="page-links">' . __( 'Pages:', 'storefront' ),
    						'after'  => '</div>',
    					)
    				);
    			?>
    		</div><!-- .entry-content -->
    		<?php
    	}
    }
    <code></code>

    This will display the content on the home page as well as pagination links if the home page content is set to be on more than one page.

    There are lots of nested functions/actions here, but you can search for each one in your code editor to find the next. They all do eventually have an end where content is ultimately added.

    I hope that points you in the right direction. Let us know if you have any questions.

    Thanks!

    Thread Starter mirquintilianus

    (@mirquintilianus)

    Dear @3sonsdevelopment,

    I immensely appreciate your detailed response.

    To provide some more context I also found the following article very helpful.
    So, in the admin panel of my wordpress I found that the my sotrefront theme is currently using the “Full width” template, that is the template-fullwidth.php to pull together the contents of the homepage.

    I drilled down further and consulted the general post table in the database, and it turns out the above file’s content loop gets a single post of type page (which obviously is created on activating the storefront theme) looking like this

    <!-- wp:cover {"url":"http://localhost/wpstorefront/wp-content/uploads/2021/01/hero-scaled.jpg","id":25,"dimRatio":0,"customOverlayColor":"#ffffff","align":"full"} -->
    <div class="wp-block-cover alignfull" style="background-image:url(http://localhost/wpstorefront/wp-content/uploads/2021/01/hero-scaled.jpg);background-color:#ffffff"><div class="wp-block-cover__inner-container"><!-- wp:heading {"textAlign":"center","level":1} -->
    <h1 class="has-text-align-center">Welcome</h1>
    <!-- /wp:heading -->
    
    <!-- wp:paragraph {"align":"center","style":{"color":{"text":"#000000"}}} -->
    <p class="has-text-align-center has-text-color" style="color:#000000">This is your homepage which is what most visitors will see when they first visit your shop.</p>
    <!-- /wp:paragraph -->
    
    <!-- wp:paragraph {"align":"center","style":{"color":{"text":"#000000"}}} -->
    <p class="has-text-align-center has-text-color" style="color:#000000">You can change this text by editing the "Welcome" page via the "Pages" menu in your dashboard.</p>
    <!-- /wp:paragraph --></div></div>
    <!-- /wp:cover -->
    
    <!-- wp:heading {"textAlign":"center"} -->
    <h2 class="has-text-align-center">Shop by Category</h2>
    <!-- /wp:heading -->
    
    <!-- wp:shortcode -->
    [product_categories limit="3" columns="3" orderby="menu_order"]
    <!-- /wp:shortcode -->
    
    <!-- wp:heading {"textAlign":"center"} -->
    <h2 class="has-text-align-center">New In</h2>
    <!-- /wp:heading -->
    
    <!-- wp:woocommerce/product-new {"columns":4} /-->
    
    <!-- wp:heading {"textAlign":"center"} -->
    <h2 class="has-text-align-center">We Recommend</h2>
    <!-- /wp:heading -->
    
    <!-- wp:woocommerce/handpicked-products {"columns":4,"editMode":false,"products":[34,36,37,38]} /-->
    
    <!-- wp:heading {"textAlign":"center"} -->
    <h2 class="has-text-align-center">Fan Favorites</h2>
    <!-- /wp:heading -->
    
    <!-- wp:woocommerce/product-top-rated {"columns":4} /-->
    
    <!-- wp:heading {"textAlign":"center"} -->
    <h2 class="has-text-align-center">On Sale</h2>
    <!-- /wp:heading -->
    
    <!-- wp:woocommerce/product-on-sale {"columns":4} /-->
    
    <!-- wp:heading {"textAlign":"center"} -->
    <h2 class="has-text-align-center">Best Sellers</h2>
    <!-- /wp:heading -->
    
    <!-- wp:woocommerce/product-best-sellers {"columns":4} /-->

    And here I need your help again! Despite some research, this still doesn’t make much sense to my newcomer’s mind except that it looks like another template.

    Maybe some Gutenberg blocks getting referenced somewhere? Anything else?

    Hi @mirquintilianus,

    One point I forgot to mention. Once Gutenberg blocks were introduced the developers recreated the layout of the home page template using blocks. They made this available for new installations of Storefront.

    That makes it much easier to rearrange and add content to the home pages. Now instead of using a plugin or writing custom code, you can simply use the block editor to make the changes. It’s a better experience for everyone.

    Cheers!

    Thread Starter mirquintilianus

    (@mirquintilianus)

    I would very much appreciate if anyone can elaborate a little bit more on the way these Gutenberg blocks are implemented in Storefront/Woocommerce.

    For example, where to look for the code behind a particular block, let’s say the code for the search or the recommend block, etc.?
    I am particularly asking about Woocommerce and Storefront, because, I guess you’d agree, it’s easier to understand the concepts when looking at a particular implementation.

    Hey @mirquintilianus,

    I totally agree that it’s best to focus on something specific when trying to learn a new concept. Gutenberg blocks are quite a bit different from the PHP code that’s made up most of WordPress’s code for years. It’s largely JavaScript based.

    The developers do have some documentation you can use to figure out how it all fits together. Here’s the URL to it on GitHub

    https://github.com/woocommerce/woocommerce-gutenberg-products-block/tree/trunk/docs

    You may also find it helpful to read some general posts about Gutenberg Blocks, how they are made and work. That would also be a helpful starting point.

    https://developer.wordpress.org/block-editor/packages/packages-create-block/

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    We’ve not heard back from you in a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Cheers! 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Storefront homepage template’ is closed to new replies.