How to create a single page in Wordpress for a post-type?

Asked

Viewed 145 times

1

I created a specific Post-type in functions.php:

// Meus posts types
function meus_posts_type() {
    // Testemunhos
    register_post_type('testemunhos',
        array(
            'labels'    => array(

                'name'  => __('Testemunhos'),
                'singular_name'     => __('Testemunhos')
            ),
            'public'        => true, 
            'has_archive'   => true, 
            'menu_icon'     => 'dashicons-format-chat',
            'supports'      => array('title', 'editor', 'thumbnail', 'page-attributes'),

        )    
        );

        register_post_type('consultorias',
        array(
            'labels'    => array(

                'name'  => __('Consultorias'),
                'singular_name'     => __('Consultorias')
            ),
            'public'        => true, 
            'has_archive'   => true, 
            'menu_icon'     => 'dashicons-clipboard',
            'supports'      => array('title', 'editor', 'thumbnail', 'page-attributes'),

        )    
        );

        register_post_type('treinamentos',
        array(
            'labels'    => array(

                'name'  => __('Treinamentos'),
                'singular_name'     => __('Treinamentos')
            ),
            'public'        => true, 
            'has_archive'   => true, 
            'menu_icon'     => 'dashicons-welcome-learn-more',
            'supports'      => array('title', 'editor', 'thumbnail', 'page-attributes'),

        )    
        );

        register_post_type('clientes',
        array(
            'labels'    => array(

                'name'  => __('Clientes'),
                'singular_name'     => __('Clientes')
            ),
            'public'        => true, 
            'has_archive'   => true, 
            'menu_icon'     => 'dashicons-admin-multisite',
            'supports'      => array('title', 'thumbnail', 'page-attributes'),

        )    
        );
}

add_action( 'init', 'meus_posts_type' );

OK.

I made a "page-training.php" page with:

<?php 

get_header(); 

/*
    Template name: Treinamentos
*/

?>


<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>

<section class="features18 popup-btn-cards cid-rlWkB2eQKv" id="features18-y">




    <div class="container">
        <h2 class="mbr-section-title pb-3 align-center mbr-fonts-style display-2"><strong>WORKSHOPS E TREINAMENTOS</strong></h2>
        <h3 class="mbr-section-subtitle display-5 align-center mbr-fonts-style mbr-light"><strong>GOVERNOS | EMPRESAS | ONGs (IN-COMPANY
)</strong><div>Workshops e treinamentos especializados em gestão integrada de resíduos sólidos
</div><div><br></div></h3>

<?php
            if ( function_exists('yoast_breadcrumb') ) {
            yoast_breadcrumb( '<p id="breadcrumbs">','</p>' );
            }
        ?>

<?php query_posts( 'post_type=treinamentos' ); ?>

       <?php if(have_posts()): while(have_posts()): the_post(); ?>
.......

However, when I click on the link I want, the single.php does not show the post of the post-type, it shows the 404.php.

How do I read the post-type post? Do I have to create a single-nameSingle.php? If so, what would this structure look like?

My single.php is like this:

<?php get_header();?>

<br/><br/><br/><br/><br/><br/><br/><br/>

<div class="container">
<?php
            if ( function_exists('yoast_breadcrumb') ) {
            yoast_breadcrumb( '<p id="breadcrumbs">','</p>' );
            }
        ?>
     <center>
    <h1><?php single_post_title(); ?></h1>
    </center>   
    <div class="row">   
        <div class="col-md-9">


        <?php
            if (have_posts()): the_post();
                    the_post_thumbnail('medium_large', array('class' => 'img-fluid'));
                    the_content();
            endif;
        ?>

        </div>
        <div class="col-md-3">
            <?php if ( is_active_sidebar('sidebar-1') ) {
                dynamic_sidebar('sidebar-1');
            } ?>    
        </div>
        <!--<div class="col-md-3">
            <?php //get_sidebar('personalizado') ?>
        </div>-->
    </div>
</div>

<?php //get_footer('personalizado'); ?>
<?php get_footer(); ?>

2 answers

1

Yes. His structure is identical to a single.php.

<?php

get_header(); ?>

<div id="main-content" class="main-content">

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <?php
                // Start the Loop.
                while ( have_posts() ) : the_post();

                    // Include the page content template.
                    get_template_part( 'content', 'page' );

                endwhile;
            ?>
        </div><!-- #content -->
    </div><!-- #primary -->
</div><!-- #main-content -->

<?php
get_sidebar();
get_footer();

This link helps.

  • I edited with my 'Single.php'. The way you presented my 'Page.php' to list, I need to show the contents of what is already listed in a 'post_type' .... @daviaragao

  • I don’t understand what you said.

0


Talk partner! all beauty?

You can make specific single for post types this way below:

single-testimonials.php
single-consultorias.php
single-training.php
single-client.php

Then you can follow with the same structure as it is in the single.php

<?php

get_header(); ?>

<div id="main-content" class="main-content">

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
            <?php
                // Start the Loop.
                while ( have_posts() ) : the_post();

                    // Include the page content template.
                    get_template_part( 'content', 'page' );

                endwhile;
            ?>
        </div><!-- #content -->
    </div><!-- #primary -->
</div><!-- #main-content -->

<?php
get_sidebar();
get_footer();

that is, the hierarchy works in this way:

single-MEU_POST_TYPE

And so, it also works in the same way, for the file page, see:

Archive-testimonials.php
Archive-consultorias.php
Archive-trainings.php
Archive-clients.php

I hope it helps you!

Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.