在类别页面上显示两个自定义帖子类型及其帖子

时间:2019-12-12 作者:Hakimuddin Saifee

我有两种自定义帖子类型“Article”&;“新闻”,它们都使用相同的分类,如[审计、收入、税收]。我想在打开审计类别页面时,帖子显示如下:enter image description here

我已经在archieve上尝试过的代码。php但不工作:

<?php

    $cats = get_the_category();
    $args = array(
        \'post_type\' => \'articles\',
        \'post__not_in\' => array( get_the_ID() ),
        \'posts_per_page\' => 5,
        \'cat\'     => $cats[0]->term_id,
        \'meta_query\' => array(
                    array(
                    \'key\' => \'recommended_article\',
                    \'value\' => \'1\',
                    \'compare\' => \'==\'
                        )
                    )
    );
    $query = new WP_Query( $args );

?>  

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

<!--HTML-->

<?php endwhile; endif; wp_reset_postdata(); ?>

4 个回复
最合适的回答,由SO网友:Hakimuddin Saifee 整理而成

完成了,非常感谢所有回答的人!

<?php
/*
 * dglib_breadcrumbs_section_template - hook
 *
 * @hooked - dglib_breadcrumbs_section_callback - 10
 *
 * @since 1.0.6
 */

do_action( \'dglib_breadcrumbs_section_template\' );
?>
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">


<header class="page-header">
                <?php
                    the_archive_title( \'<h1 class="page-title">\', \'</h1>\' );
                    the_archive_description( \'<div class="archive-description">\', \'</div>\' );

                ?>
            </header>
<div class="row">
                <?php

$category = get_queried_object();

        // news
        $args = array(
    \'post_type\' => \'news\',
    \'posts_per_page\' => 5,
    \'cat\'     =>  $category->term_id,
);

$the_query = new WP_Query( $args ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
            <!-- .page-header -->

<div class="col-md-6 col-sm-6">
            <div class="panel panel-default"> 
            <div class="panel-heading">
        <h3 class="sub_header">News</h3>
        </div>
        <ul style="padding-left: 25px; padding-top:15px;list-style: disc; line-height:23px">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title( \'<li class="entry-title"><a href="\' . esc_url( get_permalink() ) . \'" rel="bookmark">\', \'</a></li>\' ); ?>

<?php wp_reset_postdata(); ?>
<?php endwhile;  ?>
</ul>
</div>
</div>

<?php else: ?>
<p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
<?php endif;  ?>
<?php



        // articles
        $args1 = array(
    \'post_type\' => \'articles\',
    \'posts_per_page\' => 5,
    \'cat\'     =>  $category->term_id,
);

$the_query1 = new WP_Query( $args1 ); 
?>
<?php if ( $the_query1->have_posts() ) : ?>
            <!-- .page-header -->

<div class="col-md-6 col-sm-6">
            <div class="panel panel-default"> 
            <div class="panel-heading">
    <h3 class="sub_header">Articles</h3>
        </div>
        <ul style="padding-left: 25px; padding-top:15px;list-style: disc; line-height:23px">
<?php while ( $the_query1->have_posts() ) : $the_query1->the_post(); ?>
<?php the_title( \'<li class="entry-title"><a href="\' . esc_url( get_permalink() ) . \'" rel="bookmark">\', \'</a></li>\' ); ?>

<?php wp_reset_postdata(); ?>
<?php endwhile;  ?>
</ul>
</div>
</div>

<?php else: ?>
<p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
<?php endif;  ?>
</div>
        </main><!-- #main -->
    </div><!-- #primary -->
<?php
get_sidebar();
get_footer();

SO网友:Heba Fareed

首先,我建议在类别上进行此更改。php文件,作为归档文件。根据Wordpress Theme Hierarchy 文件系统。

尝试添加以下两个查询:

$cats = get_the_category();
    $cat_id = $cats[0]->term_id;


        // news
        $args = array(
    \'post_type\' => \'news\',
    \'posts_per_page\' => 5,
    \'cat\'     => $cat_id,
);
$query = new WP_Query($args);

if($query->have_posts() ) : 
    while( $query->have_posts() ) :
        $query->the_post();
    endwhile; 
endif; 

wp_reset_postdata(); 

    // articles
$args = array(
    \'post_type\' => \'articles\',
    \'posts_per_page\' => 5,
    \'cat\'     => $cat_id,
);
$query = new WP_Query($args);

if($query->have_posts() ) : 
    while( $query->have_posts() ) :
        $query->the_post();
    endwhile; 
endif; 

wp_reset_postdata(); 

SO网友:Chetan Vaghela

创造taxonomy-categories.php 或者在你的archive.php 添加以下代码以获取帖子articlenews 当前类别的职位类型。我假设这两种帖子类型的分类是categories. 改变categories 使用您的分类法和post_type 与您的新闻和文章类型从下面的代码。

 # Article 
 $articleargs = array(
        \'post_type\' => \'article\',
        \'posts_per_page\' => 5,
        \'tax_query\' => array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\' => \'categories\',
                \'field\'    => \'name\',
                \'terms\'    => get_queried_object()
            )
        ),
    );
    $articlequery = new WP_Query( $articleargs );

     if( $articlequery->have_posts() ) : 
        echo "<h2>Articles</h2>";
        echo "<ul>";
        while( $articlequery->have_posts() ) : $articlequery->the_post();   
            echo "<li>".get_the_title()."</li>";
         endwhile; 
         echo "</ul>";
    endif; 
    wp_reset_postdata(); 

    # news
    $newsargs = array(
        \'post_type\' => \'news\',
        \'posts_per_page\' => 5,
        \'tax_query\' => array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\' => \'categories\',
                \'field\'    => \'name\',
                \'terms\'    => get_queried_object()
            )
        ),
    );
    $newsquery = new WP_Query( $newsargs );

     if( $newsquery->have_posts() ) : 
        echo "<h2>News</h2>";
        echo "<ul>";
        while( $newsquery->have_posts() ) : $newsquery->the_post();   
            echo "<li>".get_the_title()."</li>";
         endwhile; 
         echo "</ul>";
    endif; 
    wp_reset_postdata(); 

SO网友:nmr

这个get_the_category() 函数从循环中检索分配给第一篇文章的类别,这不是您的意思。我想audit, income, tax, 等是内置类别,而不是自定义分类法(在这种情况下,需要替换cat 的参数WP_Querytax_query).

创造category.php 如果文件不存在,请通过复制archive.php. 这样,模板将仅在类别页面上使用。

的IDcurrently viewed 您可以使用的类别get_queried_object_id(). 您不必在category.php 文件,它将始终是正确的。有了类别ID,就可以将它们插入查询参数中,并且只接收属于它的帖子。

如果您打算仅将模板应用于选定类别(审计、收入、税务),而不是全部,请使用{$type}_template 滤器

//
// ge ID of the  current category
$cat_id = get_queried_object_id();

//
// get \'news\' posts from current category 
$args = array(
    \'post_type\' => \'news\',
    \'posts_per_page\' => 10,
    \'cat\'       => (int)$cat_id,  // <==
    //\'meta_query\' => array(
    //    array(
    //        \'key\' => \'recommended_article\',
    //        \'value\' => \'1\',
    //        \'compare\' => \'=\',    // <== 
    //    )
    )
);
$query = new WP_Query( $args );
//
// display posts
// 
wp_reset_postdata();


//
// get \'articles\' posts from current category 
$args = array(
    \'post_type\' => \'articles\',   // <==
    \'posts_per_page\' => 10,
    \'cat\'       => (int)$cat_id,
    //\'meta_query\' => array(
    //    array(
    //        \'key\' => \'recommended_article\',
    //        \'value\' => \'1\',
    //        \'compare\' => \'=\',
    //    )
    )
);
$query = new WP_Query( $args );
//
// ...
// 
wp_reset_postdata();

相关推荐

WP_Query by MAX post_id?

我正在尝试使用wp\\U查询类获取作者插入的最新post\\U id。我必须在我使用的查询的参数中使用MAX(wp\\u posts.ID)。这是使用wp\\u查询类实现的还是必须使用wpdb::get\\u results()实现的?感谢您的帮助。