wordpress - display an ACF field in the post page - php

I am loading a field in the header of my wordpress website. This field is displaying a text on top.
<?php the_field('text_on_top'); ?>
Every page display it, the name of the field is exactly the same on every ACF page. My problem : the blog / post page doesn't load that field. What is going wrong ?

use : the_field() to get_field() ;
<?php echo get_field( "text_on_top" ); ?>
Get a value from a specific post
$value = get_field( "text_on_top", $post->ID);
More details

Related

Display ACF custom fields on WooCommerce store home and category listing

I am trying to display Advanced Custom Field on my WooCommerce storefront in html list tag. I modified the file:
content-product_cat.php
from this:
<li <?php wc_product_cat_class( '', $category ); ?>>
to this:
<li style="background: <?php the_field('my_ACF_field'); ?>" <?php wc_product_cat_class( '', $category ); ?>>
And also I added ACF field on my WP backend and assigned it to my page called "Store" which is the WooCommerce storefront. I can fill the input on my backend (Store page) but it's not showing on storefront/category page, it's just not passing. Where is the problem?
I also tried to set this field as taxonomy, and display it on category setting in WooCommerce, also without success.
I think you'll need to use the second parameter on ACF the_field, to get the value for the specific object that you're after. E.g. if you have the ACF Field set on a product category, you'll need to pass in the term that you're currently looking at to get the right value.
I believe that on a category page you'll need to use something like
<li style="background: <?php the_field('my_ACF_field', $category);?>" <?php wc_product_cat_class( '', $category ); ?>>
This ACF documentation field gives more detail about adding fields to taxonomy terms: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

Add a custom field in a woocommerce category

Hi I have created with CTP custom fields plugin a custom field to appear only in taxonomy -product category- this field, appear in the back end when I edit the category.
Field is intended to be there to upload images and output the URL of the image like this:
<div class="page-heading" style="background-image:url('<?php the_field('field_name'); ?>') !important;">
This kind of code works perfectly OUTSIDE woocommerce (in the normal WP pages and posts) but for some reason, doesn't show anything, even that in the back end I can see the image attached to the category.
In the front end it shows like a empty field...
Im not sure what i'm doing wrong.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
the_field( 'field_name', $post->ID );
Thanks, your tips made me search the right thing and found the answer:
<?php
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
the_field('the_name_of_the_field', $post_id);
?>

How to add multiple post in single wordpress page as per post id

I can try this code in page text but not working
<?php
$post_id = '138';
echo get_post_field('post_content', $post_id);
?>
show a code only not display content,
provide solutions in which file I can add this code.
wordpress provide insert_pages plugin and in this plugin in page add post or another page title, content etc

Display image after taxonomy text with advanced custom fields

I have created a custom post type called:'activities' and created a taxonomy for it called: 'activity_locations'. I have then added a custom field to the taxonomy for an image using Advanced Custom Fields, this image field is called: 'activity_location_image'
On the single template I have managed to display the taxonomy for the product with the following code:
Available in: <?php the_terms( $post->ID, 'activity_locations', ' ', ' / ' ); ?>
However I need to elaborate on this to add a small image after the taxonomy text. I have tried the following code but it hasn’t worked (nothing is displaying):
<?php
$term = get_field('test');
if( $term ): ?>
<img src="<?php echo get_field('activity_location_image', $term); ?>" />
<?php endif; ?>
Can anyone offer any advice/assistance on how to make this work?
If you are still on the single template page, then get_field() without a second argument will only be fetching metadata for your current post object, not the related term objects.
If you read the documentation for get_field() you'll see that to query for term fields you need to use the format
get_field('field_name', 'taxonomyname_X)
Where taxonomyname would be activity_locations and X would be the term ID.
Since you want to customize the display and use the data about the terms, you'll have to swap the_terms() with wp_get_post_terms() which will give you an array of term objects. You can then loop over the array, grabbing the image for each term and outputting the HTML to display the term.

ACF fields not showing on a wordpress custom taxonomy page

I'm having trouble showing ACF on a custom taxonomy page.
The custom tax is 'destinations' with the page being taxomony-destinations.php the field is called 'destination_landing_image. I'm trying to display it on 'mysite.com/destinations/coutryname' as follows:
<?php if (have_posts()) : while (have_posts()) : the_post();
$destination_landing_image = get_field('destination_landing_image');
<img src="<?php echo $destination_landing_image['url'] ?>" alt="<?php echo $destination_landing_image['alt'] ?>">
<?php endwhile; endif; ?>
Oddly enough the page in question does show up the ACF fields within the custom post type(holidays) which is lower in the page. So I guess firstly am I calling it correctly within the loop? and Secondly is a custom taxonomy the right type of page to use?
When you use ACF with posts you can just use get_field() as you are, but when you use it with anything else such as taxonomies, users or options, you need to give ACF a hint about how to look it up. In this case you need to tell it what taxonomy and term you are specifically targeting. Luckily both WordPress and ACF make this really easy:
//Ask WordPress for the currently queried object
//http://codex.wordpress.org/Function_Reference/get_queried_object
$obj = get_queried_object();
//Pass that object as a second parameter into get_field
$destination_landing_image = get_field( 'destination_landing_image', $obj );

Categories