Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 在页面模板中显示特定分类法(类别)_Wordpress_Custom Post Type_Custom Taxonomy - Fatal编程技术网

Wordpress 在页面模板中显示特定分类法(类别)

Wordpress 在页面模板中显示特定分类法(类别),wordpress,custom-post-type,custom-taxonomy,Wordpress,Custom Post Type,Custom Taxonomy,我有一个名为Short Courses的自定义帖子类型,我在其中添加了一个名为Course Types的自定义分类法,它使我能够对课程进行分类 我正在尝试创建一个页面模板,该模板将显示具有单个分类类别的所有课程。在本例中,我想显示所有已分配分类类别Adobe的课程。。。我尝试了一些不同的方法,但以下是我的最新代码: <?php // get the custom post type's taxonomy terms $custom_taxterms = wp_get_object_t

我有一个名为Short Courses的自定义帖子类型,我在其中添加了一个名为Course Types的自定义分类法,它使我能够对课程进行分类

我正在尝试创建一个页面模板,该模板将显示具有单个分类类别的所有课程。在本例中,我想显示所有已分配分类类别Adobe的课程。。。我尝试了一些不同的方法,但以下是我的最新代码:

<?php 

// get the custom post type's taxonomy terms

$custom_taxterms = wp_get_object_terms( $post->ID, 'adobe', array('fields' => 'ids') );
// arguments
$args = array(
'post_type' => 'short_courses',
'post_status' => 'publish',
'posts_per_page' => 20, // you may edit this number
'orderby' => 'rand',
'tax_query' => array(
    array(
        'taxonomy' => 'adobe',
        'field' => 'id',
        'terms' => $custom_taxterms
    )
),
'post__not_in' => array ($post->ID),
);
$related_items = new WP_Query( $args );

// loop over query
if ($related_items->have_posts()) :
    echo '<ul>';
    while ( $related_items->have_posts() ) : $related_items->the_post();
    ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php
    endwhile;
    echo '</ul>';
endif;
// Reset Post Data
wp_reset_postdata();
?>
它不能解决任何问题

我也尝试过使用此代码:

<?php
    $args = array( 
      'post_type' => 'short_courses',
      'orderby' => 'title',
      'order' => 'ASC',
      'product_categories' => 'adobe'
    );
    $the_query = new WP_Query( $args );

?>

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


      <?php
        $thumbnail_id = get_post_thumbnail_id(); 
        $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
      ?>

      <p><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="<?php the_title();?> graphic"></a></p>
      <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>     


  <?php $products_count = $the_query->current_post + 1; ?>
  <?php if ( $products_count % 4 == 0): ?>

  <?php endif; ?>


  <?php endwhile; endif; ?>
但这段代码贯穿了所有的短期课程,不仅仅是Adobe课程


有人能告诉我哪里出了问题吗

你注意到这条线了吗

<?php if ( have_posts() ) : while
你能把它改成

<?php if ( $the_query->have_posts() ) : while
看看是否应用了过滤器?WordPress具有全局post功能,例如获取标题,这可能会干扰您的自定义查询对象,例如您正在使用的$theu查询

我还建议您在while之前删除if语句,以提高代码的可读性。如果查询没有任何帖子,while循环将不会在页面上生成任何输出,因此需要if语句

然后,整个代码变为

<?php
        $args = array( 
          'post_type' => 'short_courses',
          'orderby' => 'title',
          'order' => 'ASC',
          'product_categories' => 'adobe'
        );
        $the_query = new WP_Query( $args );

    ?>

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


          <?php
            $thumbnail_id = get_post_thumbnail_id(); 
            $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
          ?>

          <p><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="<?php the_title();?> graphic"></a></p>
          <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>     


      <?php $products_count = $the_query->current_post + 1; ?>
      <?php if ( $products_count % 4 == 0): ?>

      <?php endif; ?>


      <?php endwhile; ?>

请确保您在“product_categories”=>“adobe”上使用的分类名称与您在register_taxonomy功能上使用的名称相同,即确保您拥有register_taxonomy“product_categories”、array“short_courses”和$args;如果您还有其他关于产品类别的信息,请在查询中使用。希望这能有所帮助。

好的,我设法解决了这个问题——只是有点小错误。在上面的第二批代码中,表示产品类别需要更改为课程类型的部分!以下是新的工作守则:

<?php
        $args = array( 
          'post_type' => 'short_courses',
          'orderby' => 'title',
          'order' => 'ASC',
          'course_type' => 'adobe'
        );
        $the_query = new WP_Query( $args );

     ?>

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


          <?php
            $thumbnail_id = get_post_thumbnail_id(); 
            $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
          ?>

          <p><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="<?php the_title();?> graphic"></a></p>
          <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>     


      <?php $products_count = $the_query->current_post + 1; ?>
      <?php if ( $products_count % 4 == 0): ?>

      <?php endif; ?>


      <?php endwhile; endif; ?>

似乎没有任何效果,我担心@Nkole-我已经用这个添加更新了我的代码。。。