【问题标题】:WordPress Adding Variables to Loop ArgumentsWordPress 将变量添加到循环参数
【发布时间】:2017-02-18 17:39:44
【问题描述】:

我有一个名为“部门”的自定义帖子类型和另一个名为“挑战”的帖子类型。挑战帖子类型有一个名为“部门类型”的分类法 - 与部门名称相同。

我创建了一个名为“single-sector.php”的页面,在该页面上显示一个包含与该部门相关的挑战的循环。

当我编写用于显示挑战的循环时,如何将 'sectortype' => 'advanced-education' 设为变量,以便它可以在其他单扇区页面上工作?

这就是我的循环...

<?php $challenge_args = array(
     'post_type' => 'challenge',
     'sectortype' => 'advanced-education', //Need Help Here
      );
// create a new instance of WP_Query
$challenge_query = new WP_Query( $challenge_args );
?>
<?php if ( $challenge_query->have_posts() ) : while ($challenge_query->have_posts() ) : $challenge_query->the_post(); // run the loop ?>

【问题讨论】:

    标签: wordpress loops variables


    【解决方案1】:

    通过自定义分类术语获取自定义帖子:

    <?php
       $terms = get_terms('sectortype');
       $challenge_args = array(
       'post_type' => 'challenge',
       'publish_status' => 'published',
       'posts_per_page' => -1,
       'tax_query' => array(
          array(
            'taxonomy' => 'sectortype',
            'field'    => 'slug',
            'terms'    => $terms[0], //whichever term you want to select
          ),
        ),
    );
    // create a new instance of WP_Query
    $challenge_query = new WP_Query( $challenge_args );
    ?>
    <?php if ( $challenge_query->have_posts() ) : while ($challenge_query->have_posts() ) : $challenge_query->the_post(); // run the loop ?>
    

    分页显示 如您在评论中提到的那样,要在单独的页面中显示帖子,您必须执行以下操作:

    创建单独的页面链接::(在页面上用作导航项)

    <?php $categories = get_terms('sectortype');?>
    <ul>
        <?php foreach( $categories as $key => $c ):?>
          <?php $cat_link = get_term_link( $c->term_id );?>
          <?php $term_title= single_term_title('', false);?>
          <li class="<?php echo ($c->name == $term_title )?'active':'';?>"><a href="<?php echo $cat_link;?>"><?php echo $c->name;?></a></li>
          <?php endforeach;?>
     </ul>
    

    在主题目录(实际上是分类术语的存档模板)中创建一个文件名为“taxonomy-sectortype.php”的文件。

    在该模板上,从通常的循环中获取帖子而不使用任何查询,您将获得相应的帖子。

    【讨论】:

    • 对于分类,我有商业、高等教育等类别。因此,通过您提供的编辑(谢谢),应该在正确的页面上显示来自给定分类的帖子,而无需创建每个人都有一个新页面?
    猜你喜欢
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多