Skip to content

Instantly share code, notes, and snippets.

@faaezahmd
Last active October 3, 2018 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faaezahmd/60ff18c522d39470aa4a273e45052f68 to your computer and use it in GitHub Desktop.
Save faaezahmd/60ff18c522d39470aa4a273e45052f68 to your computer and use it in GitHub Desktop.
Get WP ACF-Plugin Field value from child category
<?php
/*
* Get Advanced Custom Field value from child category
*/
$cats = get_categories('child_of='.get_query_var('cat'));
foreach ($cats as $cat) :
$args = array(
'posts_per_page' => 3, // max number of post per category
'category__in' => array($cat->term_id)
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) :
echo '<h3>'.$cat->name.'</h3>';
// 'background_image' Custom Field
the_field('background_image', 'category_' . $cat->term_id);
echo $cat->description;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a> <br />
<?php endwhile; ?>
<?php else :
echo 'No Posts for '.$cat->name;
endif;
endforeach;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment