admin管理员组文章数量:1130349
I have a page called "Developments" and I want to list all the Project posts by the custom tax Project Type using the page title as the term.
This doesn't work:
<?php
$title_slug = sanitize_title(wp_title($sep = ''));
$the_query = new WP_Query( array(
'post_type' => 'project',
'tax_query' => array(
array (
'taxonomy' => 'project_type',
'field' => 'slug',
'terms' => $title_slug,
)
),
) );
while ( $the_query->have_posts() ) :
$the_query->the_post();
get_template_part( 'template-parts/content', 'post' );
endwhile;
wp_reset_postdata();
?>
It doesn't work with 'terms' => $title either (hence I tried to define a new variable $title_slug).
If I use 'terms' => 'Developments' it works.
Side question: why does $title_slug = sanitize_title(wp_title($sep = '')); print out the title to display? I am trying to define a variable not echo anything. Answered below
EDIT:
Changing to this format works:
...
$options = array(
'post_type' => 'project',
'tax_query' => array(
array (
'taxonomy' => 'project_theme',
'field' => 'slug',
'terms' => $title_slug,
)
),
);
$the_query = new WP_Query( $options );
...
But I still don't understand why the original is failing to list posts.
I have a page called "Developments" and I want to list all the Project posts by the custom tax Project Type using the page title as the term.
This doesn't work:
<?php
$title_slug = sanitize_title(wp_title($sep = ''));
$the_query = new WP_Query( array(
'post_type' => 'project',
'tax_query' => array(
array (
'taxonomy' => 'project_type',
'field' => 'slug',
'terms' => $title_slug,
)
),
) );
while ( $the_query->have_posts() ) :
$the_query->the_post();
get_template_part( 'template-parts/content', 'post' );
endwhile;
wp_reset_postdata();
?>
It doesn't work with 'terms' => $title either (hence I tried to define a new variable $title_slug).
If I use 'terms' => 'Developments' it works.
Side question: why does $title_slug = sanitize_title(wp_title($sep = '')); print out the title to display? I am trying to define a variable not echo anything. Answered below
EDIT:
Changing to this format works:
...
$options = array(
'post_type' => 'project',
'tax_query' => array(
array (
'taxonomy' => 'project_theme',
'field' => 'slug',
'terms' => $title_slug,
)
),
);
$the_query = new WP_Query( $options );
...
But I still don't understand why the original is failing to list posts.
本文标签: List custom post type by custom tax term as page title
版权声明:本文标题:List custom post type by custom tax term as page title 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749082239a2313227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论