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

问题描述 投票:0回答:1

我有一个高级定制,输出一个选定的分类,我试图传递到WordPress循环内的数组。

循环显示了我的自定义帖子类型的特定分类,在这里:

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

我想用自定义帖子类型的结果替换组合类别,因此用户可以选择要显示的分类。

我必须在高级自定义字段分类中引入的代码如下:

<?php 

$term = get_field('portfolio_category');

if( $term ): ?>

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

<?php endif; ?>

这两段代码分开工作。我试过像这样一起运行它们:

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

除了一些其他的东西,但我似乎无法让它显示任何东西......我做错了什么?

php wordpress custom-post-type advanced-custom-fields
1个回答
1
投票

改变这个

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

至:

'portfolio_category' => $term->slug

您将变量作为字符串而不是变量传递。

© www.soinside.com 2019 - 2024. All rights reserved.