I have created a custom taxonomy for my custom post type.
Each term only needs to display its name (which works with $term->name;) and an image. For the image I am using ACF (as I usually do for custom fields on a page or post). It is set up and I can select an image when I add a new term in the dashboard, but how do I display it in a foreach loop? The usual get_field() isn't showing anything.
<ul class="taxonomy-terms">
<?php
$terms = get_terms( array(
'taxonomy' => 'my_taxonomy',
'hide_empty' => false,
) );
?>
<?php foreach ( $terms as $term) { ?>
<li>
<img src="<?php the_field('image'); ?>" />
<?php echo $term->name; ?>
</li>
<?php } ?>
</ul>
The issue being with the_field('image');
The image field is set to return the image url.
Maybe you should add your taxonomy as a parameter when calling the image, try :
<?php if( get_field('image') ): ?>
<img src="<?php the_field('image', $term); ?>" />
<?php endif; ?>
It's better to test the Image Field as you don't wan't to show an empty <img> tag.
Also please check that you didn't change the field name in the Image Field settings.
You can read about that here : https://www.advancedcustomfields.com/resources/image/
And here :
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
Let's hope this works,
Have a good day.
You should pass the current term of the loop as the second parameter of the ACF function:
<?php the_field('image', $term); ?>
See here:
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
Related
I'm trying to make a row layout where people can add an mp4 with ACF. So far so good, but when I try to add multiple video sections in the same post it outputs the same video in every player, even though they are different in the backend.
Does anyone know what I'm doing wrong?
Row layout:
<?php if (get_row_layout() == 'video') : ?>
<?php get_template_part('template-parts/sections/section', 'video'); ?>
<?php endif; ?>
Video section part
<div class="section section-type-video flex">
<?php
$video_mp4 = get_field('video_file'); // MP4 Field Name
$video_poster = get_field('video_poster_image'); // Poster Image Field Name
// Build the Shortcode
$attr = array(
'mp4' => $video_mp4,
'poster' => $video_poster,
'preload' => 'auto'
);
echo wp_video_shortcode($attr);
?>
</div>
Many thanks in advance!
now i have made a Flexible Content field called "video-section"
and created a layout called "video-main' which contains a URL field called
"video-url"
<?php if( have_rows('video-section') ): ?>
<?php while( have_rows('video-section') ): the_row(); ?>
<?PHP
//get the URL
if( get_row_layout() == 'video-main' ): ?>
<?php echo the_sub_field('video-url'); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
I used this code to echo all different URLs that included in the post that I was created for testing try it if that what you want to do
To answer my own question, I was targetting the wrong fields.
Needed to target sub_fields instead of get_field.
In ACF I set the return value of my subfield to array instead of url, to finish it I passed the url variable in the video shortcode array as an attribute.
Correct code below:
<div class="section section-type-video flex">
<?php
$video_mp4 = get_sub_field('video_file'); // MP4 Field Name
$video_poster = get_sub_field('video_poster_image'); // Poster Image Field Name
$video_url = $video_mp4['url'];
// Build the Shortcode
$attr = array(
'mp4' => $video_mp4,
'src' => $video_url,
'poster' => $video_poster,
'preload' => 'auto'
);
echo wp_video_shortcode($attr); ?>
</div>
please check the ACF Documentation for get_row_layout() how to display it
Click here! to see it
I am very new to wordpress theme development and I have created a custom post and custom taxonomy . I have created a image acf on taxonomy now I want to show all the taxonomy term and their acf fields on front-page . I can get all term by using get_terms() function but I don't know how to get acf field of that taxonomy.
$terms = get_terms(array(
"taxonomy" => "categories",
"hide_empty" => false ));
foreach($terms as $term):
echo $term->name;
endforeach;
I want term name and image acf field of that term on front-page.php . Any advice will be helpful and thanks in advance.
Please try this code
<?php
$terms = get_the_terms(get_the_ID(), "categories");
if (!empty($terms)): ?>
<ul>
<?php foreach ($terms as $term): ?>
<li class="<?php echo $term->slug; ?>">
<img src="<?php the_field("image_field_name", $term); ?>" />
</li>
<?php endforeach; ?>
</ul>
<?php endif;
?>
For more details you can check this documentation.
I have this code in my child theme but it's pulling in the category featured image instead of the individual single posts image, when viewing a list of single post under category. The code is within a loop.
<?php if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail( "full" ); ?>
<?php } ?>
If anybody gets caught up on retreiving an image outside of a single post this solution worked for me.
<img src="<?php $image_source = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); echo $image_source[0]; ?>">
The $post->ID may have to change depending on what you're working on.
I have in my template-parts file where I created this code to list pages under page with ID=347 (I just wanted to create list of products using template file on one, single page). Code looks like this:
<?php $pages = get_pages(array('child_of' => 347, 'sort_column' => 'post_date', 'sort_order' => 'desc')); ?>
<?php foreach ($pages as $page): ?>
<div class="fw-col-xs-12 fw-col-sm-4 product-box">
<?php echo get_the_post_thumbnail($page->ID, 'large');?>
<div class="fw-heading fw-heading-h3 naglowek-maly"><h3><?php echo $page->post_title; ?></h3></div>
<a class="fw-btn fw-btn-1 button1" href="<?php echo get_permalink($page->ID); ?>" title="<?php echo esc_attr($page->post_title);?>"><span>check more</span></a>
</div><!--product box-->
<?php endforeach; ?>
And now I want to add extra field there using Advanced Custom Fields plugin. I need to display below the small info (it will be product dimensions). So I created custom field and I am able to show field only if I add an ID of this page for example:
<?php the_field('product_dimensions', 200); ?>
How to make it dynamic? I was trying to use simply:
<?php the_field('product_dimensions'); ?>
But nothing shows then. Do I have to add there some extra code to read the ID for each page?
just add $post->ID
the_field('product_dimensions', $post->ID);
Okay, I have the code pulling the custom fields URL and title of the url. Now I can't seem to get it to show the second featured blog. Here is the working code.
<?php $related = get_post_meta($post->ID, "Featured-Blog", $single=true);
$related=explode(',',$related);
$args = array_merge( array('post__in' => $related, $wp_query->query ) );
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="<?php the_ID(); ?>">
<p class="caption"><?php the_title(); ?></p>
</div>
<?php endwhile; else: ?>
<p>no related</p>
<?php endif; wp_reset_query();?>
This code example here produces two results, which is almost what I want. Which is caused by the foreach I believe. I do not want to use the code below, but I need to find a way to add the foreach I think to get it to list all of the featured-blogs if I have more than one.
<?php
$custom_fields = get_post_custom($post_id); //Current post id
$my_custom_field = $custom_fields['Featured-Blog']; //key name
foreach ( $my_custom_field as $key => $url )
echo $key ="<a href='".$url."'>TEST</a><br /><br /><br/>";
?>
Here is a screenshot showing my Custom Fields if it helps at all, and the results they are showing on the site. screenshot
Example 1: You are using the actual word 'TITLE' for the link use <?php the_title() ?> instead
Example 2: You are not building link at all. Your href attribute is empty. Cut echo $featured_blog1 and paste it to href attribute to end up like so:
Example 3: Same as 2
Also you can delete dose instructions or put them inside <?php ?> code so they are not visible to viewers.
Hope this helps.
If you need more info just ask. ;)