In Advanced Custom Fields (ACF) I created an image field and added that to the categories of a custom post type so every category can have his own image. Then I created a loop that displays the categories of that custom post type. This works but I can't get the image that I uploaded in the ACF field to display.
The code for displaying the categories of the custom post type:
<?php $taxonomy = 'customposttype'; $terms = get_terms($taxonomy); if ( $terms && !is_wp_error( $terms ) ) : ?>
<?php foreach ( $terms as $term ) { ?>
<?php echo $term->name; ?>
// display acf image
<?php } ?>
<?php endif;?>
I tried to work with 'array_combine' but I couldn't get that to work.
Try this code in the loop
$term_image = get_term_meta( $term->term_id , 'category_afbeelding', true);
$image_attributes = wp_get_attachment_image_src( $term_image,’full’);
if ( $image_attributes ) :
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" />
endif;
Related
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; ?>
I'm displaying a list of sibling pages using the below code. As well as retrieving the page title and link. I need to grab a custom field called excerpt and output it for each sibling item. How do I do this using get_pages? Currently the var_dump I'm using is grabbing the current pages excerpt, which is incorrect.
<?php
global $post;
if ( is_page() && $post->post_parent ) {
$children = get_pages('child_of='.$post->ID);
$parent = $post->post_parent;
$our_pages = get_pages('child_of='.$parent.'&exclude='.$post->ID);
$ex = get_field('excerpt', $our_pages);
if (!empty($our_pages)):
foreach ($our_pages as $key => $page_item):
<a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>" title="<?php the_title(); ?>">
<?php echo $page_item->post_title ; ?>
<?php var_dump($ex) ?>
<?php endforeach;
endif;
}
UPDATE
Ok so I have put in <?php print_r($page_item); ?> as suggested and it has returned the following:
I don't see any of my ACF fields here, so how do I retrieve those? What are my next steps?
You should be using the_field(), or echo get_field(), within your forloop, and should pass the post ID as the second parameter of the function:
if (!empty($our_pages)):
foreach ($our_pages as $key => $page_item):
<a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>" title="<?php the_title(); ?>">
<?php echo $page_item->post_title ; ?>
<?php the_field('excerpt', $page_item->ID); ?>
</a>
<?php endforeach;
endif;
You can read more about how get_field() works, in the documentation.
You full code like below:
<?php
global $post;
if ( is_page() && $post->post_parent ) {
$children = get_pages('child_of='.$post->ID);
$parent = $post->post_parent;
$our_pages = get_pages('child_of='.$parent.'&exclude='.$post->ID);
$ex = get_field('excerpt', $our_pages);
if (!empty($our_pages)):
foreach ($our_pages as $key => $page_item):
<a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>" title="<?php the_title(); ?>">
<?php echo $page_item->post_title ; ?>
<?php echo get_post_meta( $page_item->ID, 'excerpt', true); ?>
<?php endforeach;
endif;
}
If you didn't get the data like above then change you custom filed name with any other name and then try.
I've got the same problem with AFC and get_pages().
Try to access ACF fields by retrieving all posts by page type.
$args = array(
'post_type' => 'page',
'numberposts' => -1, // Accepts -1 for all. Default 5.
);
$pages = get_posts($args);
Now ACF get_field('excerpt'); function will work, because get_pages() doesn't return all needed data.
If you'd like to get specific pages like siblings you could pass theirs id's as get_posts() 'include' argument (arr).
Read more about get_posts() in documentation.
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>
`
I have two different custom post types: "Movies" and "Press" with different custom fields.
Now I want to display both custom fields in the single.php.
home.php:
...
$query = array(
'post_type' => 'movies',
'posts_per_page' => 3,
);
$loop = new WP_Query( $query );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="movie_cover">
<img src="<?php the_field('cover'); ?>" title="<?php echo get_the_title() ?>" />
</div>
...
single.php:
get_header(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'single', 'film' ); ?>
<?php endwhile; // end of the loop. ?>
<?php get_footer(); ?>
single-film.php:
...
<img src="<?php the_field('cover'); ?>" title="<?php echo get_the_title() ?>" />
...
the field 'cover' is from "Movies", but now i want to list the field 'release' from "Press".
Does I need a second loop?
if you add movie_id custom field to press, and set it to the ID of the movie
then you can do something like inside single-film.php
query_posts('meta_key=movie_id&meta_value=' . get_the_ID());
and do another loop for the reviews
while have_posts() ...
There's a lot of additional stuff but this should get you going in the
right direction.
I'd consider using get_posts instead of query_posts to not override wordpress' main query loop, but then you'd have to do your own manual loop:
$posts = get_posts('meta_key=movie_id&meta_value=' . get_the_ID());
foreach ($posts as $p) {
echo $p->post_title;
...
}
I have a slider script for my homepage (wp nivo slider), but only want to show slides (posts) if todays date is before post_end_date (custom field). This is so that I do not have to manually remove posts that are no longer relevant.
This is the code that loads the posts. Any help would be greatly appreciated.
Custom post field: post_end_date
<?php
$category = get_option('wpns_category');
$n_slices = get_option('wpns_slices');
?>
<?php query_posts( 'cat='.$category.'&posts_per_page=$n_slices' ); if( have_posts() ) : while( have_posts() ) : the_post(); ?>
<?php if ( '' != get_the_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif ?>
<?php endwhile; endif;?>
<?php wp_reset_query();?>
<?php $meta_values = get_post_meta($post_id, $key, $single); ?>
in this function $postid is that id of wich is coming in loop and key is that name of custom field last in $single you can write true of false
get the custom fild by this function after that try the if condition for you custom postfeild values
hope this will help you