doufei16736
2019-03-05 13:10
浏览 49
已采纳

将自定义字段传递到wordpress循环数组

I've got an advanced custom, outputting a chosen taxonomy, which I'm trying to pass into an array inside a WordPress loop.

The loop, which shows a specific taxonomy of my custom post type, is here:

<?php

$loop = new WP_Query( array(
    'post_type' => 'portfolio',
    'portfolio_category' => 'social-media-marketing',
    'posts_per_page' => -1,
  )
  );
?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

  <h3><?php the_title(); ?></h3>

<?php endwhile; wp_reset_query(); ?>

I would like to replace the portfolio category with the results from the custom post type, so the user can select which taxonomy to display.

The code I have to pull in the Advanced custom field taxonomy is here:

<?php 

$term = get_field('portfolio_category');

if( $term ): ?>

  <h2><?php echo $term->slug; ?></h2>

<?php endif; ?>

Both bits of code work separately. I've tried running them both together like this:

<?php

$term = get_field('portfolio_category');

$loop = new WP_Query( array(
    'post_type' => 'portfolio',
    'portfolio_category' => 'echo $term->slug;',
    'posts_per_page' => -1,
  )
  );
?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

  <h3><?php the_title(); ?></h3>

<?php endwhile; wp_reset_query(); ?>

As well as a few other things, but I can't seem to get it to display anything... What am I doing wrong??

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpalou5352 2019-03-05 13:14
    已采纳

    Change this

    'portfolio_category' => 'echo $term->slug;'
    

    To:

    'portfolio_category' => $term->slug
    

    You were passing the variable as a string instead of a variable.

    点赞 打赏 评论

相关推荐 更多相似问题