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
|
1 Answer
Reset to default 0I 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
has_archive
argument totrue
while registering the CPT? It's false by default. – Abhik Commented Sep 23, 2019 at 3:13