Jump to content
Sign in to follow this  
shmokes

Pull data from Custom Fields in a Custom Post Type Template

Recommended Posts

shmokes

I have (with a bit of hand-holding) learned how to make templates for my custom post types but I am stuck on one last step. I want to include a table with product information at the top of every post. I input the product information in custom fields. Prior to making custom templates I inserted the table manually and I pulled data from the fields using shortcodes. However, when I put this stuff in my template and make a new post, the shortcode is simply displayed rather than pulling the proper information from the custom fields. Here is the code I put into my functions.php file (it displays properly, except that it shows the reader my shortcode rather than the information the shortcode is meant to pull from a custom field): ************************************************************************* add_action('pagelines_loop_before_post_content', 'book_info'); function book_info(){ if( get_post_type() == 'books' ) { echo "

Publisher / DeveloperAgePriceScore
[ecpt_field id='publisher/author'][ecpt_field id='age'][ecpt_field id='price'][ecpt_field id='score']
"; } } ************************************************************************* I've tried to shoehorn in the following line to pull the information from the custom fields in the proper way, but I'm getting the syntax wrong and breaking my site. I think I'm close, but I'm stuck. Can anyone help? Here's the line that I should use to put code from my custom fields into my template, but I'm just not sure how to integrate it into the table above: *************************************************************************

<?php echo get_post_meta($post->ID, 'publisher/author', true); ?>

*************************************************************************

Share this post


Link to post
Share on other sites
kastelic
Try the do_shortcode function: http://codex.wordpress.org/Function_Reference/do_shortcode

Share this post


Link to post
Share on other sites
shmokes
Brilliant. Thank you. I was able to figure it out with that link (and a bit of trial-and-error). For future reference, here is the code that worked for me:
	add_action('pagelines_loop_before_post_content', 'book_info');
		function book_info(){
		if( get_post_type() == 'books' ) {
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "";
		echo "
Publisher / DeveloperAgePriceScore
"; echo do_shortcode('[ecpt_field id="publisher/developer"]'); echo ""; echo do_shortcode('[ecpt_field id="age"]'); echo ""; echo do_shortcode('[ecpt_field id="price"]'); echo ""; echo do_shortcode('[starreview tpl=16 post=% style="plain_green" size="16"]'); echo "
"; } }
This automatically puts a custom table at the top of my "Books" custom post type. The table automatically populates itself based on whatever I input in the custom fields. Thus, I fill in the form on the Word Press post edit screen like this: http://i55.photobucket.com/albums/g134/shmokes/Fields-Complete.png And the resulting table at the top of my posts looks like this: http://i55.photobucket.com/albums/g134/shmokes/Screenshot2011-08-25at102247AM.png

Share this post


Link to post
Share on other sites
shmokes
Sorry about my code. I can't figure out how to put code into nice yellow boxes like the rest of you do.

Share this post


Link to post
Share on other sites
Simon
use bbcode ;) code and /code wrapped in [] ;)

Share this post


Link to post
Share on other sites
shmokes
Thanks. For reference, ignore the [quote]
	
[/quote] in my code up there. Those tags were an attempt to blockquote my code to make it easier to read.

Share this post


Link to post
Share on other sites
red3media
Could this "if( get_post_type() == 'books' ) {" be easily modified to pull certain categories?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

×