个性化阅读
专注于IT技术分析

结合并显示博客文章结果和自定义分类法

我创建了一个自定义博客页面, 可以在其中列出所有帖子。我有一个分类分类为新闻的帖子类型, 我想在此分类下显示可用的帖子以及同一页面上的普通帖子。

这是我用来显示帖子的while循环:

while($ wp_query-> have_posts()):$ wp_query-> the_post(); ?>

    <div class="columns large-12" style="margin-top:10px; border:1px solid #8080804a; padding-left: 0px;" id="2663539596">
    <div class="columns large-6" id="imagesection" style="padding-left: 0px;">
        <p style="background-color: white; padding: 23px; color: black; position: absolute; z-index: 2; font-family: 'Roboto Condensed', Arial, sans-serif; font-weight: bold; margin-top: 75px;"><?php echo get_the_date('jS \of F Y' ); ?> </p>
     <?php the_post_thumbnail('large'); ?>

    </div>

    <div class="columns large-6" style="padding-left:20px; padding-right:20px;">
        <p id="postcat" style="color:white; margin-top:100px; font-family: 'Roboto Condensed', Arial, sans-serif;"> <?php $id = $post->ID; echo esc_html(get_the_category( $id )[0]->name); ?> </p>
    <h2 id="posttitle" style="margin-top:70px;"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" style="color: white; font-family: 'Roboto Condensed', Arial, sans-serif; font-weight:bold;"><?php the_title(); ?></a></h2>

” title =”阅读更多” style =” color:white;字体家族:” Roboto Condensed”, Arial, 无衬线; margin-left:5px; font-weight:bold”>了解更多

所以有没有什么方法可以将分类法结果与此结合起来。


#1


你需要的是自定义查询。你可以使用WP_Query实现。

根据你的情况, 你需要使用两种自定义帖子类型(POST和NEWS)创建查询。这是示例代码:

$custom_taxonomy = 'your_taxonomy_slug'; // UPDATE to your custom taxonomy slug
$cusom_taxonomy_terms = array( 'term_slug' ); // UPDATE to your custom taxonomy terms
$cpt_slug = 'news'; // UPDATE to your custom post type slug
// set the args for the Query
$args = array(
'post_type' => ['post', 'news'], 'tax_query' => [
    [
        'taxonomy' => $custom_taxonomy, 'field'    => 'slug', 'terms'    => $custom_taxonomy_terms, ]
 ]
);

// Create the query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
  // Loop start
  while ( $the_query->have_posts() ) {
    $the_query->the_post();
    // UPDATE pasting the loop code below
  }
  // End loop
} else {
 // No posts found
}

/* Restore original Post Data */
wp_reset_postdata();
赞(0)
未经允许不得转载:srcmini » 结合并显示博客文章结果和自定义分类法

评论 抢沙发

评论前必须登录!