1

I've been using the following code to get a template part based on the parent term. It was working fine until I updated WordPress to v4.4 earlier today. Since updating if there is also a child term selected it no longer works to bring in the template part.

Any ideas? I'm tearing my hair out.

<?php

$terms = get_the_terms( $post->id, 'type', array( 'parent' => 0 ) );
$terms_slugs = array();
foreach( $terms as $term ) {
    $terms_slugs[] = $term->slug;
}

if( !empty($terms_slugs) ) :
  get_template_part( 'blocks/block', array_pop($terms_slugs) );
else : endif;

?>

Edit: Solved it! Changing array_pop to array_shift. No idea what the difference is or why array_pop would suddenly stop working. Phew!

  • I'm assuming your file is: theme-name/blocks/block-termname? How does the term-slug look, can you give us an example? – Howdy_McGee Jan 5 '16 at 15:36
  • File is theme-name/blocks/block-written-in-stone and the term slug is written-in-stone – leanda Jan 5 '16 at 15:39
  • What does "if there is also a child term selected " mean? Where does this fail? With get_the_terms()? With get_template_part()? Can you narrow this down? – s_ha_dum Jan 5 '16 at 15:51
  • If the parent category is the only category selected then it works no problem, but if I select a second sub category (child) then it fails to use the template part. It's really odd, as this was working before I upgraded WP this morning. – leanda Jan 5 '16 at 15:53
  • 1
    Please do not edit the solution into the question. – s_ha_dum Jan 5 '16 at 17:55
1

leanda reports that the problem has been resolved:

Solved it by changing array_pop to array_shift.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.