I am writing some functions to display Custom Fields across multiple templates. Rather than getting each CF like so:
$var = get_post_meta($post->ID, 'my_cf_key', true);
and then checking if the var exists, if so, doing some HTML and then echoing the variable, I have started another approach.
My concern is that this new approach might become more expensive and load on the server… Here it is…
Each CF has a function to a) get it via the above code, then b) echo it. Within each get function, I am calling global $post.
Then, on each actual template page, I am first checking the get function for that CF, then doing the echo function, like so:
if ( func_get_the_cf() ) {
echo '<div>';
func_the_cf();
echo '</div>';
};
So the above looks like it is having to do this for each one… a) get a global $post from the get function and get a value from the DB, then if that exists, run the echo function, which again has to do global post and assign a value from the DB by running the get function.
So it seems like I am doing things in unncessary loops just so I can have clean looking template pages.
is this too much PHP? Am I making this too expensive on the server?
Thanks.. I’m just 5% into the project now so I rather get some opinions before I finish it all the way I’m doing it just to change it all.
Thank you!
Here is a basic class which you can create once:
Now in your template files you can access the class like this: