Ver Mensaje Individual
  #3 (permalink)  
Antiguo 11/07/2019, 06:32
bauhaus1975
 
Fecha de Ingreso: agosto-2003
Ubicación: Málaga, España
Mensajes: 419
Antigüedad: 20 años, 8 meses
Puntos: 1
Respuesta: Añadir palabra a los enlaces permanentes

Hola.

Escribo la solución tras haberlo estudiado.

Pongamos como ejemplo que quiero hacer válida la URL: /seccion/{postname} y seccion coincide con el nombre de la categoría del post.

Tras echar un rato de lectura. He añadido una función para el rewrite_url. En el archivo 'functions.php' de mi template (que es Astra). Por lo que añado al final:

Código PHP:
add_action'init''wpa_rewriterules' );

function 
wpa_rewriterules()
{
   
    
add_rewrite_rule(
        
// The regex to match the incoming URL
        
'section/([^/]+)/?',
        
// The resulting internal URL: `index.php` because we still use WordPress
        // `pagename` because we use this WordPress page
        // `designer_slug` because we assign the first captured regex part to this variable
        
'index.php?name=$matches[1]',
        
// This is a rather specific URL, so we add it to the top of the list
        // Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
        
'top' );


También he leído que tendría que añadir de igual forma un filtro a 'the_permalink' para que cada vez que se tenga que mostrar un enlace se transforme a la URL deseada. Por lo que he añadido también en el mismo archivo 'functions.php' el filtro:

Código PHP:
add_filter'post_link''post_permalink_w_section'10); 

function 
post_permalink_w_section$link$post )

    if ( 
$post->post_type === 'post' && has_category('section'$post) )
    {
        
$link str_replace$post->post_name'section/' $post->post_name$link ); 
    } 
    return 
$link;  

P.D. Guardar cambios de los 'permalinks' para hacer un flush de las reglas de URLs.

Última edición por bauhaus1975; 14/07/2019 a las 03:20 Razón: Solución alcanzada