Community Forums › Forums › Archived Forums › Design Tips and Tricks › Custom loop help needed
Tagged: CPT Pagination, Custom fields, custom loop, genesis loop
- This topic has 14 replies, 2 voices, and was last updated 8 years ago by
Brad Dalton.
-
AuthorPosts
-
April 20, 2017 at 1:43 am #205041
michaeloeser
MemberHi,
I have a (maybe simple) question: I need to create a custom loop for a CPT archive page. I already have the archive template (archive-marketplace.php) which currently uses the default loop. I need to change that in order to display some (custom)fields that are added by using ACF.
I know there has a lot been written about custom loops but I can´t find a simple tutorial or example of such a custom loop.
This is the CPT archive so far http://www.automotiveit.eu/marketplace
Any support will be great.
Thanks y´all
MichaelApril 20, 2017 at 1:48 am #205043Brad Dalton
ParticipantApril 20, 2017 at 1:53 am #205044michaeloeser
MemberIt´s custom child theme based on the default Genesis theme. Nothing spectacular.
April 20, 2017 at 3:57 am #205048Brad Dalton
ParticipantApril 20, 2017 at 4:16 am #205049michaeloeser
MemberI want to list the entries of that CPT marketplace on the archive page (http://www.automotiveit.eu/marketplace) in the form of showing several custom fields (like logo, some text etc.) as well as tags for each entry.
A bit as shown in this screenshot (not exactly but similar)
http://dl.dropbox.com/u/4814298/archive-IT4automotive.jpgSo I need to modify the loop of the CPT marketplace archive page which currently uses the default loop.
April 20, 2017 at 6:12 am #205055Brad Dalton
ParticipantYou want to output the custom field value in the entry for each post on the CPT archive?
April 20, 2017 at 6:41 am #205056michaeloeser
MemberYes and some extra text possibly. Sounds like problem?
April 20, 2017 at 7:08 am #205058Brad Dalton
ParticipantVery simple. Use this tutorial
2 Ways to use the code:
1. You can change the hook to genesis_entry_content and remove the conditional then add the code directly to your template.
Or
2. Use the code in functions.php with a conditional tag for your archive type.
Tested
add_action( 'genesis_entry_content', 'michaeloeser', 13 ); function michaeloeser() { $cf = get_post_meta( get_the_ID(), 'test', true ); if ( ! empty( $cf ) ) { echo '<div class="text">'. $cf .'</div>'; } }
This code works with both the entry content and excerpt settings.
April 20, 2017 at 7:59 am #205059Brad Dalton
ParticipantApril 20, 2017 at 8:11 am #205060michaeloeser
MemberThanks. Will take a look at it
April 21, 2017 at 4:01 am #205108michaeloeser
MemberHey Brad,
I figured everything out now except one simple thing. I created my own custom loop and that works. What currently not works is the default genesis page navigation at the end of a (archive) page. I´m talking about this "1,2,3...next page" navigation on the bottom of a default archive page (e.g http://www.automotiveit.eu/category/news) and I want to have that here as well: http://www.automotiveit.eu/marketplace
Of course it can´t work because my loop doesn´t "know" about it and I assum I need to add it to my loop. But where can I find it how does it look? Is there a piece of code that I can add to my loop which outputs the default Genesis paging?
Thanks
MichaelApril 21, 2017 at 5:11 am #205110Brad Dalton
ParticipantHello Michael
Works exactly the same for CPT loops.
You'll need to check your posts per page settings as these affect all archive page types includes CPT archives unless overridden using code.
If you have set posts per page to display 10 posts and you have 10 or less posts in the archive, pagination will NOT display as there's no more posts to paginate.
You can override CPT archives using pre_get_posts.
See updated tutorial.
April 21, 2017 at 5:56 am #205111Brad Dalton
ParticipantHere's a video where i explain how to add custom fields to a custom post type archive page loop.
Includes explanation about pagination.
April 28, 2017 at 3:02 am #205443michaeloeser
MemberHi Brad,
sorry for my late reply but I was quite busy in the last couple of days.
My "problem" is I do not use the default Genesis loop at all. I use a custom loop. And by removing the default loop the pagination gets removed also
remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'dp_amtv_loop_archive_marketplace' );
After that I go on with
function dp_amtv_loop_archive_marketplace() { ?> <?php // the query $args = array( 'post_type' => 'marketplace', 'post_status' => 'publish', 'posts_per_page' => 10 ); $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : ?> <!-- pagination here --> <!-- the loop --> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <!-- include the archive page output --> <?php include( CHILD_DIR . '/inc/marketplace-stuff-archive.php' ); ?> <?php endwhile; ?> <!-- end of the loop --> <!-- pagination here --> <?php wp_reset_postdata(); ?> <?php else : ?> <p><Nothing found...</p> <?php endif; ?> <?php } // end function ?>
I want the pagination where it says <!-- pagination here -->
That´s why I mean there must be some kind of code snipped for that (hopefully)
Any suggestions?
Cheers
MichaelApril 28, 2017 at 5:02 am #205447Brad Dalton
Participant -
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.