繁体   English   中英

通过自定义分类法调用wordpress发布

[英]Calling wordpress post by custom taxonomy

我在WordPress中定义了一个自定义分类法,如下所示:

function content_selector() {
  register_taxonomy(
      'contentselector',
      'post',
      array(
              'label' => __( 'Content Selector' ),
              'rewrite' => array( 'slug' => 'cs' ),
              'hierarchical' => true,
      )
  );
}
add_action( 'init' , 'content_selector' );

它显示在新帖子中,并且可以分配值,实际上似乎可行。 但是当我使用以下功能通过该分类法调用帖子时,没有成功。

add_shortcode('rps', 'rpsf');
function rpsf() {
     $args =[ 'posts_per_page' => 1, array(

             'tax_query' => array(
                            array(
                                 'taxonomy' => 'contentselector',
                                 'field' => 'slug',
                                 'terms' => 'one-of-the-assigned-terms')
          ))];

  $query = new WP_Query( $args );

     if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
          ob_start();  ?>

              <div class="rpst">
              <a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span</a>
              </div>

      <?php endwhile; endif; wp_reset_postdata();
   return ob_get_clean();
}

在定义分类法或致电职位时,我是否犯了错误?

如果正确格式化代码,以及在使用的符号方面保持一致(例如[] vs array() ),则调试代码会更容易。

在“清理”您的$args定义之后,很明显看到它的结构不正确:

$args = array(
    'posts_per_page' => 1,
    array(
        'tax_query' => array(
            array(
                'taxonomy' => 'contentselector',
                'field' => 'slug',
                'terms' => 'one-of-the-assigned-terms'
            )
        )
    )
);

它看起来应该像这样:

$args = array(
    'posts_per_page' => 1,
    'tax_query' => array(
        array(
            'taxonomy' => 'contentselector',
            'field' => 'slug',
            'terms' => 'one-of-the-assigned-terms'
        )
    )
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM