ACF repeater inside group - wordpress

I tried to get ACF repeater from the group field.
I must be blind, but I cannot find a mistake inside my code.
<?php if( have_rows( 'acf_group' ) ): ?>
<?php while( have_rows( 'acf_group' ) ): the_row(); ?>
<?php if( have_rows( 'acf_repeater' ) ): ?>
<ul>
<?php while( have_rows( 'acf_repeater' ) ): the_row();
$link_text = get_sub_field( 'acf_repeater_field_text' );
$link_url = get_sub_field( 'acf_repeater_field_url' );
?>
<li>
<?php echo $link_text; ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>

Related

How to get the total number of count in acf field

<p class="showitem">Showing
<span>
<?php if ( have_rows( 'packaging', 'options' ) ) : ?>
<?php while ( have_rows( 'packaging', 'options' ) ) : the_row(); ?>
<?php $count = count( get_sub_field( 'content' ) ); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php echo $count; ?>
</span>
items</p>
I need output Showing 20 items. But it shows Showing 1 items
I tried to give inside the loop but it shows 1 20 times

Wordpress how to display query results in specific div class if total number of posts is < 2

I'm working on a new Wordpress site and am trying to achieve something specific. I'd like to do a query_post and if there is only 1 post available, display that information in a specific div class, otherwise use a different class. Here is a simplified version of the code I am currently using. The one I will end up using, actually has additional query_posts within this:
<?php query_posts('post_type=events&showposts=-1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$type = get_field('event_type');
if( $type && in_array('public', $type) ): ?>
<a href="<?php the_permalink(); ?>">
<div id="event" class="public secondary left">
<?php else: ?>
<a href="mailto:johnsmith#gmail.com?subject=<?php echo the_field('event_date'); ?> - <?php the_title(); ?> Event Inquiry">
<div id="event" class="private secondary left">
<?php endif; ?>
<?php else: ?>
<?php
$type = get_field('event_type');
if( $type && in_array('public', $type) ): ?>
<a href="<?php the_permalink(); ?>">
<div id="event" class="public secondary right">
<?php else: ?>
<a href="mailto:johnsmith#gmail.com?subject=<?php echo the_field('event_date'); ?> - <?php the_title(); ?> Event Inquiry">
<div id="event" class="private secondary right">
<?php endif; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
I hope this makes sense..
You can check the global query after query_posts() like this before you run through the posts and display them:
<?php query_posts( 'post_type=events&showposts=-1' );
global $wp_query;
$classes = array( 'secondary' );
if( $wp_query->post_count == 1 ) :
$classes[] = 'left';
else :
$classes[] = 'right';
endif; ?>
With the above, your code can be shortened to something like this:
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();
$type = get_field( 'event_type' );
if ( $type && in_array( 'public', $type ) ) :
$classes[] = 'public';
$action = get_permalink();
else :
$classes[] = 'private';
$action = 'mailto:johnsmith#gmail.com';
endif; ?>
<!-- Output HTML -->
<a href="<?php echo $action; ?>">
<div id="event" class="<?php echo join( ' ', $classes ); ?>">
<?php endwhile;
endif; ?>

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; ?>

How do I sort category results on this code?

I have this code on the category.php file and I need to sort the results by ASC cause right now results are in the reverse order.
Thanks in advance.
<ol class="search-results-list">
<?php
// Return Event Items
$i = 0;
while (have_posts()) : the_post();
if( get_post_type() == 'researcher' ) {
$i++; ?>
<li><strong><?php the_title(); ?></strong><br /> <?php print_excerpt(200); ?></li>
<?php }
endwhile;?>
<?php if( $i == 0 ) { ?><li><?php _e( 'No results were found.', 'qns' ); ?></li><?php } ?>
<!--END .search-results-list -->
</ol>
Add below code into your functions.php file
if ( !function_exists( 'get_cat_id_by_slug' ) ) {
function get_cat_id_by_slug ($get_slug) {
$CatId = get_term_by( 'slug', $get_slug, 'category' );
$CatId = $CatId->term_id;
return $CatId;
}
}
Add below code into your category.php
<?php
$category_id = get_cat_id_by_slug('researcher');
$args = 'cat=' . $category_id . 'order=asc';
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post();
?>
<ol class="search-results-list">
<li>
<strong>
<?php the_title(); ?>
</strong><br />
<?php print_excerpt(200); ?>
</li>
</ol>
<?php
endwhile;
else :
?>
<ol class="search-results-list">
<li><?php _e( 'No results were found.', 'qns' ); ?></li>
</ol>
<?php
endif;
?>

Wordpress - display certain categories on homepage

I have been trying to alter my current code, but it makes no sense to me (it doesn't do what I was told it does). All I want to do is only display posts with category 13 on my homepage and I want to be able to add multiple categories to a post.
Post 1 (Category 13 and 1) = displayed on homepage
Post 2 (Category 13 and 4 and 5) = displayed on homepage
Post 3 (Category 6 and 1) = not visible on homepage
Post 4 (Category 2) = not visible on homepage
This is my current code to only show category 13 on my homepage, if another category is added to the post it won't be displayed at all.
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
if (is_home()) {
query_posts("cat=-6,-4,-1,-11");
}
?>
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat' => 13,
'posts_per_page' => 5,
'paged' => $paged);
query_posts($args);
?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Geen berichten beschikbaar', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Helaas, er zijn nog geen gearchiveerde berichten in deze categorie. ', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
You're looking way too hard at this code. You don't need to exclude anything, you just need to include cat 13, which will include everything that is in cat 13; even if it is in other categories as well. Just run a regular WP_Query() like so:
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
if (is_home()) {
// The Query
$the_query = new WP_Query("cat=13, paged=".get_query_var('paged'));
}
?>
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Geen berichten beschikbaar', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Helaas, er zijn nog geen gearchiveerde berichten in deze categorie. ', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
(had to add a few charachters or I could not edit, sorry :P)
Try this...
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=6&cat=13'.'&paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
Your stuff goes here...
<?php endwhile; ?>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
Try this code
<?php
global $post;
if (is_home()) {
$args = array( 'numberposts' => 5, 'category' => 13 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content( 'Continue reading <span class="meta-nav">ยป</span>); ?>
</div>
<?php endforeach;
}
?>
This will display the title and content. If you want additional information like date, author, category, tag etc, you may have to use other template tags. Also if you want to style these differently, then you have to use appropriate CSS.

Resources