# Displaying Custom Post Type with Composer

**URL:** https://discourse.roots.io/t/displaying-custom-post-type-with-composer/26677
**Category:** sage
**Tags:** sage10
**Created:** 2024-02-05T14:15:06Z
**Posts:** 4

## Post 1 by @moisreis — 2024-02-05T14:15:06Z

Hi, that may be a noob question but I didn’t find answers in the forum so I need to ask. Sorry about that, really.

So I created a Composer called ‘Company’ with this code:

'\<?php

namespace App\View\Composers;

use Roots\Acorn\View\Composer;

class Company extends Composer  
{  
/\*\*  
\* List of views served by this composer.  
\*  
\* @var string  
\*/  
protected static $views = [  
‘partials.content’,  
];

```
/**
 * Data to be passed to view before rendering, but after merging.
 *
 * @return array
 */
public function override()
{
    return [
        'title' => $this->title(),
    ];
}

/**
 * Retrieve the post title.
 *
 * @return string
 */
public function title()
{       
    return get_the_title();
}
```

}’

And a content.blade with:

'\<article @php(post\_class())\>

## {{!! $title !!}}

```
</header>

<div class="entry-summary">
    @php(the_excerpt())
</div>
```
'

And in my index file:

’ @while(have\_posts()) @php(the\_post())  
@includeFirst([‘partials.content-’ . get\_post\_type(‘empresa’), ‘partials.content’])  
@endwhile’

So I didn’t add any custom meta yet, but even with ‘empresa’ in the get\_post\_type I still get the normal posts (Hello World). Can somebody tell me what I’ve missed?

---

## Post 2 by @moisreis — 2024-02-05T14:26:49Z

I did it, sorry to bother

Solution:

\<?php namespace App\View\Composers; use Roots\Acorn\View\Composer; class Company extends Composer { /\*\* \* List of views served by this composer. \* \* @var string[] \*/ protected static $views = ['partials.content',]; /\*\* \* Data to be passed to view before rendering, but after merging. \* \* @return array \*/ public function override() { return ['title' =\> $this-\>title(), 'cnpj\_data' =\> $this-\>cnpj\_data(), 'cnpj' =\> $this-\>cnpj(),]; } /\*\* \* Retrieve the company title. \* \* @return string \*/ public function title() { return get\_the\_title(); } /\*\* \* Retrieve the company CNPJ group meta. \* \* @return string \*/ public function cnpj\_data() { return get\_field('dados\_de\_cnpj'); } /\*\* \* Retrieve the company CNPJ code. \* \* @return string \*/ public function cnpj() { $cnpj\_data = get\_field('dados\_de\_cnpj'); return esc\_attr($cnpj\_data['cnpj']); } } And @php $args = ['post\_type' =\> 'empresa', 'status' =\> 'publish', 'order' =\> 'DESC', 'posts\_per\_page' =\> 16,]; $query = new WP\_Query($args); @endphp @if ($query-\>have\_posts()) @while ($query-\>have\_posts()) @php $query-\>the\_post(); @endphp @includeFirst(['partials.content-' . get\_post\_type(), 'partials.content']) @endwhile @endif

---

## Post 3 by @ben — 2024-02-05T15:09:37Z

[Please format your code when posting it](https://discourse.roots.io/t/how-to-best-ask-questions-on-this-forum/24582)

---

## Post 4 by @ben — 2024-02-05T15:09:39Z


