Posso atribuir um modelo a um tipo de postagem personalizado?
-
-
http://www.wpbeginner.com/wp-themes/create-Custom-single-post-templates-for-specific-posts-or-section-in-wordpress/(isto éparaposts,mas vocêpodemodificá-lopara o cpt) http://www.nathanrice.net/blog/wordpress-single-post-templates/(isto éparaposts,mas vocêpodemodificá-lopara o CPT) é umaboaideiapara umplugin realmente.http://www.wpbeginner.com/wp-themes/create-custom-single-post-templates-for-specific-posts-or-sections-in-wordpress/ ( this is for posts but you can modify it for CPT's) http://www.nathanrice.net/blog/wordpress-single-post-templates/ ( this is for posts but you can modify it for CPT's) It's a good idea for a plugin actually.
- 0
- 2011-07-22
- Wyck
-
8 respostas
- votos
-
- 2017-04-23
de WordPress versão 4.7 Agora vocêpode atribuirmodelos depáginapersonalizadospara outrostipos depostagemjunto com apágina.
Para conseguirisso,além do cabeçalho do arquivo denome domodelo,ospós-programas suportadospor ummodelopodem serespecificados usando o Tipo de Post de Template: Como segue.
<?php /* Template Name: Full-width page layout Template Post Type: post, page, product */
Vocêpode obtermaisinformações sobre as seguintespáginas.
https://wppavern.com/wordpress-4-7-Brings-custom-page-template-functionality-to-PoT-POST-POST-YPES https://make.wordpress.org/núcleo/2016/11/03/pós-modelos-tipo-em-4-7/
From WordPress version 4.7 you can now assign custom page templates to other post types along with page.
To achieve this in addition to the Template Name file header, the post types supported by a template can be specified using Template Post Type: as following.
<?php /* Template Name: Full-width page layout Template Post Type: post, page, product */
You can get more information about it on the following pages.
https://wptavern.com/wordpress-4-7-brings-custom-page-template-functionality-to-all-post-types https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/
-
- 2011-07-21
Vocêpode criarmodelopara ostipos depostagempersonalizados,criando arquivos,comoporexemplo:
single-mycustomposttype.php
ver hierarquia demodelo no códice.
You can create template for custom post types by creating files, like for example :
single-mycustomposttype.php
See Template Hierarchy in the codex.
PS : this has already been answered.
-
Obrigadoporisso,mas o queeugostaria de saber é se épossível adicionarmodelospersonalizados atipos depostpersonalizados.Então,porexemplo,posso criar doismodelose atribuir cadamodelo ao seuposto respectivo?Isso sópermiteespecificar um arquivo demodelopara lidar comessetipo depostagemespecífico,tanto quantoeu aentendo.Thanks for that, but what I would like to know is if it is possible to add custom templates to custom post types. So for instance can I create two templates and assign each template to it's respective post? That only allows for specifying one template file to handle that specific post type as far as I understand it.
- 2
- 2011-07-21
- Odyss3us
-
É ver,se vocêprecisar demodelos diferentesparaposts únicos,vocêpode criar váriostipos depostagempersonalizados,de acordo com cadamodelonecessário.Eu acho queisso depende de quantosmodelos diferentes vocêprecisa.O que você vaifazernosmodelos queprecisam ser diferentesem cadapost?Is see, if you need different templates for single posts, you may want to create several custom post types, according to each needed template. I guess that depends on how many different templates you need. what are you going to do in the templates that needs to be different on each post ?
- 0
- 2011-07-22
- mike23
-
Esta resposta agoraestá desatualizada.Veja a resposta de Vinod Dalvi.This answer is now out of date. See Vinod Dalvi's answer.
- 0
- 2017-09-12
- Simon East
-
Nãoestá desatualizado.Aindafuncionamuitobem e ainda épráticapadrão.It is not out of date. Still works just fine and is still standard practice.
- 1
- 2020-03-01
- Tim Hallman
-
- 2015-06-11
Aquiestá o quefuncionaparamim:
add_filter ('single_template',função ($ original) { postagemglobal; $post_name=$post-e gt;post_name; $post_type=$post- >post_type; $base_name='single-'.$post_type.'-'.$post_name.'.php'; $template=locate_template ($base_name); se ($template & amp;e amp; vazio ($modelo)) retornar $ Modelo; retornar $ Original; });
Assim,dada umapostagem detipopersonalizado
foobar
e uma lesma deOlá-World O código acima carregaria o Single-Foobar-Hello-World.modelophp
,seexistiu.Here's what works for me:
add_filter('single_template', function($original){ global $post; $post_name = $post->post_name; $post_type = $post->post_type; $base_name = 'single-' . $post_type . '-' . $post_name . '.php'; $template = locate_template($base_name); if ($template && ! empty($template)) return $template; return $original; });
Thus given a post of custom post type
foobar
and a slug ofhello-world
the above code would load thesingle-foobar-hello-world.php
template, if it existed. -
- 2017-03-08
Para aqueles que atingemessetópico através do Google,WP 4.7introduziumodelosparatodos ostipos depostagem.Ver Make WP Core Para umpasso apasso completo.Vocênãoestámais restrito a ummodeloparatodos os seus CPT,vocêpode atribuirmodelosindividuaispostpelopost apenas como vocêpodefazer compáginas.
For those reaching this thread through Google, WP 4.7 introduced templates for all post types. See Make WP Core for a full walkthrough. You're no longer restricted to one template for all of your CPT, you can assign individual templates post by post just like you can do with Pages.
-
- 2016-07-13
Isso é umpouco velho,mas vocêtambémpodetentarisso:
Crie ummodelopara otipopersonalizado Tipo:
single-*custom-post-type-slug*.php
O arquivo deve verificar a lesmae verificar seexiste um arquivo,senão,fallbackem um arquivo demodelopadrão:
<?php $slug = get_post_field( 'post_name', get_post() ); $slug = ( locate_template( 'templates/*custom-post-type-slug*/' . $slug . '.php' ) ) ? $slug : 'default'; get_template_part( 'templates/*custom-post-type-slug*/' . $slug ); ?>
Substituatodas asinstâncias de Slug depós-tipopersonalizado com a lesma real do seutipo depostagempersonalizado.
Eufaçoissoporfacilidade de usoe fins organizacionais.Parecemais limpoparamim do quetertodos os arquivosnapasta raiz dotema.
Exemplo deestrutura depasta:
themeroot | |single-cases.php |-templates | --cases | |default.php | |case-one.php | |case-two.php
This is a little old but you can also try this:
Create a template for the custom post type:
single-*custom-post-type-slug*.php
The file should check the slug and verify if a file exists, if not, fallback to a default template file:
<?php $slug = get_post_field( 'post_name', get_post() ); $slug = ( locate_template( 'templates/*custom-post-type-slug*/' . $slug . '.php' ) ) ? $slug : 'default'; get_template_part( 'templates/*custom-post-type-slug*/' . $slug ); ?>
Replace all instances of custom-post-type-slug with the actual slug of your custom post type.
I do this for ease of use and organizational purposes. It seems cleaner to me than having all of the files in the theme's root folder.
example folder structure:
themeroot | |single-cases.php |-templates | --cases | |default.php | |case-one.php | |case-two.php
-
- 2015-07-13
Primeiro crienapáginanomeada comoitens como seu desejo queexibe o conteúdo deitens Post Tipos,do que criar um arquivo demodelo como abaixoe nomeado que ositens domodelo. Selecioneessemodelopara apágina que você criou.
<div class="container"> <div class="row"> <div class="col-md-9"> <div class="panel panel-default text-center"> <?php $loop = new WP_Query( array( 'post_type' => 'items', 'posts_per_page' => 5 ) ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <?php the_title();?> <?php if(has_post_thumbnail() ) { the_post_thumbnail(); } ?> <?php the_content();?> <?php endwhile; ?> <?php wp_reset_query(); ?> </div> </div> </div> </div>
First create on page named as Items as your wish which display the content from items post types, than create one template file as below and named that template-items. Select that template for the page you have created.
<div class="container"> <div class="row"> <div class="col-md-9"> <div class="panel panel-default text-center"> <?php $loop = new WP_Query( array( 'post_type' => 'items', 'posts_per_page' => 5 ) ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <?php the_title();?> <?php if(has_post_thumbnail() ) { the_post_thumbnail(); } ?> <?php the_content();?> <?php endwhile; ?> <?php wp_reset_query(); ?> </div> </div> </div> </div>
-
- 2019-06-07
Isso émuito simples defazer.
Crie umnovo arquivo PHPno diretório raiz do seutemae adicioneisso aotopo:
<?php /* * Template Name: My custom view * Template Post Type: Post_typename // here you need to add the name of your custom post type */ ?>
Exemplo completo será o seguinte:
<?php /* * Template Name: My custom view * Template Post Type: Post_typename // here you need to add the name of your custom post type */ ?> <?php get_header();?> <div class="container pt-5 pb-5"> <?php if (has_post_thumbnail()):?> <img src="<?php the_post_thumbnail_url('largest');?>" class="img-fluid"> <?php endif;?> <?php if (have_posts()) : while (have_posts()) : the_post();?> <?php the_content();?> <?php endwhile; endif;?> </div> <?php get_footer();?>
This is very simple to do.
Create a new PHP file in your theme root directory and add this to the top:
<?php /* * Template Name: My custom view * Template Post Type: Post_typename // here you need to add the name of your custom post type */ ?>
Full example will be as following:
<?php /* * Template Name: My custom view * Template Post Type: Post_typename // here you need to add the name of your custom post type */ ?> <?php get_header();?> <div class="container pt-5 pb-5"> <?php if (has_post_thumbnail()):?> <img src="<?php the_post_thumbnail_url('largest');?>" class="img-fluid"> <?php endif;?> <?php if (have_posts()) : while (have_posts()) : the_post();?> <?php the_content();?> <?php endwhile; endif;?> </div> <?php get_footer();?>
-
- 2016-04-01
Nóstrabalhamos outra solução usando o pluginpersonalizadopost
We worked out another solution using the custom post template plugin
Posso atribuir um arquivo demodelo a umtipopersonalizado depostagem?
Eu criei umtipo depostagempersonalizado chamado
items
e eugostaria de atribuirmodelos aositens como vocêpodefazer compáginas.