Obtenir un permalien sans domaine (c'est-à-dire obtenir un permalien relatif)
-
-
Oùessayez-vousexactement ce code ...??Utilisez-vous `global $post` avant votre code ci-dessus?Where exactly are you trying this code....?? Are you using `global $post` before your above code?
- 0
- 2012-08-28
- Joshua Abenazer
-
@JoshuaAbenazer J'utilise le codetel qu'ilest dansmonfichier header.php - on dirait que cen'estpas labonnefaçon de lefaire.Jemodifieraima questionpour refléter ce quej'aiessayé,afin que vouspuissiezmieuxm'aider.@JoshuaAbenazer I am using the code as it is in my header.php file — looks like that's the wrong way of doing it. I will edit my question to reflect whatelse I've tried, so that you can help me better.
- 0
- 2012-08-28
- its_me
-
Puis-je vous demanderpourquoi vous voudriez cela?Can I ask why you'd want this?
- 2
- 2012-08-28
- Tom J Nowell
-
@TomJNowell Salut.Je dois utiliser un domaine différentpour cebalisage: ``-mais le lienpermanent actuelest comme ceci:` http://example.com/2012/01/post-title/`.([Lire ceci] (http://support.google.com/webmasters/bin/answer.py?hl=fr&answer=189077) vous donnera unemeilleureidée de ce queje fais.)@TomJNowell Hey. I need to use a different domain for this markup: `` — but the actual permalink is like this: `http://example.com/2012/01/post-title/`. ([Reading this](http://support.google.com/webmasters/bin/answer.py?hl=en&answer=189077) will give you a better idea of what I am doing.)
- 1
- 2012-08-28
- its_me
-
Si vous voulez aller ** à lamanière WP **,vous devez réaliser que `get_permalink ()`est destiné à obtenir desinstances de liens de classe `WP_Post` (articles,pages,types d'articlespersonnalisés,...) ...pour les archives,balises,etc -juste des catégoriesen général - desinstances de la classe `WP_Term` - c'est`get_term_link () `,pour lamaison c'est` home_url () `If you want to go **the WP way**, you must realize `get_permalink()` is for getting instances of class `WP_Post` links (posts, pages, custom post types, ... )... for archives, tags, etc - just categories in general - instances of class `WP_Term` - it is `get_term_link()`, for home it is `home_url()`
- 0
- 2018-03-18
- jave.web
-
3 réponses
- votes
-
- 2012-08-28
Utilisez
$_SERVER['REQUEST_URI']
au lieu deget_permalink()
pour récupérer l'URL actuelle.get_permalink
vous donnera l'adresse complète de lapublication actuelle,et non l'adresse de l'URL visitée.parexempleparexemple.com/test/page
echo $_SERVER['REQUEST_URI'];
imprime/test/page
Notez que celan'inclutpas le hashtag,car cettepartien'estjamaisenvoyée au serveur,et ellen'inclutpasnonplus lesparamètres detype
?foo=bar
,ceux-ci sont dans le$_GET
.Use
$_SERVER['REQUEST_URI']
instead ofget_permalink()
to grab the current URL.get_permalink
will give you the full address of the current post, not the address of the URL visited.e.g. for example.com/test/page
echo $_SERVER['REQUEST_URI'];
prints/test/page
Note that this doesn't include the hashtag, as that part never gets sent to the server, and it also doesn't include
?foo=bar
type parameters, those are in the$_GET
array.-
Marcheparfaitement!!!Petite correction - le deuxième coden'apas deguillemet.`echo $ _SERVER ['REQUEST_URI'];` -et merci!Works perfectly!!! Small correction — the second code is missing a quote. `echo $_SERVER['REQUEST_URI'];` — and thanks!
- 0
- 2012-08-28
- its_me
-
@TomJNowell Sije neme trompepas,$ _SERVER ['REQUEST_URI'] vous donnera l'URL * actuelle *,ce quipeutposerproblème s'il s'agit d'un articlepaginé,ce qui signifie que/test/page/2 sera récupéré lors de l'utilisation deget_permalink() serait la solution à ceproblème.Ai-je un sens?@TomJNowell If I am not mistaken, $_SERVER['REQUEST_URI'] will give you the *current* URL, which may be a problem if it is a paginated post, meaning that /test/page/2 will be grabbed while using get_permalink() would be the solution for this issue. Am I making sense?
- 0
- 2014-06-10
- Christine Cooper
-
Ce quiest similaire auproblème des auteurs d'origine,je devraisfaire quelquestests,mais vouspouveztoujours ajouter votrepropre réponseet si celafonctionne,je voterai=]Which is similar to what the original authors issue was, I would have to do some testing, but you can always add your own answer and if it works I'll upvote =]
- 0
- 2014-06-10
- Tom J Nowell
-
Pourquoi cette réponse a-t-elle été acceptée?Ilne répondmêmepas à distance à la question du PO!Why was this answer accepted? It doesn't even remotely answer the OP's question!
- 0
- 2017-07-26
- Jim Maguire
-
@JimMaguireilestpar définition lié à ce que OP a demandé,mais l'auteur de la réponse a oublié dementionner qu'il retourneessentiellement TOUT après le domainejusqu'au hachage (quin'estpasenvoyé au serveurpar conception).* Cen'est donc qu'unpoint de départ,**pas ** la solutionfinale. *@JimMaguire it is defintely related to what OP asked, however answer author forgot to mention that it returns basically EVERYTHING after domain up to hash (which is not sent to server by design). *So it is just a starting point, **not** the final solution.*
- 0
- 2018-03-18
- jave.web
-
** Obtenez une solution unpeufinale: ** ** a) ** `explode ()` la chaînepar `?` Etne gardez que lapremièrepartie` [0] `à supprimer *possible * GET query **b) **run (regexp) replace (`preg_replace ()`)pour supprimertous les "jolisparamètres d'url",leplus souvent `page/N/` - lenuméro depageet d'autresparamètrespossiblespeuvent changer,c'estpourquoi regexp ...parexemple`preg_replace ('/\/page \/\ d + \//','/',$ url)`**Get somewhat final solution:** **a)** `explode()` the string by `?` and keep only the first`[0]` part to remove *possible* GET query **b)** run (regexp) replace (`preg_replace()`) to remove any possible "pretty url parameters", most commonly `page/N/` - page number and other possible params can change, that's why regexp... e.g. `preg_replace( '/\/page\/\d+\//', '/', $url )`
- 0
- 2018-03-18
- jave.web
-
-
Non,ne fonctionnepas lorsque WPest dans un sous-répertoire ...Nope, doesn't work when WP is in a subdirectory...
- 1
- 2014-12-27
- Nabil Kadimi
-
Que voulez-vous dire,quand WPest dans un sous-répertoire?What do you mean, when WP is in a subdirectory?
- 0
- 2017-07-26
- Jim Maguire
-
@JimMaguire `` http://exemple.com/pas-le-répertoire-racine/`@JimMaguire `http://example.com/not-the-root-dir/`
- 2
- 2017-11-08
- Walf
-
-
- 2016-04-22
Celafonctionnepourmoi:
function force_relative_url ($url) { return preg_replace ('/^(http)?s?:?\/\/[^\/]*(\/?.*)$/i', '$2', '' . $url); }
Pour l'utiliser sur unpermalien:
$relative_permalink = force_relative_url (get_permalink ($post->ID));
This works for me:
function force_relative_url ($url) { return preg_replace ('/^(http)?s?:?\/\/[^\/]*(\/?.*)$/i', '$2', '' . $url); }
To use it on a permalink:
$relative_permalink = force_relative_url (get_permalink ($post->ID));
Voici le code quej'utilise:
Ce qu'ilfait,c'est afficher lepermalien sousforme d'URL relative,c'est-à-dire uniquement le slug. Parexemple,si le lienpermanentest
http://example.com/2012/01/post-title/
,l'URL relative sortiepar le code ressemblerait à/2012/01/post-title/
.Problème: Tous lesmessageset toutes lespages affichent lebonpermalien,ce quiesttrèsbien. Maistoutes les autrespages (y compris Accueil,Rechercheet Archives) affichent l'URL relative dupremiermessageet non celle despages respectives. Uneidéepourquoi? Qu'est-ce queje fais demalici?
Référence: Obtenir le lienpermanent de lapage sans wpurl
MODIFIER: Voici ce quej'aiessayé d'autre:
Dansfunctions.php
Dans header.php
Mêmeproblème avec ça aussi. Mais celui-cimontre également uneerreurpas siinformative.