Community Forums › Forums › Archived Forums › Design Tips and Tricks › Custom archive loop for CPT
Tagged: archive, CPT, custom loop
- This topic has 5 replies, 2 voices, and was last updated 5 years, 11 months ago by photoaddictsa.
-
AuthorPosts
-
December 21, 2018 at 12:04 am #225185photoaddictsaMember
Hi all,
I know this question has been answered before but for the life of me I can't find it anymore.
I created a few custom post types and custom fields using ACF, now I know how to display the info, what I'm struggling with is creating a custom archive, I don't want to output anything on a single post here, everything is just displayed on the archive page (I can't think of any other way to do this)
What I want to know is how do I format the following code correctly in a loop?
http://photoaddict.co.za/cjpartners/trucks/<?php /** * Template for <code>trucks</code> CPT archive page. */ add_filter( 'genesis_pre_get_option_site_layout','__genesis_return_full_width_content' ); remove_action('genesis_before_post_content','genesis_post_info'); remove_action('genesis_after_post_content','genesis_post_meta'); // Custom loop <h2><?php the_title(); ?></h2> <?php the_field('registration_number'); ?> <?php $posts = get_field('truck_driver'); if( $posts ): ?> <ul> <li> <?php the_field('full_name'); ?> </li> </ul> <?php endif; ?> <?php $posts = get_field('route'); if( $posts ): ?> <ul> <li> From:<?php the_field('route_from'); ?> </li> <li> To:<?php the_field('route_to'); ?> </li> </ul> <?php endif; ?> // Custom loop ends genesis();
December 23, 2018 at 7:35 am #225217andytcParticipantYou would create a file with your working code and name it - archive-trucks.php - then place that file into your child themes root directory.
If the CPT archive isn't showing any 'content' from the wordpress editor and it's just the custom fields only , you probably want to remove the standard loop and replace it with your own custom loop.
If you just want to add the ACF fields to existing post content , for example below the post or above it , then there's no need for a full custom archive , you could just add the fields with a conditional in functions.php
Can't help much more as you haven't provided any info on how you expect this archive to look or how the fields should be laid out , repeater fields etc. You'd need to show a basic image mock up and an explanation of how you expect it look.
If you can do that you might get more useful help.
December 23, 2018 at 8:15 am #225218photoaddictsaMemberThanks @andytc,
Apologies for not being clearer. I've already got the archive file there, it's the loop that I'm battling with.
I'm using relationship fields, seemed like the best one for this idea.
Here is a graphic of what I'm hoping to achieve.
This one of the relationship fields.
<?php $posts = get_field('route'); if( $posts ): ?> <ul> <li> From:<?php the_field('route_from'); ?> </li> <li> To:<?php the_field('route_to'); ?> </li> </ul> <?php endif; ?>
In the graphic there is a column for the route, the above will then display:
- From: destination a
- To: destination b
Each post on the archive page will look like the graphic, no need to click through to a single post.
December 23, 2018 at 11:02 am #225219andytcParticipantThis should get you started with it , I haven't included your ACF fields only the post-title , it's best you play with that to get what you want. I've added some comments so you know what's what ... remove these when you are done.
It's set-up for pagination and will use the posts per page specified in WordPress 'Reading' Settings.
Some 'removes' are present at the start of the template shown as examples , just un-comment the lines if you want to use them or add your own from genesis.
<?php /* * Custom WordPress Template: Archive trucks */ // Removing elements from the archive - add/remove what you need - // Example removes Breadcrumbs // remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' ); // Example - removes Archive Title and Description // remove_action( 'genesis_before_loop', 'genesis_do_cpt_archive_title_description' ); // Removing the standard loop remove_action( 'genesis_loop', 'genesis_do_loop' ); // Adding our custom trucks loop add_action( 'genesis_loop', 'trucks_archive_loop' ); function trucks_archive_loop() { // Define the query for CPT named trucks $args = array( 'post_type' => 'trucks', // the name of your CPT 'posts_per_page' => get_option('posts_per_page'), // Uses the WordPress 'Reading' Settings post per page 'post_status' => 'publish', 'paged' => get_query_var( 'paged' ) ); // run our query $query = new WP_Query( $args ); if( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); ?> <h2><?php the_title(); ?></h2> <!-- post title --> <!-- ADD YOUR CUSTOM FIELDS HERE --> <?php } // endwhile do_action( 'genesis_after_endwhile' ); // Pagination - previous >> next page - 'Reading' Settings post per page } else { // no posts found. } } // end function trucks_archive_loop // reset postdata to restore orginal query wp_reset_postdata(); // call genesis for everything else genesis();
December 23, 2018 at 11:07 am #225220andytcParticipantAlthough the single posts will not be linked to , they can still be accessed. If you wanted to totally deny access to the single posts add this into functions.php of your child theme.
/********************************** * * Deny Single post access * **********************************/ /* Deny access to single-post.php for trucks CPT */ function pl_disable_single_truck_views() { $queried_post_type = get_query_var('post_type'); if ( is_single() && 'trucks' == $queried_post_type ) { wp_redirect( home_url(), 301 ); exit; } } add_action( 'template_redirect', 'pl_disable_single_truck_views' );
December 23, 2018 at 11:22 am #225221photoaddictsaMemberThank you very very much @andytc,
Appreciate you taking the time to answer this, this will most definitely get me going the right way.
-
AuthorPosts
- The topic ‘Custom archive loop for CPT’ is closed to new replies.