WP Ultimate Recipeの有料版にはテンプレートエディタがついているが、テンプレートエディタを使わず無料版でもテンプレートは作成可能。WP Ultimate Recipe公式ドキュメント「Code your template」より。
テンプレート作成手順
テンプレートの追加
functions.phpに次のようにコードを追加。
function wpurp_custom_template( $content, $recipe )
{
// $contentを編集してテンプレート内容を変更
return $content;
}
add_filter( 'wpurp_output_recipe', 'wpurp_custom_template', 10, 2 );
引数の$recipeはWPURP_Recipeオブジェクトで、レシピのパーツのすべてにアクセスできるようになっている。
WPURP_Recipeオブジェクトの利用
次のコードは、レシピのタイトルと説明を出力する。
function wpurp_custom_template( $content, $recipe )
{
ob_start();
?>
<div class="recipe">
<h2><?php echo $recipe->title(); ?></h2>
<?php echo $recipe->description(); ?>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_filter( 'wpurp_output_recipe', 'wpurp_custom_template', 10, 2 );
使用可能な関数は、/helpers/models/recipe.phpファイルを参照。
ブロックの利用
$recipeを使って直接レシピのパーツにアクセスする以外に、テンプレートエディタ内での「テンプレートブロック」と同様にブロック単位でアクセスすることも可能。ブロックにどのようなものがあるのかは、/addons/custom-templates/templates/ フォルダ参照。
次の例では、レシピ材料ブロックをテンプレートに追加する。
function wpurp_custom_template( $content, $recipe )
{
ob_start();
?>
<div class="recipe">
<h2><?php echo $recipe->title(); ?></h2>
<?php echo $recipe->description(); ?>
<h3>Ingredients</h3>
<?php
$ingredient_list = new WPURP_Template_Recipe_Ingredients();
echo $ingredient_list->output( $recipe );
?>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_filter( 'wpurp_output_recipe', 'wpurp_custom_template', 10, 2 );
印刷ボタンや評価の追加
印刷ボタンや評価用の星もコードで追加可能だが、スタイルや機能をきちんと使用できるようにするためには .wpurp-container で囲む必要あり。
印刷ボタンと評価を追加する例は、次のとおり。
function wpurp_custom_template( $content, $recipe )
{
ob_start();
?>
<div class="recipe">
<div class="wpurp-container">
<?php
$print_button = new WPURP_Template_Recipe_Print_Button();
echo $print_button->output( $recipe );
$star_rating = new WPURP_Template_Recipe_Stars();
echo $star_rating->output( $recipe );
?>
</div>
<h2><?php echo $recipe->title(); ?></h2>
<?php echo $recipe->description(); ?>
<h3>Ingredients</h3>
<?php
$ingredient_list = new WPURP_Template_Recipe_Ingredients();
echo $ingredient_list->output( $recipe );
?>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_filter( 'wpurp_output_recipe', 'wpurp_custom_template', 10, 2 );