Résoudre un conflit de nom de type de publication personnalisé et de permalien de page (même slug)
8 réponses
- votes
-
- 2014-02-19
Leplus simple serait de simplement désactiver lapage d'archivepour ce CPT:
register_post_type( 'visningshus', array( [...] 'has_archive' => false, [...] ) );
N'oubliezpas d'actualiser vospermalienspar la suite dans "Paramètres> Permaliens"
The easiest would be to just disable the archive page for this CPT:
register_post_type( 'visningshus', array( [...] 'has_archive' => false, [...] ) );
Don't forget to refresh you permalinks afterwards at "Settings > Permalinks"
-
Fonctionnaitparfaitement -maismaintenant,tous lespermaliens commençantpar lenom dutype depublicationpersonnalisé donnent un 404,bien qu'ils soient corrects dans wp-adminet qu'ilne semblepas y avoir de réécriture.C'est-à-dire quetous lespermaliens sont corrects sur le site,maistous lesmessagespersonnalisés donnent un 404. Pouvez-vousnous aider?:)Worked perfectly - but now all permalinks starting with the custom post type's name give a 404, despite that they're correct in wp-admin and there seems no rewrite taking place. That is to say, all permalinks are correct on the site, but all custom posts give a 404. Can you help with that? :)
- 1
- 2014-03-11
- Henrik Erlandsson
-
@HenrikErlandsson Peut-être que votretype demessageporte lemêmenom que lapage.Dans ce cas,je vous recommande de changer lenom de votretype depublication.Exemple: Nom dutype depublicationpersonnalisé - Avant:produits Après: cpt_products.@HenrikErlandsson Maybe your post type have the same name of the page. In this case I recommend you to change your post type name. Example: Custom post type name - Before: products After: cpt_products.
- 0
- 2016-04-06
- Bruno Monteiro
-
Trèsintelligent.Résout untas deproblèmesVery intelligent. Solves a bunch of problems
- 0
- 2017-04-09
- bysanchy
-
- 2017-03-14
Jene suispas assez réputépour commenter lemessage de @ Carl,mais c'est labonne réponse (dumoins à cette situation consistant à conserver le slug depage et le slug detype demessagepersonnalisé lemêmeet n'utilisepas defichier archive.php)
Mais sa réponse donnerait deserreurs depagination.Pour éviter leserreurs depagination:
add_rewrite_rule('^visningshus/page/([0-9]+)','index.php?pagename=visningshus&paged=$matches[1]', 'top');
Cela regarde lapagination,saisit lenuméro depage,puis sur le revers,leforce à utiliser lapage avec le slug correspondant,puisinsère la variable depage.
Ceci en plus de
'has_archive' => false,
publié ci-dessus devrait atteindre lafonctionnalité souhaitée.Comme d'autres l'ontindiqué,assurez-vous de vider les règles de réécritureen visitant Paramètres> Lienspermanents après avoireffectué cettemodification.
J'espère que cela aide quelqu'un,carje sais que celam'a déconcertépendanttrès longtemps.
I'm not high enough reputation to comment on @Carl's post, but his is the correct answer (at least to this situation of keeping the page slug and custom post type slug the same and not using an archive.php file)
But his answer would give errors with pagination. To avoid pagination errors:
add_rewrite_rule('^visningshus/page/([0-9]+)','index.php?pagename=visningshus&paged=$matches[1]', 'top');
This looks at the pagination, grabs the page number, and then on the flip side forces it to use the page with the matching slug, and then inserts the page variable.
This in addition to
'has_archive' => false,
posted above should achieve the desired functionality.As others stated, be sure to flush the rewrite rules by visiting Settings > Permalinks after making this change.
Hope this helps someone, because I know this stumped me for a really long time.
-
- 2016-05-13
Tjena Henrik!
Jepense quetout ce que vous avez àfaireest d'ajouter une règle de réécriture aux règles de réécriture Wordpress qui ont déjà été créées.Ajoutez cetextrait de code à votrefunctions.php de votrethème ouincluez-le comme unpugin séparé ...
add_action('init', function () { add_rewrite_rule('visningshus/?$','index.php?pagename=visningshus', 'top'); flush_rewrite_rules(); }, 1000);
Tjena Henrik!
I think all you need to do is to add a rewrite rule to the Wordpress Rewrite rules that have already been created. Add this code snippet to your functions.php of your theme or include it as a separate pugin...
add_action('init', function () { add_rewrite_rule('visningshus/?$','index.php?pagename=visningshus', 'top'); flush_rewrite_rules(); }, 1000);
-
Si quelqu'un l'utilise,supprimez `flush_rewrite_rules ();` Vousne devriezpas changer les règles de réécriture sur `init` à chaquefois.Rechargez unefoispuis supprimez-le,ou videzmanuellement lespermaliensen accédant auxparamètres depermalien dans letableau debord d'administrationet en cliquant sur "Enregistrer lesmodifications"If anyone uses this, remove `flush_rewrite_rules();` You shouldn't change the rewrite rules on `init` every time. Reload once then remove it, or manually flush permalinks by going to the permalink settings in admin dashboard and click "Save Changes"
- 4
- 2017-06-14
- tehlivi
-
Detoutes les réponsesici,celle-ci afonctionnépourmoi !!!Tysm.Out of all the answers here, this one worked for me !!! Tysm.
- 0
- 2020-06-11
- Pooja Mistry
-
- 2020-02-09
Si vous avezenregistré vous-même letype depublication,vouspouvezmodifier l'état de l'archiveet l'URL dans votrethème ouplugin.
'has_archive' => true,
active l'archive danspost-type-permalink.alors changez celaen'has_archive' => false,
désactivera l'archive.'has_archive' => 'custom-slug',
activera l'archive dans le slugpersonnalisé ( http://my-site.com/custom-slug/).Si unplugin ou votrethème l'enregistre,vouspouvez le désactiver comme l'afait krafter.Vouspouvez également utiliser un slugpersonnalisépour l'archive.
register_post_type( 'visningshus', array( [...] 'has_archive' => 'custom-slug', [...] ) );
Si vous avezbesoin à lafois de lapageet de l'archive,lameilleurefaçon serait deplacer l'archive dans un slugpersonnalisé différent.
If you registered the post type yourself, you can change the archive status and URL in your theme or plugin.
'has_archive' => true,
enables the archive in post-type-permalink. so change that to'has_archive' => false,
will disable the archive.'has_archive' => 'custom-slug',
will enable the archive in custom slug(http://my-site.com/custom-slug/).If a plugin or your theme register it, you can disable it with the way krafter did. You can also use custom slug for the archive.
register_post_type( 'visningshus', array( [...] 'has_archive' => 'custom-slug', [...] ) );
If you need both the page and the archive the best way would be to put the archive in a different custom slug.
-
- 2014-02-19
Je l'ai résolu sans résoudre le conflit. Créez unepage avec slug visningshuset écrivez-y ce que vous voulez. Preneznote de l'identifiant de cettepage.
Créezet personnalisez lemodèle archive-custom-post-type.php comme vous le souhaitez (vous utiliserezprobablement simplement unmodèle depage),mais remplacez lapartiebouclepar
$recent = new WP_Query("page_id=your-page-id"); while($recent->have_posts()) : $recent->the_post(); the_content(); endwhile;
Ensuite,lorsque vous accéderez à http://my-site.com/visningshus ,vous verrezle contenugérépar lapage à l'URL correcte.
Faites-moi savoir que celafonctionne.
I solved it without solving the conflict. Create a page with slug visningshus and write whatever you want in it. Take note on the ID of this page.
Create and customize the archive-custom-post-type.php template in whatever way you like (most likely you will just use a page template) but replace the loop part with
$recent = new WP_Query("page_id=your-page-id"); while($recent->have_posts()) : $recent->the_post(); the_content(); endwhile;
Then when you'll go to http://my-site.com/visningshus youll see the content managed by the page at the correct url.
Let me know it it works.
-
Bonneidée,mais c'esttrop compliqué.Avez-vousessayéma solution?Nice idea, but this is overly complicated. Did you try my solution?
- 0
- 2014-02-19
- kraftner
-
@kraftnernon,je ne l'aipasfait.Je vaisessayer laprochainefois.Merci!@kraftner no, I didn't. I'll give it a try next time. Thanks!
- 0
- 2014-02-19
- molokom
-
Cela a également deseffets secondaires comme l'en-têteet toutestencoreen cours de création comme s'il s'agissait d'unepage d'archiveet non d'unepage.Jene conseilleraispas d'utiliser cette solution.This also has some side effects like the header and everything still being created as if it was an archive page and not a page. I wouldn't advise on using that solution.
- 0
- 2014-02-19
- kraftner
-
Et si l'ID depage change?Cen'estpas sûr.What if page ID changes? This is not safe.
- 0
- 2017-02-06
- Joel James
-
- 2017-02-23
Non autorisé à voter,mais +pour la solution Carls,register_post_type devrait avoir
'has_archive' => false
Simple.Cependant,lorsque vous rencontrez desproblèmes depagination,jetez un œil à
add_rewrite_rule
Not allowed to vote, but + for Carls solution, register_post_type should have
'has_archive' => false
Simple. However, when you face pagination problems, take a look at
add_rewrite_rule
-
- 2018-10-05
Ce qui a résolu leproblèmepourmoi était de définir l'archive surfalse,mais également de définir la hiérarchie surfalse lors de la configuration demontype depublicationpersonnalisé!
What Fixed it for me was setting archive as false but also Setting the Hierarchical to false when setting up my custom post type!
-
- 2018-10-08
Voici une solutiongénéralepour donner lapriorité à lapaginationpar rapport au contenu destypes depublicationpersonnaliséspour TOUT slug detype depublicationpersonnalisé.
// prioritize pagination over displaying custom post type content add_action('init', function() { add_rewrite_rule( '(.?.+?)/page/?([0-9]{1,})/?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top' ); });
Outre l'ajout de cetextrait de code,vous devez également vous assurer que votretype depublicationpersonnalisé a
archive = false
et enfin,allez à lapage desparamètres des lienspermanents Paramètres> Lienspermanents (/wp/wp-admin/options-permalink.php)et enregistrez lesparamètres actuelspour actualiser la structure des liens sur lapage.
Here is a general solution to prioritize pagination over custom post types content for ANY custom post type slug.
// prioritize pagination over displaying custom post type content add_action('init', function() { add_rewrite_rule( '(.?.+?)/page/?([0-9]{1,})/?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top' ); });
Besides adding this snippet, you should also make sure that your custom post type has
archive = false
and last but not least go to the Permalinks settings page Settings > Permalinks (/wp/wp-admin/options-permalink.php) and save current settings to refresh links structure on the page.
J'ai untype demessagepersonnalisé appelé "visningshus",ainsi qu'unepage avec ce slug.C'est comme cela devrait (doit) être.
Actuellement," http://my-site.com/visningshus " répertorietous lesmessages à ce sujettype.Je souhaite afficher lapage contenant ce slug depermalien à laplace.
Commentpuis-jefaireen sorte que Wordpressne fassepaspasser lenom dutype depublication,maisplutôt le lienpermanentet afficher lapage?