0

At the moment I create a new page, set a featured image and link a certain CPT template to show all those CPT's (by using a query) with the featured image on top. However, this is in my opinion the wrong way to do this because each time I create a new CPT I'll have to create a new template. My page types now look like "default, cases, products, clients, ..." which is not the purpose of a template.

I tried using archive-CPT.php which will automatically loop through the correct CPT but the problem is that I can't use a featured image anymore. I could, but I want to change/remove/add the image via Wordpress like I do with pages...

How would I be able to achieve this the right way?

EDIT: archive-clients.php

<?php
  while( have_posts() ) : the_post();
  $header = get_the_post_thumbnail_url();
    $title = get_post_meta(get_the_ID(), 'page_title', true);
    $subtitle = get_post_meta(get_the_ID(), 'page_subtitle', true);
?>
<header class="page-header about-header" style="background-image:url(<?php echo $header; ?>)">
  <h1 class="page-hero-title"><?php echo $title; ?></h1>
  <h2 class="page-hero-subtitle"><?php echo $subtitle; ?></h2>
</header>

<?php
  endwhile; 
  while( have_posts() ) 

: the_post();
?>

<div class="row clients">
  <?php while( have_posts() ) : the_post(); ?>
    <h1><php the_title(); ?></h1>
  <?php endwhile; ?>
</div>

<?php get_footer(); ?>
  • 1
    Why you can't use a featured image anymore? It has nothing to do with where the posts are being shown. Please provide your code to be clearer what are you doing – Laxmana Apr 27 '17 at 11:48
  • If I use archive-CPT.php (not using it as a template, but generated by WP) then I'm not able to add an featured image. I want to avoid having Cases archive or Product archive in my page templates because nothing else can use this.. – Greg Apr 27 '17 at 12:00
  • archive-{post_type}.php is not generated by WP. The responsibility lies on the Theme. Why don't you share your code? – Laxmana Apr 27 '17 at 12:03
  • Have added minimised code example. This was my page template first but have changed it to archive-client.php which means I can't reach the page meta anymore.. – Greg Apr 27 '17 at 12:15
  • Can you post the code you use to create the custom post type. – odinp123 Apr 27 '17 at 12:17
3

In my opinion the right way to do this is to create theme options. Create a field asking the desired image for the clients post type archive.

  • True, that's a solution but it puts me off because I'll have to add more fields in the future when there are more CPT's archive pages.. – Greg Apr 27 '17 at 12:39
  • You can dynamically create the theme option fields looping through all your CPT's – Laxmana Apr 27 '17 at 12:40
  • I didn't know that, I'll have to check that out then. Thanks! – Greg Apr 27 '17 at 12:41
  • The answer provided here stackoverflow.com/a/32164479/1287548 will help you to achieve it. – Vinod Dalvi Apr 27 '17 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.