Custom Post Type Archive Template
-
I love this theme, but I have two custom post types and I would like to use custom page templates for their archive pages (mostly just to add a nice title and some intro text).
What files do I need to create for a page template that runs the loop for a custom post type? Are there files included I can use as a guide?
Thank you!
-
Hi Tim,
you can use the the custom-page.php, rename it and give it a proper name in the header.Depending on what you wanna do, in your loop you have to declare the name of your custom-post-type with
‘post-type’ => ‘your-cpt-name’Here is an example for my CPT called ‘projects’
<?php $args = array( 'posts_per_page' => -1, 'post_type' => 'projects' ); query_posts($args); ?>
Hope it helps
Hi Giorgio,
Thank you -that got me a lot closer!
I also need to change the loop output on these so that the title and featured image link to each client’s website. Can you tell me where I go to do that?
So you have a better idea of what it is I’m doing, this is what I’m working on…
https://dev.nuggetweb.com/clients/
Thanks again!
Tim
Hey Tim,
there are several ways to achieve your goal:
-hard coding functions.php and index.php with custom loops
-using SEO plugins to redirect each post to a specific url
-using this plugin: Page Links ToNow you have to understand that this topic is on the discussion table since 10 years: WP forum 2004
So my recommendation is to consider pros and cons; if for e.g. you are already using the YOAST SEO plugin you should use it also to redirect those posts saving a lot of time in customizing your loop.
If you wanna go for the hard way, coding your loop, I can point you in a certain direction with a 2013 tutorial.
Your call,
let us know!Hi Giorgio – Challenge accepted! ??
I would really like to have control of the loop, so I can tweak it how I want, so I’ll take look at the tutorial. I would still like to keep the styling that Customizr uses, but I need to be able to use CPT-specific content.
Thank you for all of the help!
Tim
Sounds good to me ??
So here is the 2012 tutorial (sorry for saying 2013!)
How to Link to External Links from the Post TitleIt gives a clear idea of what you need to do with custom fields and loop.
If you succeed please share your solution; I have no time right now, but I would try also ACF to create a custom solution.
Good luck
Hi Giorgio,
This is where I got lost before… the article creates a replacement function for the_title() function, but that is not in the Customizr template.
The part of the template where you would expect to find the_title(), the_thumbnail(), the_content() etc, you only find do_action(__loop), which is where I fell off the cliff. I don’t see where loop is defined.
<?php do_action( '__before_article' ) ?> <article <?php tc__f( '__article_selectors' ) ?>> <?php do_action( '__loop' ); ?> // <<---- This is the code I need to replicate and modify into a new function </article> <?php do_action( '__after_article' ) ?>
Thanks!
Tim
FYI – I’m going to knock off for the day, but I think I’m getting closer with this filter:
https://themesandco.com/snippet/expand-recent-post-category-list/
Right now it’s doubling up the output, but I’m going to try to work with it and see if I get anywhere. This seems way more complicated than it needs to be. :/
I’m going to knock off for the WE.
I was able to double the title link, but it does not link outside wordpress. ??I’m working on this piece of code:
function tc_content_heading_title( $_title ) { //Must be in the loop if ( ! in_the_loop() ) return $_title; //gets the post/page title if ( is_singular() || ! apply_filters('tc_display_link_for_post_titles' , true ) ) return is_null($_title) ? apply_filters( 'tc_no_title_post', __( '{no title} Read the post »' , 'customizr' ) ) : $_title; else return sprintf('<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>', get_permalink(), sprintf( apply_filters( 'tc_post_link_title' , __( 'Permalink to %s' , 'customizr' ) ) , esc_attr( strip_tags( $_title ) ) ), is_null($_title) ? apply_filters( 'tc_no_title_post', __( '{no title} Read the post »' , 'customizr' ) ) : $_title );//end sprintf }
It is annoying though! Way more complicated than expected.
Customizr has this super hooked loop that is tricky to customize.In the documentation when they explain their loop there is this link:
Three techniques to alter the query in WordPressNo kidding! I don’t know why they made it so complicated but I’m afraid I’m going to have to spend some time digging into the code to figure out what’s going on with their loop.
I did get a reply from someone on their support board. He said that they will have a custom post type guide out “pretty soon”, but was otherwise of little help.
Tim
Ok, I’ll try to dig into it next vacation days.
PHP and WP are getting more and more complex every day, we need to keep up!
If you come up with a solution earlier let me know ??
Hi Tim,
can I know if you’re using some plug-in for cpt?
Also, specifically, what’s your goal?
I can assure you Customizr loop isn’t that complicated.Hi @twalker123,
I am sorry it was little help but what you want to go into details.
The link provided by Giorgio Three techniques to alter the query in WordPress could really help you dig into it.This CPT for Customizr is something I have in mind since quite a while but I couldn’t find the time to write it yet.
Any contribution is welcome on this by the way! ??Cheers
Keeping up is the hard part. Just when I think I’ve got it figured out somebody comes up with a “better” way to do something. ??
I ended up looking at the same function in class-content-headings.php. The action filter is set at line 81 so I tried to unset the action filter, which seems like it should have worked, but didn’t…
<?php remove_filter( ‘the_title’, ‘tc_add_comment_bubble_after_title’ ); ?>
No worries Giorgio. I’ll be working on this hit-and-miss during the week, but I’m not sure how much time I’ll have to put into it until next week. I really appreciate your help!
Tim
Hi d4z_c0nf and nikeo – thanks for replying!
d4z – I wrote my own plugin to add the CPTs I needed. The CPT plugins I found out there added more bloat than I felt like I wanted, so there’s no admin interface – just code to add the CPT the way I need it.
What I’m doing is changing our current website from the current theme to Customizr Pro, and the only thing that doesn’t work the way I want it to is the CPTs.
This is the one I’m starting with:
Current website: https://nuggetweb.com/clients/
Dev website: https://dev.nuggetweb.com/clients/What I want to do is really simple. I have a custom page template with the loop for the CPT, and I’m assigning the template to a page. What I need it to do is…
1) replicate the Customizr loop so the style of the output is consistent from a blog page to the CPT list page.
2) Change the links on the title and featured image to link to our client’s website – this is a CPT field.The problems I’m having are:
1) I can’t find a loop in the Customizr code to replicate, so I’m using the code from our current website – the CSS is way wrong.
2) Something in Customizr is overwriting the_title() so my titles don’t link to our client’s websites. The featured images link works as expected. See Giorgio and my last post for where we think it’s being overwritten.Any ideas on where to go from here would be really appreciated. I do really like the theme, BTW – so I don’t mean to be hard on anyone. It’s this one problem that I’m getting stuck on, and it probably wouldn’t effect all that many people.
Tim
Ok, this is a solution you might find interesting.
I’m not familiar with custom post types, but by principle, this should work. You have to change something here and there.
1) Create a new php file in your child-theme dir. Name it the way you want.
2) Fill it with this code:<?php /* Template Name: Clients */ /* I use the action hook __after_loop * and partially replicate the customizr loop * to achieve: * 1) keep the page title * 2) show the page content if any, this can be used as a summary * before the list of posts */ add_action('__after_loop', 'clients_list', 0); function clients_list(){ add_filter('the_title', 'link_title_to_site', 0); global $wp_query; $wp_query = new WP_Query(array( /* change this with the query vars you need to display your cpts */ 'paged' => get_query_var('paged'), 'post_type' => 'post' ) ); if ( have_posts() ) : while ( have_posts() ) : the_post(); do_action ('__before_article'); ?> <article <?php tc__f('__article_selectors') ?>> <?php do_action( '__loop' ); ?> </article> <?php do_action ('__after_article'); endwhile; endif; ##end if have posts } function site_link(){ $href = '/*here your code to grab the title from the custom type post*/'; return $href; } /* the title link */ function link_title_to_site($title){ /* your code to wrap the title into a link */ return sprintf('<a href="%1$s" title="%2$s" target="_blank">%3$s</a>', site_link(), sprintf( __("Go to %s"), $title ), $title ); } add_filter('tc_post_thumb_wrapper', 'change_thumbnails_link'); function change_thumbnails_link($thumb){ return preg_replace('/<a(.*?)href="(.*?)"(.*?)>/', '<a\1href="'.site_link().'"\3>', $thumb ); } /* * in order to have the navigator we have to reset the custom query * with priority 30 (navigation has priority 20) */ add_action('__after_loop', 'reset_query', 25); function reset_query(){ global $wp_query, $wp_the_query; $wp_query = $wp_the_query; } /* let's remove customizr title filter on the title */ remove_filter('the_title', array(TC_Headings::$instance, 'tc_content_heading_title'), 0); /* uncomment line below if you want to get rid of comments bubble */ //remove_filter('the_title', array(TC_Headings::$instance, 'tc_add_comment_bubble_after_title'), 1); /* uncomment line below if you want to get rid of edit link */ //remove_filter('the_title', array(TC_Headings::$instance, 'tc_add_edit_link_after_title'), 2); /* * let's include customizr index.php * this way we'll not lose eventual * changes on it */ include_once( TC_BASE . 'index.php' );
3) don’t forget to use this as template for your clients page, before reporting it doesn’t work ??
Let me know how it works..
- The topic ‘Custom Post Type Archive Template’ is closed to new replies.