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);
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 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/
I have created two custom post types, 'offers' and 'brands'. I have made the relationship using an advanced custom fields relationship field 'related_brand'.
Each brand uses a single post and the related posts are shown on the same page, however, when using post_class on the related posts I just noticed they are displaying the brands post classes and not the related posts classes, which are different.
How can I pull in the correct post_class for the related posts bearing in mind they are two separate post types.
The following pulls in the related posts.
$offers = get_posts(array(
'post_type' => 'offers', // name of custom post type
'meta_query' => array(
array(
'key' => 'related_brand', // name of custom field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
Watered down code that displays the related posts.
<?php if ($offers) : ?>
<?php foreach ($offers as $offer) : ?>
<div <?php post_class(); ?>> <!--Displaying the incorrect post classes-->
<div>
<a href="<?php the_permalink($offer->ID); ?>">
<?php echo get_the_post_thumbnail($offer->ID,); ?>
</a>
</div>
<div>
<a href="<?php the_permalink($offer->ID); ?>">
<?php echo get_the_title($offer->ID); ?>
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
In his case he is taking the post_class with the id of the current loop and not that of foreach but of the current loop of the tag.
To get it right you should use post_class with two arguments post_class ('', $ post_id); where the second is the post id. In your case the code should be + - like this:
<?php if ($offers) : ?>
<?php foreach ($offers as $offer) : ?>
<div <?php post_class('', $offer->ID ); ?>> <!--Displaying the incorrect post classes-->
<div>
<a href="<?php the_permalink($offer->ID); ?>">
<?php echo get_the_post_thumbnail($offer->ID,); ?>
</a>
</div>
<div>
<a href="<?php the_permalink($offer->ID); ?>">
<?php echo get_the_title($offer->ID); ?>
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
quick Wordpress question.
I want to display the last 30 posts from my category "photos", but to only display the featured image on the relevant page as a link that will take the user to the actual post.
I have managed to do this, but it displays posts from all categories, not the "photos" category. Code I'm using is below.
I'm sure it's simple, but would love to know how to display just the recent posts from the photo category only (as the featured image).
Thanks
<!-- In functions.php -->
function recentPosts() {
$rPosts = new WP_Query();
$rPosts->query('showposts=100');
while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
<div class="photos">
<li class="recent">
<?php the_post_thumbnail('recent-thumbnails'); ?>
</li>
</div>
<?php endwhile;
wp_reset_query();
}
<!-- this is on the page template -->
<?php echo recentPosts(); ?>
Just add the category selection to your query.
Replace:
$rPosts = new WP_Query();
$rPosts->query('showposts=100');
With:
$rPosts = new WP_Query('category_name=photos&showposts=100');
Reference: http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category
You need to provide the loop argument that you want post only of certain category by providing category id cat=1. Replace 1 with the id of your photos category
<!-- In functions.php -->
function recentPosts() {
$rPosts = new WP_Query();
$rPosts->query('showposts=100&cat=1');
while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
<div class="photos">
<li class="recent">
<?php the_post_thumbnail('recent-thumbnails'); ?>
</li>
</div>
<?php endwhile;
wp_reset_query();
}
<!-- this is on the page template -->
<?php echo recentPosts(); ?>
Add the category ID to your query arguments. echo on the last line is redundant by the way. Your function outputs the HTML directly rather than returning it.
Finally your original markup was invalid. An li can't be the child of a div so I've corrected that in my example.
function recentPosts() {
$rPosts = new WP_Query( array(
'posts_per_page' => 30,
'cat' => 1
'no_found_rows' => true // more efficient way to perform query that doesn't require pagination.
) );
if ( $rPosts->have_posts() ) :
echo '<ul class="photos">';
while ( $rPosts->have_posts() ) : $rPosts->the_post(); ?>
<li class="recent">
<?php the_post_thumbnail( 'recent-thumbnails' ); ?>
</li>
<?php endwhile;
echo '</ul>';
endif;
// Restore global $post.
wp_reset_postdata();
}
<!-- this is on the page template -->
<?php recentPosts(); ?>
I just started WP, and made a table with many rows arrived at WP house. But don't know how to show. basic content well shown but custom fields. learnt that a page have to be created to deal them in order to retrieve custom typed posts.
the following is from my content-movie.php under twentyfourteenchild:
/* translators: %s: Name of current post */
the_content();
// handling movie stuff starts
$p_id = $post->ID;
$ar_fields = array( 'studio','director','starring','grade');
.
.
foreach $ar_fields as $field
$some_field = get_post_custom_values($field, $p_id);
do some thing dealing $some_field...
end for
===================
In order to make a regular page to populate movie-typed custom posts, do I have to put such codes in archive-movie, page-movie single-movie etc ?
I guess, somewhere it can be dealt instead of putting same scripts in many files.
I really want someone to help me go right direction.
firstly create a custom page template by doing the following:
Copy the regular page.php and rename it to page-movies.php.
In the page header add the following php code:
/**
* Template Name: Movie Page
*/
Now the page template should be available in the backend, select the Movie Page as your page template.
Now replace the regular page query with your query, here is an example below:
<?php $args = array( 'numberposts' => 7, 'order'=> 'ASC', 'post_type' => 'movies');
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="item">
<div class="item-content">
<?php if ( has_post_thumbnail() ) : ?>
<div class="thumbnails"> <a class="ajax" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'projects-thumbnail' ); ?>
</a>
<div class="copy">
<h3><a href="<?php the_permalink(); ?> ">
<?php the_title(); ?>
</a></h3>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
This should do the trick...