How to display a specific custom field from a specific page on any other page template in WordPress

Bish
Bash
Boshanka
Let's get it done!

0 Shares

This little number had been bugging me for a while. Specifically I have been creating a site for a client using the awesome Woocommerce plugin. In this case, i have made some significant changes to the “loop-shop.php” which is set up to use the page entitled “our services” in the woo commerce settings.

By default, the loop-shop.php page fetched the data from the main editable field in the “our services” page, however it would not display any custom fields i had set up on that page – simply adding the custom fields to the “loop-shop.php” page in the traditional way wasn’t working.

The answer to my problem, and to anyone wanting to add data from a custom field from a specific page to another page somewhere in your theme is as follows:

1. Put this at the beginning of where you want to start displaying your custom field data (replacing the Page ID parameter with the page ID which contains the custom field you wish to display:

1
2
3
4
5
6
<!--?php $args=array( 'page_id' =&gt; 8,&lt;br ?--> 'post_type' =&gt; 'page',
'post_status' =&gt; 'publish', );

$my_query = null; $my_query = new WP_Query($args);
if( $my_query-&gt;have_posts() ) { while ($my_query-&gt;have_posts()) : $my_query-&gt;the_post();
?&gt;

2. Call your custom field by name:

1
<!--?php echo get_post_meta($post---&gt;ID, 'Custom_Field_Name_Here', true); ?-->

3. Wrap up your loop

1
<!--?php endwhile; } wp_reset_query(); ?-->

You can call as many custom fields from that page within a given loop, to call from a new page simply create a new loop after the first.

2 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
Bish
Bash
Boshanka
Let's get it done!

Based in London, Working Everywhere

I’m a Freelance Web Designer and WordPress developer, I’m based in London but work with client’s in the UK and all over the world.

Creative Web Design & WordPress Development Services