Advanced Custom Fields loop behaviour - wordpress

I have a page with 3 custom fields. For each of these fields I need to use a template. So what I do is
<?php if( have_rows('page_block') ):?>
<?php while ( have_rows('page_block') ) : the_row();?>
<?php if( get_row_layout() == 'content_block' ):?>
<?php get_template_part("content-block");?>
<?php elseif( get_row_layout() == 'slider' ):?>
<?php get_template_part("slider"); ?>
<?php elseif( get_row_layout() == 'news_block' ):?>
<?php get_template_part("news-block"); ?>
<?php endif; ?>
<?php endwhile; ?>
....
<?php endif; ?>
The problem is that only the first two fields are displayed on the page. So when the order of the fields is
Content-block
Slider
News-block
Only the content-block and slider are shown. When I place the slider at the bottom, only the content-block and news-block are displayed.
Any idea what the problem might be?
EDIT
Forgot to mention that when I echo the get_row_layout(), I get the names of all 3 fields.
<?php if( have_rows('page_block') ):?>
<?php while ( have_rows('page_block') ) : the_row();?>
<?php echo get_row_layout(); ?>
<?php endwhile; ?>
<?php endif; ?>
Displays content_block slider news_block

Found the problem. The slider template was using a WP_Query and I had to add <?php wp_reset_query(); ?> to fix the problem. I think the query broke the loop or something like that. Either way, problem was fixed by adding said line!

Related

Wordpress: check if current post is first in the loop

I need to style the first post in the loop differently from the rest in a wordpress theme template. Is there a specific wordpress function to check for this or do I just need to set a "first" flag like the below?
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php if ( $first = !isset( $first ) ) : ?>
<!-- First Post HTML -->
<?php else : ?>
<!-- Every other posts HTML -->
<?php endif; ?>
<?php endwhile; ?>
The function I'm looking for would replace the $first = ! isset( $first) check. Does this function exist in WordPress?
Try this
<?php $Inc=0; ?>
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php if ( $Inc==0 ) : ?>
<!-- First Post HTML -->
<?php else : ?>
<!-- Every other posts HTML -->
<?php endif; ?>
<?php $Inc++; ?>
<?php endwhile; ?>

ACF Flexible Content Fields

I have a number of flexible content fields set up.
One of these is called 'product-picker' and another is called 'product-information'.
The product picker uses the post object functionality to pull in selected products.
I can pull in the default wordpress parts such as:
<?php the_post_thumbnail(); ?>
But I can't pull in the values within 'product-information'.
Is this actually possible? Extracts of code below:
<?php if( have_rows('content_blocks') ):
while ( have_rows('content_blocks') ) : the_row();
if( get_row_layout() == 'product_information' ):
include("inc/product-information.php");
elseif( get_row_layout() == 'products_picker' ):
include("inc/products-picker.php");
endif;
endwhile;
endif; ?>
Then within 'products-picker' I am using the following code:
<?php foreach( $post_objects as $post): ?>
<?php setup_postdata($post); ?>
<div class="product">
<?php the_post_thumbnail(); ?>
<div class="description">
/* Echoing post ID returns correct value */
<h1><?php echo $post->ID; ?></h1>
/* Does not work */
<h1><?php the_sub_field('heading', $post->ID); ?></h1>
/* Does not work */
<?php the_sub_field('description', $post->ID); ?>
</div> <!-- .description -->
</div> <!-- .product-->
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
I might have the structure of my content wrong.
Maybe you can't call flexible content within flexible content?
But I thought it should be possible to do this in light of the fact that the default fields such as:
the_post_thumbnail();
and
the_title();
work.
All your time and help is greatly appreciated.
You have to set up the flexible rows part for each of your post objects. So after you setup your post data, replicate your
if( have_rows('content_blocks') ):
while ( have_rows('content_blocks') ) : the_row();
with the product information fields for each post object.
Check out https://www.advancedcustomfields.com/resources/flexible-content/ and the nested loop example. I think that should get you a little closer.

Display ACF Repeater Fields on Child Pages

