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

将自定义字段传递到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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程