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

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

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

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
7
8
<?php
$args=array( 'page_id' => 8,
             'post_type' => 'page',
             'post_status' => 'publish', );

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

2. Call your custom field by name:

1
<?php echo get_post_meta($post--->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.

Leave a Reply

Enjoyed this article?

If you enjoyed this article please rate & share.

You Might Also Like