0

so I am hoping to retrieve a custom field that is already displayed on the "front-page.php" template on an another page.php template of my website. I know how to do that with the post-id way, but what I'm hoping to do is get it so that it automatically retrieves them from the front page template... the reason why I want to do it that way is because it's a mult-site and I want to re-use the theme installed on sub sites.

So, instead of:

 <img src="<?php the_field('image1', 31); ?>" />

have it be something like:

 <img src="<?php the_field('image1', Front Page identifier here); ?>" />

Can someone please let me know if that's possible? I would really appreciate it!

2 Answers 2

1
$val = get_post_meta( get_the_ID(), 'meta_data_name', true );
echo $val;

You can replace get_the_ID() with the ID of the post you are getting the meta from.

You could possible team this with:

$frontpage_id = get_option('page_on_front');

to get the homepage ID. Not sure if this would be friendly with multisite though.

the_field() is an Advanced Custom Fields function, this can take a second parameter. You can also achieve the same as the above:

<?php the_field($field_name, $post_id); ?>

replacing post ID with the ID of the post you are retrieving the data from.

4
  • Hmm, thanks for the reply - well, I can get the $post_ID and retrieve the field from the front-page on the site, so for instance on one site it's '31' as the front page like I indicated above... but what I would like to do, is automatically retrieve the custom field from the 'front-page.php' template that will be assigned to each site. Is that possible? Commented Jul 12, 2016 at 20:03
  • I don't think you could do this automatically, at least, I cannot think of an easy, way. If there are not many fields then it shouldn't take long to manually add fields using: <?php the_field($field_name, $homepage_id); ?>
    – user319940
    Commented Jul 12, 2016 at 20:04
  • Yea, okay I'll just continue to do it this way then.... the only thing is that if they wanted to add a new site, they would have to do that manually I think. I could also do a custom post type I think, and have the images just retreived that way - but I think it's too much trouble. Thanks or the reply... I'll let anyone know I find something that would be a solution. Commented Jul 12, 2016 at 20:06
  • Actually, you could create a custom function (in a plugin or functions.php) that spits out the homepage custom meta. Then you would only have to call that function from the page, rather than repeating all of the meta again.
    – user319940
    Commented Jul 12, 2016 at 20:14
0

The way I solved this, was to set up a custom post type set up for each of the default images I wanted, then pulled in the post with the featured image associated with each.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.