Obtenir le permalien de la page sans wpurl
-
-
** Connexes: ** [Obtenir le lienpermanent sans domaine (c'est-à-dire obtenir le lienpermanent relatif)] (http://wordpress.stackexchange.com/q/63323/10691)**Related:** [Get Permalink without domain (i.e. get relative permalink)](http://wordpress.stackexchange.com/q/63323/10691)
- 0
- 2012-08-28
- its_me
-
4 réponses
- votes
-
- 2011-11-02
Iln'y a rien deintégrépour renvoyer lebit que vous voulez,mais cela devrait être aussi simple que d'utiliser lafonction home_url ()et de supprimer sa sortie de l'url complète,parexemple:
function get_relative_permalink( $url ) { return str_replace( home_url(), "", $url ); }
There's nothing built in to return the bit you want but it should be as easy as using the home_url() function and removing it's output from the full url eg:
function get_relative_permalink( $url ) { return str_replace( home_url(), "", $url ); }
-
Je suis arrivé aumêmebout de codemaisj'espérais déjà unefonction dans lenoyau qui le rendrait commeje le souhaite.I came to the same bit of code but was hoping for some function already in core that returns it the way I want.
- 0
- 2011-11-02
- Scott
-
Celaentraînerapeut-être des valeursinattenduespour lemultisite ou avec WordPressinstallé dans le sous-répertoire.That will result in maybe unexpected values for multisite or with WordPress installed in sub directory.
- 0
- 2016-09-01
- David
-
- 2016-09-01
Il y aen fait unefonctionprincipalepour celamaintenant.
wp_make_link_relative($url)
Convertissez les chemins URL completsen chemins relatifs.
Supprime lesprotocoles http ou httpset le domaine.Conserve le chemin «/» au début,donc cen'estpas un vrai lien relatif,mais de labase racine Web.
<?php echo wp_make_link_relative('http://localhost/wp_test/sample-page/'); ?>
Cela affichera
/wp_test/sample-page/
Exemple avec ID depublication
<?php echo wp_make_link_relative(get_permalink( $post->ID )); ?>
Exemplepour lemessage actuel
<?php echo wp_make_link_relative(get_permalink()); ?>
Pouren savoirplus,consultez la documentation .
There's actually a core function for this now.
wp_make_link_relative($url)
Convert full URL paths to relative paths.
Removes the http or https protocols and the domain. Keeps the path '/' at the beginning, so it isn't a true relative link, but from the web root base.
Example
<?php echo wp_make_link_relative('http://localhost/wp_test/sample-page/'); ?>
This will output
/wp_test/sample-page/
Example with Post ID
<?php echo wp_make_link_relative(get_permalink( $post->ID )); ?>
Example for current post
<?php echo wp_make_link_relative(get_permalink()); ?>
More about this can be found in the documentation.
-
Cette réponse doit êtremarquée comme labonne,carelle utilise unefonction d'aideintégrée de WordPress Core: https://developer.wordpress.org/reference/functions/wp_make_link_relative/This answer should be marked as the correct one, because it uses a built-in helper function of WordPress Core: https://developer.wordpress.org/reference/functions/wp_make_link_relative/
- 2
- 2018-01-22
- sun
-
pas lameilleure décision,car si vous démarrez votre WP dans localhost via MAMPet que vous avezplusieursprojets sur localhost/*,la réponse à `wp_make_link_relative (get_permalink ())` sera `/wp_dir/category_slug`not the best decision, because if you are started your WP in localhost via MAMP and you have multiple projects on localhost/*, the answer to `wp_make_link_relative(get_permalink())` will be `/wp_dir/category_slug`
- 0
- 2019-06-16
- Gediminas
-
- 2011-11-02
Vousne pourrezpas utiliser
get_permalink()
pour cela.Si vous creusez dans le code de cettefonction dans
/wp-includes/link-template.php
,vous comprendrezpourquoi.Unefois la structure dupermalien analyséeet préparée,le codefait ceci:$permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
Ceciesteffectuéimmédiatement après la création de la structure du lienet avant que quoi que ce soitne soitpassé àtravers unfiltre utile.
Donc,malheureusement,vous devrezextraire vous-même lapartienonnécessaire de l'URL.Je recommande d'utiliser lafonction
str_replace()
suggéréepar @sanchothefat.You won't be able to use
get_permalink()
for that.If you dig into the code for that function in
/wp-includes/link-template.php
you'll see why. After the permalink structure is parsed and prepared, the code does this:$permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
This is performed immediately after the structure of the link is created and before anything is passed through a useful filter.
So unfortunately, you'll have to extract the non-needed part of the URL yourself. I'd recommend using the
str_replace()
function that @sanchothefat suggested. -
- 2011-11-02
$path = parse_url(get_permalink(...), PHP_URL_PATH);
... donne uniquement le PATH URL .Cen'estpas relatif à la racine dublogmais au domaine.C'est l'URI absolu.$path = parse_url(get_permalink(...), PHP_URL_PATH);
... gives the URL PATH only. This is not relative to blog root but to domain. It's the absolute URI.
J'ai actuellement l'ID d'unepage dontje souhaite utiliser sonpermalien comme devant d'uneinfrastructure d'un CPT queje metsen place.
Jepeuxmaintenant utiliserget_permalink ()mais cela renvoie l'URL complète:
http://www.example.com/imapage/subpage/subsubpage
maistout ce queje veux retournerest
imapage/subpage/subsubpage
Y a-t-il unefonction quipeutfaire cela ou dois-jeinstaller quelque chose quipeut soustraire lapartienonnécessaire de l'URL?