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.com/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.com/video/124254859">Play video</a>
</div>
</article>
<?php endwhile;
endif;
wp_reset_postdata();
?>
</div>
</section>
has_archive
argument totrue
while registering the CPT? It's false by default.