Last active
July 4, 2019 11:42
-
-
Save taciara/8fcbdffdff5a7d2ea5797d5bb034ab6e to your computer and use it in GitHub Desktop.
Remover o Slug do CPT na ulr e no Permalinks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| 1. Create your custom post type (unless already created). This can be done very easily by using the great, free Custom Post Type UI plugin. | |
| 2. Create a plugin for our new code to live in (yes, it could go in your theme’s functions.php file, but then it’d be lost if the theme were changed!). | |
| ?> | |
| <?php | |
| // PARTE 1 DO FUNCTIONS: | |
| // 3. Filter the permalink for our custom post type so that all published posts don’t have the slug in their URLs: | |
| //Remova o slug dos permalinks de postagem publicados. Apenas afeta nosso tipo de postagem personalizada. | |
| function gp_remove_cpt_slug( $post_link, $post ) { | |
| if ( 'race' === $post->post_type && 'publish' === $post->post_status ) { | |
| $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); | |
| } | |
| return $post_link; | |
| } | |
| add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 2 ); | |
| // PARTE 2 DO FUNCTIONS: | |
| // 4. At this point, trying to view the link would result in a 404 (Page Not Found) error. That’s because WordPress only knows that Posts and Pages can have URLs like domain.com/post-name/ or domain.com/page-name/. We need to teach it that our custom post type’s posts can also have URLs like domain.com/cpt-post-name/. | |
| / ** | |
| * Faça com que o WordPress corresponda ao nome do post a qualquer um dos nossos tipos de postagem pública (postagem, página, corrida). | |
| * Todos os nossos tipos de postagens públicas podem ter / post-name / como slug, por isso precisam ser únicos em todas as postagens. | |
| * Por padrão, o WordPress contabiliza apenas postagens e páginas nas quais o slug é / post-name /. | |
| * | |
| * / | |
| function gp_add_cpt_post_names_to_main_query( $query ) { | |
| // Bail if this is not the main query. | |
| if ( ! $query->is_main_query() ) { | |
| return; | |
| } | |
| // Bail if this query doesn't match our very specific rewrite rule. | |
| if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) { | |
| return; | |
| } | |
| // Bail if we're not querying based on the post name. | |
| if ( empty( $query->query['name'] ) ) { | |
| return; | |
| } | |
| // Add CPT to the list of post types WP will include when it queries based on the post name. | |
| $query->set( 'post_type', array( 'post', 'page', 'race' ) ); | |
| } | |
| add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' ); | |
| // apos isso, va até o pemalinks e salve. | |
| /* | |
| É isso aí! Basta alterar as duas instâncias racedesses exemplos de código para o slug do seu tipo de postagem personalizado e substituir gp_por qualquer prefixo de função que você queira (suas iniciais ficariam bem) e você deve estar pronto. Ir para Settings> Permalinkse salvar a estrutura do link permanente para terminar /%postname%/também pode ser necessário. | |
| */ | |
| //https://kellenmace.com/remove-custom-post-type-slug-from-permalinks/ | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment