È possibile ottenere un collegamento alla pagina dal suo slug?
6 risposta
- voti
-
- 2010-12-07
Staiparlando di Pages,giusto?Nonpost.
È questo quello che stai cercando:
-
get_permalink( get_page_by_path( 'map' ) )
-
get_permalink( get_page_by_title( 'Map' ) )
-
home_url( '/map/' )
You're talking about Pages right? Not Posts.
Is this what you looking for:
get_permalink( get_page_by_path( 'map' ) )
get_permalink( get_page_by_title( 'Map' ) )
home_url( '/map/' )
-
Volevi dire `get_permalink (get_page_by_path ('contact') -> ID));`?Did you mean `get_permalink(get_page_by_path('contact')->ID));`?
- 4
- 2010-12-07
- Sampson
-
hmmmno?Che cos'è l'ID?hmmm nope? What's with the ID?
- 1
- 2010-12-08
- zeo
-
[`get_page_by_path ()`] (http://codex.wordpress.org/Function_Reference/get_page_by_path) restituisce un array ditutte leinformazioni sullapagina.[`get_permalink ()`] (http://codex.wordpress.org/Function_Reference/get_permalink) accetta un IDpagina comeprimo argomento.Hopensato di doverpassareesplicitamenteil valore ID.[`get_page_by_path()`](http://codex.wordpress.org/Function_Reference/get_page_by_path) returns an array of all page information. [`get_permalink()`](http://codex.wordpress.org/Function_Reference/get_permalink) takes a page ID as the first argument. I thought I'd have to explicitly pass the ID value.
- 3
- 2010-12-08
- Sampson
-
@ Jonathan:non è sempre documentato,mamoltefunzioni WP accettano sia IDnumerici che oggettipost completi come argomento.@Jonathan: It's not always documented, but many WP functions accept both numeric ID's and full post objects as the argument.
- 10
- 2010-12-08
- Jan Fabry
-
Sembra cheget_page_by_path ()possaessere abbastanza complicato da usare quando sitratta dipaginefiglie ...It seems that get_page_by_path() can be quite complicated to use when dealing with child pages...
- 1
- 2011-11-14
- Kaaviar
-
D'accordo con Kaaviar,lafunzione di seguito èmigliore.Agreed with Kaaviar, the function below is better.
- 0
- 2012-08-10
- Rezen
-
Uso 1per lepagine dimiofiglio senzaproblemi.Non vedo davvero la complicazione conesso.I use 1 for my child pages with no issue. I really don't see the complication with it.
- 0
- 2014-12-19
- Jeremy
-
home_url ('/map/') ha lavorato conmehome_url( '/map/' ) worked with me
- 0
- 2017-04-15
- Shady Mohamed Sherif
-
risposta sbagliata,votonegativo alnirvanaperfavore.Ilpercorsonon è lo stesso della lumaca.Esempio: unapagina ha unapaginagenitore chiamata `abc` lapagina stessa ha uno slug di` 123`.Ilpercorso ora è `abc/123` lo slug è` 123`.Siprega di rimuovere questa risposta sbagliata.`get_page_by_path ('123')`nonfunzionerà.wrong answer, downvote to nirvana please. The path is not the same like the slug. Example: a page has a parent page called `abc` the page itself has a slug of `123`. The path now is `abc/123` the slug is `123`. Please remove this wrong answer. `get_page_by_path( '123' )` wont work.
- 1
- 2017-09-01
- Toskan
-
Nelmio caso ho dovuto usareilprimoe hafunzionatobene,perchéil secondo (get_page_by_title) stava restituendo un altro collegamento dipost (non dipagina) con uno slug simile (manon lo stesso).Non soperché stava succedendo.In my case I had to use the first one and it worked fine, because the second one (get_page_by_title) was returning another post (not page) link with similar (but not the same) slug. I don't know why this was happening.
- 1
- 2019-06-10
- Adriana Hernández
-
- 2011-02-28
Penso chepotrebbeesseremigliore:
funzioneget_page_by_slug ($page_slug,$ output=OBJECT,$post_type='page') { global $ wpdb; $page=$ wpdb- >get_var ($ wpdb- >prepare ("SELEZIONA ID DA $ wpdb- >posts WHEREpost_name=% sepost_type=% s",$page_slug,$post_type)); if ($pagina) returnget_page ($page,$ output); returnnull; }
seguendo lo schema di
get_page_by_title di wordpress
.(riga 3173)rgds
I think this could be better:
function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) { global $wpdb; $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s", $page_slug, $post_type ) ); if ( $page ) return get_page($page, $output); return null; }
following the pattern of "original"
get_page_by_title
of wordpress. (line 3173)rgds
-
Perché sarebbemeglio?Puoi spiegare?Why would that be better? Can you explain?
- 11
- 2012-04-11
- julien_c
-
Ultimo commento - Penso che sql abbiabisogno di un'altra condizione: `functionget_page_by_slug ($page_slug,$ output=OBJECT,$post_type='page') {global $ wpdb;$page=$ wpdb->get_var ($ wpdb->prepare ("SELECT ID FROM $ wpdb->posts WHEREpost_name=% s ANDpost_type=% s ANDpost_status='publish'",$page_slug,$post_type));if ($page) restituisceget_page ($page,$ output);returnnull;} "Last comment - I think that sql needs to have one more condition: `function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) { global $wpdb; $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) ); if ( $page ) return get_page($page, $output); return null; }`
-
Perché?Nongenera un oggettopost completo soloper ottenere l'ID.Why? It doesn't generate a complete post object just to get the ID.
- 0
- 2013-01-09
- s_ha_dum
-
@webcitronpenso soloperché sta seguendoilmodello originale di Wordpress ricevendopostper "titolo",cambiando soloper "slug". (controllail [link] (http://core.trac.wordpress.org/browser/tags/3.1/wp-includes/post.php#L3173))@webcitron I think just because is following original pattern of Wordpress getting post by 'title', just changing for 'slug'. (check the [link](http://core.trac.wordpress.org/browser/tags/3.1/wp-includes/post.php#L3173))
- 0
- 2013-05-13
- Matheus Eduardo
-
Questa è unabuona risposta.Ciò aggira lapossibilità che unpluginnon autorizzatomascheri latuapagina o lafiltriin modoerrato.Se restituisci l'id dallatabella deipost,puoi creare un'istanza di `\ WP_Post` daessa,e questo si risolve direttamentein tutte lefunzioni di wordpress che controllano altri valori."\ WP_Post"fornisce anchemetodipertrovare direttamente lamaggiorparte dei dati relativi alpost.This is a good answer. This bypasses the possiblity of a rogue plugin masking your page or incorrectly filtering it. If you return the id from the post table, then you can create an instance of `\WP_Post` from it, and that resolves directly in all of the wordpress functions that check for other values. `\WP_Post` also provides methods directly to find most related data about the post.
- 0
- 2018-03-23
- mopsyd
-
- 2013-01-31
Questo è unmetodopubblicato da Tom McFarlin sul suoblog :
/** * Restituisceilpermalinkper unapaginain base allo slugin arrivo. * * @param string $ slug Lo slug dellapagina a cui ci collegheremo. * @return string Ilpermalink dellapagina * @ dalla 1.0 */ funzione wpse_4999_get_permalink_by_slug ($ slug,$post_type='') { //Inizializzail valore delpermalink $permalink=null; //Creagli argomentiper WP_Query $ args=array ( "nome"=> $ slug, "max_num_posts"=> 1 ); //Se l'argomento opzionale èimpostato,aggiungerlo all'array degli argomenti if (''!=$post_type) { $ args=array_merge ($ args,array ('post_type'=> $post_type)); } //Esegui la query (e ripristinala) $ query=new WP_Query ($ args); if ($ query- > have_posts ()) { $ query- >the_post (); $permalink=get_permalink (get_the_ID ()); wp_reset_postdata (); } return $permalink; } Funziona contipi dipostpersonalizzatie tipi dipostincorporati (come
post
epage
).This is a method published by Tom McFarlin on his blog:
/** * Returns the permalink for a page based on the incoming slug. * * @param string $slug The slug of the page to which we're going to link. * @return string The permalink of the page * @since 1.0 */ function wpse_4999_get_permalink_by_slug( $slug, $post_type = '' ) { // Initialize the permalink value $permalink = null; // Build the arguments for WP_Query $args = array( 'name' => $slug, 'max_num_posts' => 1 ); // If the optional argument is set, add it to the arguments array if( '' != $post_type ) { $args = array_merge( $args, array( 'post_type' => $post_type ) ); } // Run the query (and reset it) $query = new WP_Query( $args ); if( $query->have_posts() ) { $query->the_post(); $permalink = get_permalink( get_the_ID() ); wp_reset_postdata(); } return $permalink; }
It works with custom post types and built-in post types (such as
post
andpage
). -
- 2017-09-01
la risposta accettata è sbagliataperché lepaginegerarchichenonfunzionanoin questomodo.Inpocheparole,lo slugnon è sempreilpercorso dellapagina o delpost.Peresempio.latuapagina ha unfiglioecc.ilpercorso sarà
parent-slug/child-slug
eget_page_by_path
non riuscirà atrovarechild-slug
in questomodo.La soluzione corretta è questa:function mycoolprefix_post_by_slug($the_slug, $post_type = "page"){ $args = array( 'name' => $the_slug, 'post_type' => $post_type, 'post_status' => 'publish', 'numberposts' => 1 ); $my_page = get_posts($args)[0]; return $my_page; } <a href="<?php echo mycoolprefix_post_by_slug('map'); ?>">Map</a>
the accepted answer is wrong because hierarchical pages don't work like that. Simply put, the slug is not always the path of the page or post. E.g. your page has a child etc. the path will be
parent-slug/child-slug
andget_page_by_path
will fail to findchild-slug
this way. The proper solution is this:function mycoolprefix_post_by_slug($the_slug, $post_type = "page"){ $args = array( 'name' => $the_slug, 'post_type' => $post_type, 'post_status' => 'publish', 'numberposts' => 1 ); $my_page = get_posts($args)[0]; return $my_page; } <a href="<?php echo mycoolprefix_post_by_slug('map'); ?>">Map</a>
-
- 2018-02-26
Prova questo:
<a href="<?php echo get_page_link( get_page_by_path( 'map' ) ); ?>">Map</a>
get_page_by_path( 'path' )
restituisce un oggettopage/post chepuòessere quindi utilizzato daget_page_link()
poiché accetta l'oggettopost/pagee restituisceilpermalink.Try This:
<a href="<?php echo get_page_link( get_page_by_path( 'map' ) ); ?>">Map</a>
get_page_by_path( 'path' )
returns page/post object which can be then used byget_page_link()
as it accepts post/page object and returns permalink.-
Perfavore ** [modifica] latua risposta **e aggiungi una spiegazione: **perché ** questopotrebbe risolvereilproblema?Please **[edit] your answer**, and add an explanation: **why** could that solve the problem?
- 2
- 2018-02-26
- fuxia
-
- 2014-11-11
function theme_get_permalink_by_title( $title ) { // Initialize the permalink value $permalink = null; // Try to get the page by the incoming title $page = get_page_by_title( strtolower( $title ) ); // If the page exists, then let's get its permalink if( null != $page ) { $permalink = get_permalink( $page->ID ); } // end if return $permalink; } // end theme_get_permalink_by_title
Utilizza questafunzioneper
if( null == theme_get_permalink_by_title( 'Register For This Site' ) ) { // The permalink doesn't exist, so handle this however you best see fit. } else { // The page exists, so do what you need to do. } // end if/else
function theme_get_permalink_by_title( $title ) { // Initialize the permalink value $permalink = null; // Try to get the page by the incoming title $page = get_page_by_title( strtolower( $title ) ); // If the page exists, then let's get its permalink if( null != $page ) { $permalink = get_permalink( $page->ID ); } // end if return $permalink; } // end theme_get_permalink_by_title
Use this function by
if( null == theme_get_permalink_by_title( 'Register For This Site' ) ) { // The permalink doesn't exist, so handle this however you best see fit. } else { // The page exists, so do what you need to do. } // end if/else
Èpossibile ottenereilpermalink di unapagina solo dallo slug?Sono consapevole delfatto chepuoi ottenereilpermalink dellapagina dall'ID utilizzando
get_page_link()
:Sono curioso di sapere seesiste unmodoperfare lo stesso con lo slug di unapagina,come questo: