Adding home.php as option in layout



Hi Dan, welcome to Beans Community,

If you create a home.php template file in your child theme, you don't have to register it as a layout as it would automatically be applied to your home page. Moreover, since it is a template file it should rather be registered as a page template rather than a layout if you would like to re-use it.

Are you intending to use the home template elsewhere?

Thanks,


Thanks for the reply Thierry.

My question was more about around having it as a layout option under the post, and add my own icon to show the user the layout so it feels more part of the theme (http://prnt.sc/enewgv), rather than the new template name in the template dropdown in page attributes.

Thanks


Hi Dan,

Here is a snippet which you can use in your child theme to create a custom layout option with a custom template for it:

add_filter( 'beans_layouts', 'example_layout_options' );
/**
 * Add the layouts to the admin options.
 *
 * @param array $args An array of layouts.
 *
 * @return array Layouts ready for Beans 'imageradio' option type.
 */
function example_layout_options( $layouts ) {

  $layouts['example_layout'] = __( 'Example Layout', 'example' ); // You may add a url to your layout image instead of text.

  return $layouts;

}

add_filter( 'template_include', 'example_template_include' );
/**
 * Filters the path of the current template before including it.
 *
 * @param string $template The path of the template to include.
 *
 * @return string The path of the template to include.
 */
function example_template_include( $template ) {

 if ( 'example_layout' === beans_get_layout() ) {
    $example_template = locate_template( array( 'example-layout.php' ) );

   if ( ! empty( $example_template ) ) {
     return $example_template;
   }
 }

 return $template;

}
  1. For the snippet above to work, you will have to create a file in your child theme root called example-layout.php which will contain your custom template output.
  2. The layout option in the backend will show as a radio option Example Layout. If you want an image, add the image in your child theme and change __( 'Example Layout', 'example' ) with your image path.

Happy coding,

Write a reply

Login or register to write a reply, it's free!