I'd like to display the results of an ACF repeater field on child pages. An example of the current code:
<?php if( have_rows('recommended-properties') ): ?>
<?php while ( have_rows('recommended-properties') ) : the_row(); ?>
<?php the_sub_field('recommended-properties-title'); ?>
<?php the_sub_field('recommended-properties-description'); ?>
<?php endwhile; ?>
<?php endif; ?>
Now, I thought I may be able to store the parent page ID in a variable and use that the same way you can get values from another post (see: http://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/)
I tried the following:
<?php $parentPageId = wp_get_post_parent_id( $post_ID ); ?>
<?php if( have_rows('recommended-properties', echo $parentPageId;) ): ?>
<?php while ( have_rows('recommended-properties', echo $parentPageId;) ) : the_row(); ?>
<?php the_sub_field('recommended-properties-title'); ?>
<?php the_sub_field('recommended-properties-description'); ?>
<?php endwhile; ?>
<?php endif; ?>
But no luck unfortunately. Any suggestions for where I can go from here?
Many thanks!
You don't need add echo when you add parameter in function
try below code
<?php $parentPageId = wp_get_post_parent_id( $post_ID ); ?>
<?php if( have_rows('recommended-properties', $parentPageId) ): ?>
<?php while ( have_rows('recommended-properties', $parentPageId) ) : the_row(); ?>
<?php the_sub_field('recommended-properties-title'); ?>
<?php the_sub_field('recommended-properties-description'); ?>
<?php endwhile; ?>
<?php endif; ?>

Next / Prev post link not working wp

Ok so I am having issues with linking to next and previous posts...
Here is my code:
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<div id="project-prev"> <?php previous_post_link('Prev'); ?> </div>
<div id="project-next"> <?php next_post_link('Next'); ?> </div>]
...
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php get_footer(); ?>
I have read places that next/prev posts requires a 'new WP_Query' query, but have had no such luck. There is no next/prev link rendered out on my site, using the above.
As always appreciate solutions and pointers.
Many thanks
Have you tried following (according to Wordpress codex)
<?php next_post_link('<strong>%link</strong>'); ?>
<?php previous_post_link('<strong>%link</strong>'); ?>
In your divs ... :) If you still encouter problems, then just try something like:
<?php echo get_previous_posts_link('Prev'); ?>
<?php echo get_next_posts_link('Next'); ?>
Should work.
EDIT:
<div id="project-prev"><?php previous_post_link('%link', 'PREV'); ?></div>
<div id="project-next"><?php next_post_link('%link', 'NEXT'); ?></div>
First try and get the next and previous posts.
<?php
$previous_post_url = get_permalink(get_adjacent_post(false, '', true));
$next_post_url = get_permalink(get_adjacent_post(false, '', false));
?>
And then create to <a> tags and echo out the URLs we set above.
<?php if ( $previous_post_url != get_the_permalink() ) : ?>
Previous Project
<?php endif; ?>
<?php if ( $next_post_url != get_the_permalink() ) : ?>
Next Project
<?php endif; ?>
One reason that could make these links not to show is having the posts set as draft. Only published posts will make the previous and next post links render.

WordPress search only works with posts, not pages

I'm working on my own custom WordPress theme, using a blank, default template to start. I haven't edited the search.php file.
Many of my WordPress posts are Pages. Unfortunately, the site search only retrieves posts, not pages.
Any idea how to get the theme to search both posts and pages?
Here is the majority of search.php:
<?php if (have_posts()) : ?>
<h3>Search Results</h3>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h4><?php the_title(); ?></h4>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h3>No posts found.</h3>
<?php endif; ?>
Add this code to your functions.php file.
function wpshock_search_filter( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array('post','page') );
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');
http://wpth.net/limit-wordpress-search-results-to-specific-post-types
WP Search http://wpsear.ch/ has that ability.You can adjust what post types you want to show in results page.
In your search.php, find The Loop and insert this code just after it. You can recognize the Loop because it usually starts with:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
Code to be inserted:
if (is_search() && ($post->post_type=='page')) continue;
So, your code should be like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='page')) continue; ?>
Let me know if it worked.
Lack of page search and search results ranked by post date instead of relevance is a typical WP problem. Try http://wordpress.org/extend/plugins/relevanssi/

Resources