categories - Custom Wordpress theme not displaying posts from category

I am creating a custom Wordpress theme and as far as I can remember Wordpress is supposed to do the heavy lifting for fi

I am creating a custom Wordpress theme and as far as I can remember Wordpress is supposed to do the heavy lifting for filtering of post types by their category, author, etc. on it's own. This is just an altered query from my index.php specifically for category types.

Another question... do I even need an archive.php? I thought Wordpress was supposed to do all the heavy lifting as far as filtering by categories, tags, authors, etc. My index.php works great, but when I tried to go to a specific category it was showing all of my posts and not just the selected category so I tried implementing an archive.php

EDIT 1: I should have mentioned I'm not using the main loops because I'm using custom post types

archive.php

<?php
/**
 * @author    Mark Abel
 * @package   client-name/archive
 * @version   1.0
 */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
get_header(); ?>

<?php get_template_part( 'partials/partial', 'sidenav' );?>

<section class="masonry__container" id="masonry__container">
  <div class="masonry" id="masonry">
    <?php
      $category = get_category( get_query_var( 'cat' ) );
      $cat_id = $category->cat_ID;
      $loop = new WP_Query( array( 'post_type' => 'project', 'cat' => $cat_id ) );
      if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
          <?php $project_for = get_post_meta($post->ID, "project_for", true); ?>
          <article class="masonry__tile" data-aos="zoom-in">
            <img src=<?php the_post_thumbnail_url(); ?> alt="an image" />
              <div class="masonry__overlay"><div class="masonry__title">
                <?php echo get_the_title() ?>
              </div>
              <div class="masonry__project">
                <?php echo $project_for ?>
              </div>
              <span></span>
              <div class="masonry__cta"><a href="<?php echo get_post_permalink(); ?>">Click to view</a></div>
              <a class="masonry__video" href="">Play video</a>
            </div>
          </article>
        <?php endwhile;
      endif;
      wp_reset_postdata();
    ?>
  </div>
</section>


</body>

<!-- Footer --> 
<?php get_footer(); ?>

</html>

partial imported by index.php

<?php
/**
 *  @author    Mark Abel
 *  @package   client-name/partials/partial-masonry
 *  @version   1.0
 */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
?>

<section class="masonry__container" id="masonry__container">
  <div class="masonry" id="masonry">
    <?php
      $loop = new WP_Query( array( 'post_type' => 'project' ) );
      if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
          <?php $project_for = get_post_meta($post->ID, "project_for", true); ?>
          <article class="masonry__tile" data-aos="zoom-in">
            <img src=<?php the_post_thumbnail_url(); ?> alt="an image" />
              <div class="masonry__overlay"><div class="masonry__title">
                <?php echo get_the_title() ?>
              </div>
              <div class="masonry__project">
                <?php echo $project_for ?>
              </div>
              <span></span>
              <div class="masonry__cta"><a href="<?php echo get_post_permalink(); ?>">Click to view</a></div>
              <a class="masonry__video" href="">Play video</a>
            </div>
          </article>
        <?php endwhile;
      endif;
      wp_reset_postdata();
    ?>
  </div>
</section>

I am creating a custom Wordpress theme and as far as I can remember Wordpress is supposed to do the heavy lifting for filtering of post types by their category, author, etc. on it's own. This is just an altered query from my index.php specifically for category types.

Another question... do I even need an archive.php? I thought Wordpress was supposed to do all the heavy lifting as far as filtering by categories, tags, authors, etc. My index.php works great, but when I tried to go to a specific category it was showing all of my posts and not just the selected category so I tried implementing an archive.php

EDIT 1: I should have mentioned I'm not using the main loops because I'm using custom post types

archive.php

<?php
/**
 * @author    Mark Abel
 * @package   client-name/archive
 * @version   1.0
 */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
get_header(); ?>

<?php get_template_part( 'partials/partial', 'sidenav' );?>

<section class="masonry__container" id="masonry__container">
  <div class="masonry" id="masonry">
    <?php
      $category = get_category( get_query_var( 'cat' ) );
      $cat_id = $category->cat_ID;
      $loop = new WP_Query( array( 'post_type' => 'project', 'cat' => $cat_id ) );
      if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
          <?php $project_for = get_post_meta($post->ID, "project_for", true); ?>
          <article class="masonry__tile" data-aos="zoom-in">
            <img src=<?php the_post_thumbnail_url(); ?> alt="an image" />
              <div class="masonry__overlay"><div class="masonry__title">
                <?php echo get_the_title() ?>
              </div>
              <div class="masonry__project">
                <?php echo $project_for ?>
              </div>
              <span></span>
              <div class="masonry__cta"><a href="<?php echo get_post_permalink(); ?>">Click to view</a></div>
              <a class="masonry__video" href="https://player.vimeo/video/124254859">Play video</a>
            </div>
          </article>
        <?php endwhile;
      endif;
      wp_reset_postdata();
    ?>
  </div>
</section>


</body>

<!-- Footer --> 
<?php get_footer(); ?>

</html>

partial imported by index.php

<?php
/**
 *  @author    Mark Abel
 *  @package   client-name/partials/partial-masonry
 *  @version   1.0
 */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
?>

<section class="masonry__container" id="masonry__container">
  <div class="masonry" id="masonry">
    <?php
      $loop = new WP_Query( array( 'post_type' => 'project' ) );
      if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
          <?php $project_for = get_post_meta($post->ID, "project_for", true); ?>
          <article class="masonry__tile" data-aos="zoom-in">
            <img src=<?php the_post_thumbnail_url(); ?> alt="an image" />
              <div class="masonry__overlay"><div class="masonry__title">
                <?php echo get_the_title() ?>
              </div>
              <div class="masonry__project">
                <?php echo $project_for ?>
              </div>
              <span></span>
              <div class="masonry__cta"><a href="<?php echo get_post_permalink(); ?>">Click to view</a></div>
              <a class="masonry__video" href="https://player.vimeo/video/124254859">Play video</a>
            </div>
          </article>
        <?php endwhile;
      endif;
      wp_reset_postdata();
    ?>
  </div>
</section>
Share Improve this question edited Sep 23, 2019 at 1:27 Mark asked Sep 23, 2019 at 0:50 MarkMark 1014 bronze badges 1
  • Just a wild guess.. Have you set the has_archive argument to true while registering the CPT? It's false by default. – Abhik Commented Sep 23, 2019 at 3:13
Add a comment  | 

1 Answer 1

Reset to default 0

I thought Wordpress was supposed to do all the heavy lifting as far as filtering by categories, tags, authors, etc.

It does. Which is why you shouldn't have done this:

  $category = get_category( get_query_var( 'cat' ) );
  $cat_id = $category->cat_ID;
  $loop = new WP_Query( array( 'cat' => $cat_id ) );

In the main template files you shouldn't be doing your own queries. Use the main loop:

<?php if ( have_posts() ) : ?>
      <?php while ( have_posts() ) : the_post(); ?>
            <!-- etc. -->
      <?php endwhile; ?>
<?Php endif; ?>

The purpose if archive.php is to allow you to have a different template for archives. Not to manually query different posts. The only difference should be the HTML markup around the content.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745138687a4613326.html

相关推荐

  • categories - Custom Wordpress theme not displaying posts from category

    I am creating a custom Wordpress theme and as far as I can remember Wordpress is supposed to do the heavy lifting for fi

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信