Genesis makes it super simple to use Custom Post Types and some themes like Executive Pro even include a really nice portfolio/grid style archive page layout.
But what if you want to use the archive-portfolio.php grid style layout on the front page or category archives?
There’s 3 steps you need to take in order to do this and pre_get_posts is generally the best function to use when modifying any query.
Firstly, copy your archive-portfolio.php file and rename it to front-page.php or category.php if you want to use the portfolio grid layout on all your category archive pages.
Secondly, you need to remove the standard ‘post’ type from the query and replace it with the portfolio custom post type. This removes all posts from the loop so only your single CPT’s display.
Add this code to the end of your child themes functions.php file:
add_action( 'pre_get_posts', 'display_portfolio_archive_front_page' );
function display_portfolio_archive_front_page( $query ) {
if ( is_front_page() && $query->is_main_query() && !is_admin() ) {
$query->set( 'post_type', array( 'portfolio' ) );
$query->set( 'posts_per_page', '999' );
}
}
The above code will display all portfolio items on the front page of your site.
If you want to use the archive-portfolio.php template on all your category archives, use this code instead:
add_action( 'pre_get_posts', 'display_portfolio_category_archive_pages' );
function display_portfolio_category_archive_pages( $query ) {
if ( is_category() && $query->is_main_query() && !is_admin() ) {
$query->set( 'post_type', array( 'post' ) );
}
}
The last step involves copying and changing the class for some of the CSS code which makes your custom post type featured images display in 3 columns:
.home .entry {
float: left;
margin-bottom: 60px;
margin-bottom: 6rem;
width: 33.333333333333%;
}
.home .entry:nth-of-type(3n) {
float: right;
padding-left: 30px;
padding-left: 3rem;
}
.home .entry:nth-of-type(3n+1) {
clear: left;
padding-right: 30px;
padding-right: 3rem;
}
The above CSS works from your home or front page.
If you want to use the portfolio grid layout on all category archives, simply change the class from .home to .category.
Other Options & Related Solutions
- 4 Column Category Archive Recipes – Featured Image & Title Only
- Archive Page Grid Style Columns Using CSS
- Entry Title & Featured Image Grid Archive Page Template
- Display Home Page Posts In Grid Style Layout Using Portfolio CPT Template
- Add Custom Post Type (CPT) Pages To Posts Page
- Add Responsive Portfolio Page To Lifestyle Pro
Leave a Reply
You must be logged in to post a comment.