¿Es posible obtener un enlace de página de su babosa?

100

¿Es posible obtener el enlace permanente de una página solo con la bala? Soy consciente de que puede obtener el enlace permanente de la página desde el ID utilizando get_page_link() :

<a href="<?php echo get_page_link(40); ?>">Map</a>

Tengo curiosidad por saber si hay alguna forma de hacer lo mismo con la bala de una página, como esta:

<a href="<?php echo get_page_link('map'); ?>">Map</a>
    
pregunta Sampson 07.12.2010 - 18:09

6 respuestas

155

Estás hablando de páginas ¿verdad? No Mensajes.

Es esto lo que buscas:

  1. get_permalink( get_page_by_path( 'map' ) )
  2. get_permalink( get_page_by_title( 'Map' ) )
  3. home_url( '/map/' )
respondido por el zeo 07.12.2010 - 19:19
8

Creo que esto podría ser mejor:

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;
}

siguiendo el patrón de "original" get_page_by_title of wordpress . (línea 3173)

rgds

    
respondido por el Matheus Eduardo 28.02.2011 - 16:21
6

Este es un método publicado por Tom McFarlin en su 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;
}

Funciona con tipos de mensajes personalizados y tipos de mensajes integrados (como post y page ).

    
respondido por el shea 31.01.2013 - 22:19
1

la respuesta aceptada es incorrecta porque las páginas jerárquicas no funcionan así. En pocas palabras, la bala no siempre es la ruta de la página o publicación. P.ej. su página tiene un elemento secundario, etc. la ruta será parent-slug/child-slug y get_page_by_path no podrá encontrar child-slug de esta manera. La solución adecuada es esta:

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>
    
respondido por el Toskan 02.09.2017 - 00:10
0
    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

Usa esta función por

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
    
respondido por el user46487 11.11.2014 - 05:49
0

Prueba esto:

<a href="<?php echo get_page_link( get_page_by_path( 'map' ) ); ?>">Map</a>

get_page_by_path( 'path' ) devuelve el objeto page / post que luego puede ser usado por get_page_link() , ya que acepta el objeto post / page y devuelve el enlace permanente.

    
respondido por el Sigma Wadbude 26.02.2018 - 12:54

Lea otras preguntas en las etiquetas