admin管理员组文章数量:1426855
I have this page on my website -
It's a custom taxonomy template for a custom post type called Directory Entries with a taxonomy called directory_entry_type which is what's being pulled in here.
At the moment it's just pulling in all the posts under the category "Community Directory" - This category has about 6 children categories that i'd like to pull through on this page but at the moment it's just pulling in all of the posts under community directory.
How can I do this?
This is where I've got to so far -
<div id="content" class="col-md-8">
<?php
$queried_object = get_queried_object();
$term_query = $queried_object->term_id;
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
query_posts( array(
'posts_per_page' => -1, // you may edit this number
'orderby' => 'title',
'order' => 'ASC',
'child_of' => $current_term->term_id,
'hierarchical' => true,
'depth' => 1,
'tax_query' => array(
array(
'field' => 'term_id',
'terms' => $term_query,
'taxonomy' => $current_term->taxonomy,
)
),
'meta_query' => array(
array(
'key' => 'is_member',
'value' => '1',
'compare' => 'LIKE'
)
)
)
);
?>
<div class="grid-vw">
<ul>
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink();?>">
<figure class="effect-goliath">
<?php
$image = get_field('thumbnail');
$size = 'dir-tile'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size );
}
?>
<figcaption>
<h2><?php the_title();?> ></h2>
</figcaption>
</figure>
</a>
</li>
<?php endwhile;
wp_reset_query();
?> </ul>
</div>
Thanks for any help in advance!
EDIT
So I've solved this with this
<div class="grid-vw">
<ul>
<?php
// List posts by the terms for a custom taxonomy of any post type
$post_type = 'directory_entry';
$tax = 'directory_entry_type';
$tax_args = array(
'order' => 'ASC',
'parent' => 289
);
// get all the first level terms only
$tax_terms = get_terms( $tax, $tax_args );
if ($tax_terms) {
foreach ($tax_terms as $tax_term) { // foreach first level term
// print the parent heading
?>
<li>
<a href="<?php echo get_permalink( $tax_term->ID ); ?>">
<figure class="effect-goliath">
<?php
$image = get_field('thumbnail');
$size = 'dir-tile'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size );
}
?>
<figcaption>
<h2><?php echo $tax_term->name; ?> ></h2>
</figcaption>
</figure>
</a>
</li>
<?php wp_reset_query();
}
}
?>
</ul>
That's pulling in exactly what i want, but now the permalinks aren't working, could anyone tell me what I'm doing wrong there?
I have this page on my website -
It's a custom taxonomy template for a custom post type called Directory Entries with a taxonomy called directory_entry_type which is what's being pulled in here.
At the moment it's just pulling in all the posts under the category "Community Directory" - This category has about 6 children categories that i'd like to pull through on this page but at the moment it's just pulling in all of the posts under community directory.
How can I do this?
This is where I've got to so far -
<div id="content" class="col-md-8">
<?php
$queried_object = get_queried_object();
$term_query = $queried_object->term_id;
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
query_posts( array(
'posts_per_page' => -1, // you may edit this number
'orderby' => 'title',
'order' => 'ASC',
'child_of' => $current_term->term_id,
'hierarchical' => true,
'depth' => 1,
'tax_query' => array(
array(
'field' => 'term_id',
'terms' => $term_query,
'taxonomy' => $current_term->taxonomy,
)
),
'meta_query' => array(
array(
'key' => 'is_member',
'value' => '1',
'compare' => 'LIKE'
)
)
)
);
?>
<div class="grid-vw">
<ul>
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink();?>">
<figure class="effect-goliath">
<?php
$image = get_field('thumbnail');
$size = 'dir-tile'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size );
}
?>
<figcaption>
<h2><?php the_title();?> ></h2>
</figcaption>
</figure>
</a>
</li>
<?php endwhile;
wp_reset_query();
?> </ul>
</div>
Thanks for any help in advance!
EDIT
So I've solved this with this
<div class="grid-vw">
<ul>
<?php
// List posts by the terms for a custom taxonomy of any post type
$post_type = 'directory_entry';
$tax = 'directory_entry_type';
$tax_args = array(
'order' => 'ASC',
'parent' => 289
);
// get all the first level terms only
$tax_terms = get_terms( $tax, $tax_args );
if ($tax_terms) {
foreach ($tax_terms as $tax_term) { // foreach first level term
// print the parent heading
?>
<li>
<a href="<?php echo get_permalink( $tax_term->ID ); ?>">
<figure class="effect-goliath">
<?php
$image = get_field('thumbnail');
$size = 'dir-tile'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size );
}
?>
<figcaption>
<h2><?php echo $tax_term->name; ?> ></h2>
</figcaption>
</figure>
</a>
</li>
<?php wp_reset_query();
}
}
?>
</ul>
That's pulling in exactly what i want, but now the permalinks aren't working, could anyone tell me what I'm doing wrong there?
Share Improve this question edited Dec 11, 2017 at 12:00 Lucy Brown asked Dec 8, 2017 at 14:10 Lucy BrownLucy Brown 1491 gold badge3 silver badges11 bronze badges2 Answers
Reset to default 1To clarify, is your query only pulling posts tagged as the parent category and nothing tagged as a child category? And are you saying you'd like to have everything on this page?
You might try changing child_of
to cat
and get rid of depth
.
But TBH you may want to try a different approach at querying; this question/answer suggests that query_posts()
is not the best method: When should you use WP_Query vs query_posts() vs get_posts()?
Another approach would be to query for the IDs of the child categories of this parent and then loop through them with individual post queries - if you'd want them in six sections.
I solved this using this
<div class="grid-vw">
<ul>
<?php
// List posts by the terms for a custom taxonomy of any post type
$post_type = 'directory_entry';
$tax = 'directory_entry_type';
$tax_args = array(
'order' => 'ASC',
'parent' => 289
);
// get all the first level terms only
$tax_terms = get_terms( $tax, $tax_args );
if ($tax_terms) {
foreach ($tax_terms as $tax_term) { // foreach first level term
// print the parent heading
?>
<li>
<?php echo '<a href="'.get_term_link($tax_term).'">';
?>
<figure class="effect-goliath">
<?php
$image = get_field('thumbnail');
$size = 'dir-tile'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size );
}
?>
<figcaption>
<h2><?php echo $tax_term->name; ?> ></h2>
</figcaption>
</figure>
</a>
</li>
<?php wp_reset_query();
}
}
?>
</ul>
本文标签: Show categories of custom post type and not all posts
版权声明:本文标题:Show categories of custom post type and not all posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745484836a2660335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论