I’m entirely new to OOP, but trying to dip my toe in by creating a simple Recipes plugin. I have added a Recipes custom post type and a few meta fields to go along with it, and now I am trying to create a few template files for displaying the recipe meta and content. To do this, I am thinking it would be useful to create a class to get all the meta for a particular post.

To test, I created a template file that is meant to echo one sentence with a single meta value after the post content:

$recipe = new Wp_Recipes_Recipe;
echo '<p>The prep time for this recipe is ' . $recipe->$recipeprep . '</p>';

And I created a new file in plugin-dir > public called “class-wp-recipes-recipe.php” that contains the following:

class Wp_Recipes_Recipe {

  public function __construct( $post_id ) {

    $this->$recipemeta = get_post_custom($post_id);
    $this->$recipeprep = $this->$recipemeta['_rcp-prep-time'][0];

  }

}

I think I need to add some code to specify to include my new file, but I am just not sure where to put it. I have tried putting a “require_once” for the file in the load_dependencies() function in the includes folder. No matter what I try, the meta value I am trying to display does not display, and the only noticeable effect from my efforts is that the wp admin bar no longer displays.

I may be going about this completely the wrong way, but any guidance would be greatly appreciated! Thanks!

2 Answers
2

Your constructor requires the post ID to be passed to the class when it’s instatiated, try this

$recipe = new Wp_Recipes_Recipe( get_the_ID() );

Leave a Reply

Your email address will not be published. Required fields are marked *