在存档页面上显示特定类别的帖子

发布于 2025-01-17 23:57:51 字数 1216 浏览 2 评论 0原文

我正在使用 ACF 和 CPT UI 添加我的项目。每个项目帖子都分配有一个自定义类别。

我使用下面的代码来显示存档页面中的所有项目...

<?php $loop = new WP_Query( array( 'post_type' => 'projets', 'paged' => $paged ) );
      if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>

          <div class="col-md-4 col-sm-6 p-4">
            <div class="box">
             <a href="<?php the_permalink(); ?>"><h3> <?php the_title(); ?></h3></a>
            </div>
          </div> 

        <?php endwhile;
        if (  $loop->max_num_pages > 1 ) : ?>
          <div id="nav-below" class="navigation">
            <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Previous', 'domain' ) ); ?></div>
            <div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">&rarr;</span>', 'domain' ) ); ?></div>
          </div>

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

该代码可以很好地获取所有类别的所有帖子,但是当我转到项目/类别1时,它仍然显示所有项目。

如何修改它以仅显示特定类别的项目?

I am using ACF and CPT UI to add my projects. Each projects post have a custom category assigned to it.

I am using this code below to display all projects in the archive page…

<?php $loop = new WP_Query( array( 'post_type' => 'projets', 'paged' => $paged ) );
      if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>

          <div class="col-md-4 col-sm-6 p-4">
            <div class="box">
             <a href="<?php the_permalink(); ?>"><h3> <?php the_title(); ?></h3></a>
            </div>
          </div> 

        <?php endwhile;
        if (  $loop->max_num_pages > 1 ) : ?>
          <div id="nav-below" class="navigation">
            <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Previous', 'domain' ) ); ?></div>
            <div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">→</span>', 'domain' ) ); ?></div>
          </div>

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

The code works well to get all the posts from all category but when I go to projects/category1, it still show all projects.

How can I modify this to show only the projects from specific category ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2025-01-24 23:57:51

这是wp_query

https://developer.wordpress.orgg/classes/ wp_query/

的特定类别的帖子

这将仅显示您感兴趣的类别

<?php $loop = new WP_Query( 
    array( 
        'post_type' => 'projets',
        'paged' => $paged,
        'category_name' => 'my_category_slug'
    ));

      if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>

          <div class="col-md-4 col-sm-6 p-4">
            <div class="box">
             <a href="<?php the_permalink(); ?>"><h3> <?php the_title(); ?></h3></a>
            </div>
          </div> 

        <?php endwhile;
        if (  $loop->max_num_pages > 1 ) : ?>
          <div id="nav-below" class="navigation">
            <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Previous', 'domain' ) ); ?></div>
            <div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">→</span>', 'domain' ) ); ?></div>
          </div>

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

替换'my_category_slug'。通常不需要这是您在创建新帖子类型后刷新永久链接的。 IE。转到设置永久链接,然后单击“保存”。

标准循环应适用于自定义帖子类型。

''

<?php if(have_posts()): ?>

  <?php while(have_posts()): the_post() ?>

    <h1><?php the_title(); ?></h1>

    <div><?php the_content(); ?></div>

  <?php endwhile; ?>

<?php endif; ?>

'''

这将是您的标准页面布局以显示内容,然后您将使用 http://yoursite.com/projets/

尽管上面的循环应该已经包含在主题中,因此除非从头开始构建主题,否则几乎不需要重写它。

this is a good reference for WP_Query

https://developer.wordpress.org/reference/classes/wp_query/

This will show only post from a specific category

replace 'my_category_slug' with the category you are interested in.

<?php $loop = new WP_Query( 
    array( 
        'post_type' => 'projets',
        'paged' => $paged,
        'category_name' => 'my_category_slug'
    ));

      if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>

          <div class="col-md-4 col-sm-6 p-4">
            <div class="box">
             <a href="<?php the_permalink(); ?>"><h3> <?php the_title(); ?></h3></a>
            </div>
          </div> 

        <?php endwhile;
        if (  $loop->max_num_pages > 1 ) : ?>
          <div id="nav-below" class="navigation">
            <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Previous', 'domain' ) ); ?></div>
            <div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">→</span>', 'domain' ) ); ?></div>
          </div>

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

This though is usually not required have you refreshed your permalinks after creating the new post type. ie. go to settings permalinks and click save.

The standard loop should work for custom post types.

'''

<?php if(have_posts()): ?>

  <?php while(have_posts()): the_post() ?>

    <h1><?php the_title(); ?></h1>

    <div><?php the_content(); ?></div>

  <?php endwhile; ?>

<?php endif; ?>

'''

This would be your standard page layout to show the content then you would navigate to the the custom post type using http://yoursite.com/projets/

Though the loop above should already be included in your theme so you should rarely need to rewrite it unless building a theme from scratch.

花开雨落又逢春i 2025-01-24 23:57:51

您是否尝试过WP默认循环,没有任何自定义WP_QUERY IE

<?php if ( have_posts() ) : ?>

because WP automatically determines which category projects post you're trying to view and show the projects post accordingly. 

Right now you're mentioning the "'post_type' => 'projets'" so due to that it's showing all the projects.

so please use WP default loop then view the category i.e.
sitename.com/CPT_taxonomy/category_name

then it'll only show the project related to that category only. 

have you tried WP default loop without any custom WP_Query i.e.

<?php if ( have_posts() ) : ?>

because WP automatically determines which category projects post you're trying to view and show the projects post accordingly. 

Right now you're mentioning the "'post_type' => 'projets'" so due to that it's showing all the projects.

so please use WP default loop then view the category i.e.
sitename.com/CPT_taxonomy/category_name

then it'll only show the project related to that category only. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文