Si el enlace permanente es igual a la URL actual del sitio
28 feb 2013, 18:00:36
Vistas: 13.7K
Votos: 3
¿Existe alguna forma de agregar una clase a un div cuando la URL actual del sitio es igual al enlace permanente de una entrada?
Necesito una clase "current" para mi menú personalizado. Coloqué los enlaces de las entradas en un elemento de lista con un bucle, y para cada enlace quiero verificar si el enlace permanente coincide con la URL de la página actual. Si el enlace permanente coincide con la URL de la página actual, quiero agregar la clase "current".
Algo como:
<?php $currentUrl = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
<?php if ( $currentUrl == the_permalink() ) { echo ' class="current"'; } else {} ?>
Junto con el bucle:
<?php $currentUrl = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
<?php query_posts('orderby=name'); ?>
<?php while (have_posts()) : the_post(); ?>
<li>
<a <?php if ( $currentUrl == the_permalink() ) { echo ' class="current"'; } else {} ?>
href="<?php the_permalink() ?>">
<?php the_title() ?>
</a>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

ElBrm
157
Todas las respuestas a la pregunta
1
1
Haría algo más simple usando el ID:
<?php $current_id = $post->ID; ?>
<?php query_posts('orderby=name'); ?>
<?php while (have_posts()) : the_post(); ?>
<li>
<?php $current_class = ( $current_id == $post->ID ) ? 'class="current"' : ''; ?>
<a <?php if ( $current_class ) echo $current_class; ?> href="<?php the_permalink() ?>">
<?php the_title() ?>
</a>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
No tuve tiempo de verificar el código, espero que esto ayude.

Simon
1.22K
28 feb 2013 18:18:40
Preguntas relacionadas
2
respuestas
5
respuestas
3
respuestas