【问题标题】:Pass custom field into wordpress loop array将自定义字段传递到 wordpress 循环数组
【发布时间】:2019-03-05 13:10:35
【问题描述】:

我有一个高级自定义,输出一个选择的分类,我试图将它传递到 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】:

    改变这个

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

    收件人:

    'portfolio_category' => $term->slug
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 2016-08-12
      • 2016-09-23
      相关资源
      最近更新 更多