get_field do not giving output in advance custom field - wordpress

This is my code but this is not giving any input
<?php
if(get_field('header'))
{
?>
<div class="about-us">
<div class="--header">
<img src="<?php echo get_template_directory_uri()?>/assets/images/laptop.jpg" alt="">
<div class="content">
<?php echo '<h2>' . the_field('heading') . '</h2>';
echo '<h4>' . get_field('description') . '</h4>';?>
</div>
</div>
</div>
<?php
}
?>
This is my custom filed that i created

the_field function echos value. so you don't have to use it in another echo function
please try this code and let me know if it works.
<?php
if ( get_field('header') ){
?>
<div class="about-us">
<div class="--header">
<img src="<?php echo get_template_directory_uri() ?>/assets/images/laptop.jpg" alt="">
<div class="content">
<?php echo '<h2>' . get_field('heading') . '</h2>';
echo '<h4>' . get_field('description') . '</h4>'; ?>
</div>
</div>
</div>
<?php
}else{
_e('the field header not founded','textdomain');
?>

If you're still not getting value from ACF, then you should get value like as below:
<?php
$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above
echo $value = get_field( 'my_field', $post_id );
?>
I hope it would help you out.
Thanks

You have written a wrong condition. There is no field for header it is just a group where you have created all custom fields. You can give a condition on the custom fields.
So, you need to write a condition like this.
<?php
if( get_field( 'heading' ) ) {
// Your Code
}
?>

Related

Wordpress Category loop cannot retrieve ACF image gives Image Value: NULL

I am trying to get the image from a category but I cannot retrieve the image.
Today I already learned that I had to use
get_field('product', $term->taxonomy . '_' . $term->term_id);
to fetch content.
But when I use this same method to fetch an image URL from an ACF Field linked to my custom post type category, I do not recieve any values.
This is my code (the var_dump is included):
<?php
$args = array(
'post_type' => 'segments-overview',
'orderby' => 'date', // we will sort posts by date
);
$query = new WP_Query( $args );
$all_terms = [];
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
$terms = get_the_terms(get_the_ID(), 'category-segments-overview');
foreach($terms as $term) $all_terms[$term->term_id] = $term;
endwhile;
foreach($terms as $term):
?>
<div class="segments-card">
<div class="img">
<?php
$image = get_field('image', $term->taxonomy . '_' . $term->term_id);
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
</div>
<div class="content">
<div class="title"><?php echo $term->name; ?></div>
<!-- <?php print_r($term); ?> -->
<a class="button transparent" href="/segments/<?php echo $term->slug; ?>">
<?php echo __('View All','axia'); ?>
</a>
</div>
</div>
</div>
<?php endforeach;
wp_reset_postdata();
else :
?>
<div class="no-posts-found">
<h2>There were no items found</h2>
<h3>Please try a different search</h3>
</div>
<?php
endif;
?>
I use this var_dump to see if everything is fetched:
$image = get_field('image', $term->taxonomy . '_' . $term->term_id);
echo '<pre>';
echo "Image field value:";
var_dump($image);
echo "Category field value:";
var_dump($term);
echo '</pre>';
The only thing is that I do not get the value from my image in my category that is made in ACF.
You can simply get value by passing term object as second parameters.
$image = get_field('image', $term);
Check the docs here: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

domPDF and ACF repeater fields

I am using domPDF to generate PDFs on certain pages of my site
The problem I am having is that it won’t allow me to use repeater values (via any sort of loop), it doesn’t seem to want to generate them when the PDF is created.
If I call the repeater field itself as a variable, it returns ‘array’, so I assume it’s possible to get it working, somehow.
It appears the repeaters first need to be generated and then included (perhaps as a variable?), but I’m unsure of how to do this.
Does anyone have any idea of how I can get this to work?
This is my ACF repeater code:
<?php if( have_rows('team_contact_details') ): ?>
<div class="profile-item">
<ul class="list-contact">
<?php while( have_rows('team_contact_details') ): the_row(); ?>
<li>
<div class="list-contact-letter">
<?php the_sub_field("contact_label"); ?>
</div>
<div class="list-contact-detail">
<?php
if(get_sub_field('contact_link') == 'phone' ) {
echo '' . get_sub_field("phone_number") . '';
} else if(get_sub_field('contact_link') == 'email' ) {
echo '' . get_sub_field("email_address") . '';
}
?>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
This is my domPDF code:
//Print PDF
require_once get_template_directory() . '/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$content = '
<html>
<head></head>
<body>
<div class="person">
<img src="' . get_field('page_image') . '"/>
</div>
<h1>' . get_the_title() . '</h1>
<div class="pdf-label">' . get_field('team_academic') . '</div>
<div class="pdf-label">' . get_field('team_position') . '</div>
<<<< Attempting to put repeater values here as unordered list >>>>
</body>
</html>
';
// instantiate and use the dompdf class
$dompdf = new Dompdf(array(
'enable_remote' => true
)
);
$dompdf->loadHtml($content);
// (Optional) Setup the paper size and orientation
$dompdf->set_paper( 'letter' , 'portrait' );
// Render the HTML as PDF
$dompdf->render();
$doctitle = get_the_title();
$dompdf->stream($doctitle);
Thanks
I got it. FYI, this was how:
if( have_rows('team_contact_details')) :
while(have_rows('team_contact_details')) : the_row();
$content .= '<h3>'. get_sub_field('contact_label') .'</h3>';
if(get_sub_field('contact_link') == 'phone') {
$content .= '<div>'. get_sub_field('phone_number') .'</div>';
} else {
$content .= '<div>'. get_sub_field('email_address') .'</div>';
}
endwhile;
endif;

If Data not exists - show (custom text)

I use wordpress CMS and CMB2 METABOXES and Custom fields. Its works perfect but when I'm making loop If data in metabox not exists , this script anyway rendering empty group field. I want to give the command to script, if it is empty ----> show my custom markup code. I have this
<?php $our_advantages_items = get_post_meta( get_the_id(), 'our_advantages_block_box', true );
if( !empty( $our_advantages_items ) ) {
foreach( $our_advantages_items as $our_advantages_item ) { ?>
<div class="cey96zggvcich3">
<div class="cey96zggvcich3_cvz">
<h2><?php echo $our_advantages_item['our_advantages_block_heading']; ?></h2>
<div class="io2ji39349959jeij">
<?php echo wpautop( $our_advantages_item['our_advantages_block_content'], 1 ); ?>
</div>
</div>
</div>
<?php }
} ?>
Will be happy for any help !
Not sure I understand what you're asking, but just add an else to your if statement
<?php if( !empty( $our_advantages_items ) ) : ?>
//If not empty write your html here
<?php else: ?>
//If empty write your html here
<?php endif; ?>

Wordpress ACF Repeater Field - Only pull in first 2 fields

Topic kinda says it all. I need to only pull in the first two fields of an ACF repeater field.
Here is what I am trying to use but it is obviously not right:
<?php $args = [ 'posts_per_page' => 2, 'order' => 'desc']; ?>
<?php $ar = new WP_Query( $args ); ?>
<?php if( $ar->have_rows('prodImgs') ): while ( $ar->have_rows('prodImgs') ) : $ar->the_row(); ?>
<img src="<?php the_sub_field('prodImg'); ?>" alt="">
<?php endwhile; ?>
<?php endif; ?>
How am I supposed to do this?
<?php
// check if the repeater has data
if( have_rows('prodImgs') ) {
//counter
$i=0;
//loop through the rows
while( have_rows('prodImgs') ) {
the_row();
//check if 2 subfields have been shown
if ( $i > 1 ) { break; }
echo "<img src='" . get_sub_field('prodImg_sub') . "' alt='Lorem ipsum'>";
$i++;
}
}
?>
You are mixing apples and pears, WP_Query and ACF Repeater field. WP_Query returns the post data, whilst the ACF function have_rows( $repeater_field_name, $post_id ); checks whether there are any data in the custom field repeater that is on your page/post (or if you specify $post_id on the relevant post/page). More info at https://www.advancedcustomfields.com/resources/repeater/
<?php $rows = get_field('prodImgs'); ?>
<?php $i = 0; if ($rows):?>
<?php foreach($rows as $row): ?>
<img src="<?php echo $rows[$i][/* name of first field */]; ?>" alt="">
<?php echo $rows[$i][/* name of Second field */]; ?>
<?php $i++; endforeach; ?>
<?php endif; ?>

Rendering an image on a WooCommerce product page

Using the Advanced Custom Fields plugin, I am trying to render an image on a single product page in WooCommerce.
I found this snippet of code from someone who is doing the same thing, but is rendering a text field instead. How would I change this to render an image?
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_top_category_desc', 1 );
function woocommerce_template_top_category_desc (){
$terms = get_the_terms( $post->ID, 'wc-attibute-class' );
if ( !empty($terms)) {
$term = array_pop($terms);
$text= get_field('txt-field', $term);
if (!empty($text)) {
echo $text;
}
}
}
So far I have this, but it's not working. The add action is correct because I was using it for something else, but starting at $terms is where I get lost and is obviously not right.
add_action( 'woocommerce_product_thumbnails' , 'add_below_featured_image', 9 );
function add_below_featured_image() {
$terms = get_the_terms( $post->ID, '496' );
if ( !empty($terms)) {
$term = array_pop($terms);
$image = get_field('banner_feedback', $term);
if (!empty($image)) {
echo $image;
}
}
}
Figured it out.
add_action( 'woocommerce_product_thumbnails' , 'add_below_featured_image', 9 );
function add_below_featured_image() {
if( is_single( pageidhere ) ) {
echo '<br><img src="urlhere">';
}
}
Steps to add custom gallery below featured image in woocommerce single product page(you can modify suitably to add just a single image):
Create an ACF for Image Gallery.
Copy the template for product-image.php from /plugins/woocommerce/templates/single-product to themes/your-child-theme/woocommerce/single-product/
Add the following code to product-image.php just before the closing div of
"images"
Add the images you want in the gallery to your single product admin page in the ACF custom field.
`
<?php $images = get_field('product_image_gallery'); ?>
<div class="mycustom_image_gallery">
<?php if($images): ?>
<div class="popup-gallery">
<?php foreach( $images as $image ): ?>
<a href="<?php echo $image['url']; ?>"
class="lightbox-link"
title="<?php echo $image['caption']; ?>"
data-description="<?php echo $image['description']; ?>">
<div class="image-wrap">
<img src="<?php echo $image['sizes']['thumbnail']; ?>">
</div>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
`

Resources