Blade & Custom Post Types


#1

Has anyone been able to successfully create a CPT, then query it from a template in blade? I created the post type using CPT UI.

I’ve been digging through the docs and discourse, and I’m not sure what step I’m missing.

Many thanks!


#2

I do this all the time. What have you tried? What are your templates and how are they not working?


#3

I might have solved my own problem. My CPT settings reflect, has_archive: false when it should have been true.

I basically just set up the CPT and created an archive template, and was able to get the post type to render now. Is there a better way to do this or did I get it right?

I setup up custom post type called “case_studies” in CPT UI. Here is a screenshot of the settings I’m using to populate the CPT:

Then I created a template called archive-case_study.blade.php in the templates folder:
@extends(‘layouts.base’)

@section('content')
  @while(have_posts()) @php(the_post())
    @include('partials/content-single-'.get_post_type())
    @title
  @endwhile
@endsection

#4

And is it working now?

Also, recommendation, if you’re not too married to CPT-UI, take a look at PostTypes, a class you can include in a plugin or in your theme that makes creating new post types dead simple. I always prefer to create post types via my theme or an mu-plugin (and required by Bedrock) because that way my theme’s templates don’t rely on a plugin’s database entries to work properly.

Good luck!


#5

Heya. I tried PostTypes but it does not create a post type for me. No error though, in setup.php I included it use PostTypes\PostType; and then added ‘init’ action with basic post type register as in examples, but no post type appears in admin … Any ideas ? :open_mouth: Do I need to do something special with Sage ? If I use basic register_post_type it works as expected … was hoping I could make it nicer using PostTypes :slight_smile:


#6

Hi,
The best info I can give is a working example in this plugin:

I hope this helps!


#7

TY will check it out :slight_smile:


#8