admin管理员组文章数量:1290983
Have posts, want related categories to display under them.
Post example: Artist name.
Category example: posts with the Artist name.
My current version, arrived at based on a comment from @birgire, adds a WP_Query block under the single.php page template. It currently returns all the posts categorized to the artist, but returns them as full posts instead of the default post thumbnail above an excerpt.
<?php
$args = array(
'post_type' => 'pdsh_posts',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $post->post_name,
),
),
);
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
//the_excerpt();
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
?>
Wondering if this a good solution easily completed with css or am I missing some fundamental step?
This mockup shows what I'm going for:
Have posts, want related categories to display under them.
Post example: Artist name.
Category example: posts with the Artist name.
My current version, arrived at based on a comment from @birgire, adds a WP_Query block under the single.php page template. It currently returns all the posts categorized to the artist, but returns them as full posts instead of the default post thumbnail above an excerpt.
<?php
$args = array(
'post_type' => 'pdsh_posts',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $post->post_name,
),
),
);
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
//the_excerpt();
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
?>
Wondering if this a good solution easily completed with css or am I missing some fundamental step?
This mockup shows what I'm going for:
Share Improve this question edited Jun 6, 2021 at 1:15 markw asked Jun 4, 2021 at 18:47 markwmarkw 174 bronze badges 7- Maybe this is where the "Multiple Loops Example" sections of codex.wordpress/The_Loop might be used... – markw Commented Jun 4, 2021 at 19:36
- Concatenating the string "category-" with the title "c-c-beck" to populate this line... <?php query_posts( 'category_name=category-c-c-beck' ); ?> ? – markw Commented Jun 4, 2021 at 19:44
- Tried just <?php query_posts( 'category_name=category-c-c-beck&posts_per_page=10' ); ?> followed by the loop from archive.php but got "Nothing Found. It seems we can’t find what you’re looking for." returned. – markw Commented Jun 5, 2021 at 5:20
- Dont use query_posts because of its side effects. Look into WP_Query for secondary loops. Alternative approach is using js+rest api. – birgire Commented Jun 5, 2021 at 10:32
- Oh cool. This kinda worked... <?php $args = array( 'post_type' => 'pdsh_posts', 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => 'c-c-beck', ), ), ); $my_query = new WP_Query( $args ); while ( $my_query->have_posts() ) : $my_query->the_post(); get_template_part( 'template-parts/content', get_post_format() ); endwhile; ?> – markw Commented Jun 5, 2021 at 18:16
1 Answer
Reset to default 1Figured it out. A conditional in content.php in template-parts/ checked whether the content is_archive. I assume the functionality behind is_archive is whether the file was launched from archive.php. Since it wasn't it didn't fire.
Final step was redirecting to a custom content-post-type.php in template-parts that doesn't have the is_archive check. Seems like it all works right now. Thanks to birgire for the nudge.
So my final single.php goes:
<?php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', get_post_format() );
the_post_navigation();
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
<!--ADD AN ARCHIVE FOR php echo $post->post_name -->
<?php
$page_title = $wp_query->post->post_title;
$args = array(
'post_type' => 'pdsh_posts',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $page_title,
//'terms' => $post->post_name,//
),
),
);
$my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
//the_excerpt();
get_template_part( 'template-parts/content-pdsh', get_post_format() );
endwhile;
?>
Where "pdsh_posts" is a custom post type.
本文标签: Add archivecategory results to single post page
版权声明:本文标题:Add archivecategory results to single post page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741519569a2383099.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论