I have a custom post type called 'Projects' and I have added flexible field content to my single-projects.php. I'm trying to add a hero image for each project that sits in the main header.php as I want my hero image to sit behind the navigation. I have everything working but my hero image is displaying on CPT archive pages and CPT single pages but I just want to show the hero image on each project page.
in my header.php file I have:
<?php
// check if the flexible content field has rows of data
if( have_rows('project_flexible') ):
// loop through the rows of data
while ( have_rows('project_flexible') ) : the_row();
if( get_row_layout() == 'project_hero_image'):
$hero_image = get_sub_field('image'); ?>
<div class="hero-image"><img src="<?php echo $hero_image; ?>">
</div>
<?php
endif;
endwhile;
else :
// no layouts found
endif;
?>
maybe you could try this to only display on single page:
<?php
// check if the flexible content field has rows of data
if( have_rows('project_flexible') ):
// loop through the rows of data
while ( have_rows('project_flexible') ) : the_row();
if( get_row_layout() == 'project_hero_image'):
$hero_image = get_sub_field('image'); ?>
// Added this if statement
<?php if(is_single()){ ?>
<div class="hero-image"><img src="<?php echo $hero_image; ?>"></div>
<?php } ?>
<?php
endif;
endwhile;
else :
// no layouts found
endif;
?>
Related
In Wordpress, in order to display a nested repeater ACF field, I adapted a few lines in php that I insert in the archive page of post.
In the archive, the hard code runs very well and the lines created by the code are displayed as expected.
I need to use this code via Elementor. To do that, I need to create a shortcode.
I created a shortcode with the code.
Nothing runs good, nothing is displayed, even when I desactive Elementor
Can you, please, help me,
Bruno
Here is the code:
function acf2repeaters_ingredients_function() {
while ( have_posts() ) : the_post();
// check for rows (parent repeater)
if( have_rows('en-tete_ingredients') ): ?>
<div id="en-tete_ingredients">
<?php // loop through rows (parent repeater)
while( have_rows('ingredients') ): the_row(); ?>
<div>
<h3><?php the_sub_field('en-tete_ingredients'); ?></h3>
<?php // check for rows (sub repeater)
if( have_rows('liste_des_ingredients') ): ?>
<ul>
<?php // loop through rows (sub repeater)
while( have_rows('ingredients') ): the_row(); ?>
<li <?php echo ' class="nombre"'; ?>><?php the_sub_field('quantite');?></li>
<li <?php echo ' class="unite"'; ?>><?php the_sub_field('unite');?></li>
<li <?php echo ' class="ingredient"'; ?>><?php the_sub_field('ingredient');?></li>
<?php endwhile; ?>
</ul>
<?php endif; //if( get_sub_field('ingredients') ): ?>
</div>
<?php endwhile; // while( has_sub_field('en-tete_ingredients') ): ?>
</div>
<?php endif; // if( get_field('en-tete_ingredients') )
endwhile; // end of the loop.
}
add_shortcode( 'acf2repeaters_ingredients', 'acf2repeaters_ingredients_function' );
Solution:
Remove line: while ( have_posts() ) : the_post();
Remove line: endwhile; // end of the loop.
Then the shortcode rocks!
I want to display ACF Flexible content in all single posts on an archive page. Currently the following code on my Archive page displays only the last posts Flexible content. I would like to display all posts using flexible content on an archive page.
Any help would be much appreciated.
<?php
if( have_rows('fabric') ):
while ( have_rows('fabric') ) : the_row();
?>
<div class="sales-wrap-info-single">
<?php if( get_sub_field('image') ): ?>
<img src="<?php the_sub_field('image'); ?>" />
<? endif ?>
</div>
<?php
endwhile;
else :
// no rows found
endif;
?>
I am building a basic Wordpress blog which is build on Duplex theme.
I Would like to do some minor customization to the site where in the site has an Instagram widget after two posts and then continue the posts.
Just to simplify the order of the page would be :
2 posts.
Instagram widget
Continue from 3rd post.
The Front page displays set to posts like the way I want.
How can I achieve getting the Instagram widget in between of the posts without disturbing the flow?
What you do is run the loop two times to display the first posts, reset it and then start it again after your widget - skipping the two first posts.
You may achieve this like so:
First run the loop, displaying the two first posts.
// The Query
$the_query = new WP_Query( array( 'posts_per_page' => 2 ) );
// The Loop
if ( $the_query->have_posts() ) {
//Display post
}
Reset the query, and display your widget
/* Restore original Post Data */
wp_reset_postdata();
//Your widget here.
Display the next posts, skipping the first two.
// The Second Query
$the_query = new WP_Query( array( 'posts_per_page' => 10, 'offset' => 2 ) );
// The Loop
if ( $the_query->have_posts() ) {
//Display post
}
Thanks a ton #danjah! Below is my code
<?php get_header(); ?>
<div id="content">
<div class="posts clearfix">
<?php if ( is_search() ) { ?>
<h2 class="archive-title"><i class="icon-search"></i> <?php echo $wp_query->found_posts; ?> <?php _e('Results for','hyphatheme'); ?> "<?php the_search_query() ?>" </h2>
<?php } else if ( is_tag() ) { ?>
<h2 class="archive-title"><i class="icon-tag"></i> <?php single_tag_title(); ?></h2>
<?php } else if ( is_day() ) { ?>
<h2 class="archive-title"><i class="icon-time"></i> <?php echo get_the_date(); ?></h2>
<?php } else if ( is_month() ) { ?>
<h2 class="archive-title"><i class="icon-time"></i> <?php echo get_the_date('F Y'); ?></h2>
<?php } else if ( is_year() ) { ?>
<h2 class="archive-title"><i class="icon-time"></i> <?php echo get_the_date('Y'); ?></h2>
<?php } else if ( is_category() ) { ?>
<h2 class="archive-title"><i class="icon-list-ul"></i> <?php single_cat_title(); ?></h2>
<?php } else if ( is_author() ) { ?>
<h2 class="archive-title"><i class="icon-pencil"></i> <?php echo get_userdata($author)->display_name; ?></h2>
<?php } ?>
<?php
if ( have_posts()) :
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
else:
/*
* If no posts are found in the loop, include the "No Posts Found" template.
*/
get_template_part( 'content', 'none' );
endif;
?>
</div><!--END .posts-->
<?php if ( hypha_page_has_nav() ) : ?>
<!-- post navigation -->
<?php $post_nav_class = ( get_option( 'hypha_customizer_infinite_scroll' ) == 'enable' ) ? ' infinite' : '' ; ?>
<div class="post-nav<?php echo $post_nav_class; ?>">
<div class="post-nav-inside clearfix">
<div class="post-nav-previous">
<?php previous_posts_link(__('<i class="icon-arrow-left"></i> Newer Posts', 'hyphatheme')) ?>
</div>
<div class="post-nav-next">
<?php next_posts_link(__('Older Posts <i class="icon-arrow-right"></i>', 'hyphatheme')) ?>
</div>
</div>
</div>
<?php endif; ?>
<?php if ( is_single() ) { ?>
<!-- comments template -->
<?php if ( 'open' == $post->comment_status ) : ?>
<?php comments_template(); ?>
<?php endif; ?>
<?php } ?>
</div><!--END #content-->
One approach would be the one mentioned would be the one by #danjah wehre you're creating a secondary loop on the page to get the first two posts. Since you're new to wordPress, lets start at the beginning.
All of your templating revolves around the loop. The loop works with the query already defined on the page. wordPress has various default queries depending on which template its loading (a post, an archive, a single post, a page, the index)
The Loop
The Tempalte Heirarchy
So a) wordpress defines the query based on the route you're on, and b) you use the loop to get at it. BUT. You can alater the query, override it, or run additional queries.
Have a loop at WP_Query
Lets loop at the standard loop. In your index.php you probably have something like :
<!-- Start the Loop. -->
<?php if ( have_posts() ) :
while ( have_posts() ) :
the_post(); ?>
<!-- use markup or template helpers in here -->
<?php endwhile;
endif; ?>
While you could run a secondary query before the main loop, insert the thing you want, and then alter the main loop to skip those first two, the second way would be to, rather than creating another loop and altering the main one, why not rather keep a count. You could do this with a variable that you increment (its just a standard php while loop after all), But I believe that you can also get the current post index from the global $wp_query object.
$index = 1;
<!-- Start the Loop. -->
<?php if ( have_posts() ) :
while ( have_posts() ) :
the_post(); ?>
<!-- use markup or template helpers in here -->
<?php if ($index == 2) : ?>
<!-- do something after the second post -->
<?php endif; ?>
<!-- increment the index -->
$index++;
<?php endwhile;
endif; ?>
I believe you can get the index as well with $wp_query->current_post, but I haven't done this for a while, and haven't tested this.
Hope it helps!
Using ACF (Advanced Custom Fields), I set up a field (slides) with two subfields (title) and (slide). In my header.php file, I have this code which outputs a list of the title subfield.
// header.php //
<nav id="site-navigation" class="main-navigation" role="navigation">
<?php $frontpage_id = get_option('page_on_front'); ?>
<?php if ( is_singular() && have_rows('slides', $frontpage_id) ): ?>
<ul>
<?php while ( have_rows('slides', $frontpage_id) ) : the_row(); ?>
<li class="group"><?php the_sub_field('title'); ?></li>
<?php endwhile;?>
</ul>
<?php endif; ?>
</nav>
Then in my index.php file, I have div.content. In this div, I would like to output a list of the corresponding slide subfield. How can I do this?
// index.php //
<div class="content"></div>
You could repeat a similar loop in the index.php file -
<?php
$frontpage_id = get_option('page_on_front');
if( have_rows('slides', $frontpage_id) ):
while( have_rows('slides', $frontpage_id) ) : the_row();
// if your 'slide' field is an image
$slide = get_sub_field('slide'); ?>
<img src="<?php echo $slide['url']; ?>" alt="<?php echo $slide['alt']; ?>" />
<?php endwhile;
endif;
?>
Or you could grab the entire array of data associated with the repeater using get_field('slides', $frontpage_id); - set that as a global variable and then use a foreach loop in both files. I think the first option is easier.
I'm using Advanced Custom Fields on my website.
I have a repeater field called anime_par, with sub_field called animateur. Sub field animateur is a post-object.
I’m using this inside a loop in my page, a loop that displays posts from a category inside a custom post type.
What I’m trying to do is to display the post name and post link of the animateur selection inside my page.
Here is the code I’m using but it’s not working, it displays the permalink of my current page, not the one selected in the custom field.
<?php while(has_sub_field('anime_par')): ?>
<?php echo get_title('the_sub_field("animateur")'); ?>
<?php endwhile; ?>
Any suggestions to make this work?
thanks for your help,
This method is working for me, per the repeater and post object docs on ACF. You've got to set up the post object inside of the repeater loop.
I added in your field names, and some completely optional html to show the structure.
Hope it helps.
<!-- Start Repeater -->
<?php if( have_rows('anime_par')): // check for repeater fields ?>
<div class="a-container">
<?php while ( have_rows('anime_par')) : the_row(); // loop through the repeater fields ?>
<?php // set up post object
$post_object = get_sub_field('animateur');
if( $post_object ) :
$post = $post_object;
setup_postdata($post);
?>
<article class="your-post">
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php // whatever post stuff you want goes here ?>
</article>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<!-- End Repeater -->
<?php endif; ?>
the_sub_field doesn't work without has_sub_field
What you have to do it is use loop with has_sub_field as it said in the documenration http://www.advancedcustomfields.com/resources/functions/the_sub_field/
or you can use get_field('repeater_sluf') like that
$rows = get_field('repeater_field_name' ); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['sub_field_name' ]; // get the sub field value
<?php if(get_field('favourite_design_quarters', 'user_'.$current_user->ID)): ?>
<?php while(has_sub_field('favourite_design_quarters', 'user_'.$current_user->ID)):
$company = get_sub_field('company_name');
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $company->ID ), 'package-thumbnail' );
?>
<tr>
<td><img src="<?php echo $image[0]; ?>" alt="<?=$company->post_title;?>" /></td>
<td><?=$company->ID;?></td>
<td style="text-align:left;"><?=$company->post_content;?></td>
<td><?=$company->post_date;?></td>
<td>Delete</td>
</tr>