Estas en: Home > single

Entradas etiquetadas con single

Gráfico de datos estructurados

Corregir los errores de microformatos en WordPress

0

Como ya os comente en otro post, estoy haciendo una actualización del tema Mystique, el cual lleva sin actualizarse cerca de dos años. Esto me ha llevado a descubrir algunos fallos en el tema que hay que corregir. Uno de ellos es un problema con los microformatos.

Al utilizar la herramienta para Webmaster de Google, en la sección «Aspecto de la búsqueda«, apartado «Datos estructurados» nos aparecerá algo como lo de la siguiente imagen:

Gráfico de datos estructurados

Gráfico de datos estructurados

Continue reading “Corregir los errores de microformatos en WordPress” »

Cron Job WordPress

WordPress 3.x para desarrolladores: Temas y plantillas, single.php y comments.php

0

Vamos con dos plantillas más, esta vez las que generan la página del post (single.php) y la de los comentarios (comment.php).

 

SINGLE.PHP

Creamos el archivo single.php y añadimos el siguiente código:

  1. <?php get_header(); ?>
  2.  
  3. <div id="primary">
  4. <div id="content" role="main">
  5.  
  6. <?php while ( have_posts() ) : the_post(); ?>
  7.  
  8. <nav id="nav-single">
  9. <h3 class="assistive-text"><?php _e( 'Post navigation', 'newtheme' ); ?></h3>
  10. <span class="nav-previous"><?php previous_post_link( '%link', __( '<span class="meta-nav">&larr;</span> Previous', 'newtheme' ) ); ?></span>
  11. <span class="nav-next"><?php next_post_link( '%link', __( 'Next <span class="meta-nav">&rarr;</span>', 'newtheme' ) ); ?></span>
  12. </nav><!-- #nav-single -->
  13.  
  14. <?php get_template_part( 'content', 'single' ); ?>
  15.  
  16. <?php comments_template( '', true ); ?>
  17.  
  18. <?php endwhile; // end of the loop. ?>
  19.  
  20. </div><!-- #content -->
  21. </div><!-- #primary -->
  22.  
  23. <?php get_footer(); ?>

Como puedes ver la estructura de este archivo es muy parecida a la de index.php, tenemos una llamada get_header() para crear la cabecera y otra a get_footer() para crear el pie. En medio encontramos el bloque que generará el html para mostrar el post. Tenemos el mismo bucle que teníamos en la página principal además de dos funciones que generan los links hacia el post anterior y el posterior: previous_post_link() y next_post_link().

Se hace una llamada a get_template_part() que cargará la plantilla content-single.php.

Por último carga la plantilla de comentarios con la función comments_template().

 

COMMENTS.PHP

Crea el archivo comments.php y añade el siguiente código:

 

 

  1. <div id="comments">
  2. <?php if ( post_password_required() ) : ?>
  3. <p class="nopassword"><?php _e( 'This post is password protected. Enter the password to view any comments.', 'newtheme' ); ?></p>
  4. </div><!-- #comments -->
  5. <?php
  6. /* Stop the rest of comments.php from being processed,
  7. * but don't kill the script entirely -- we still have
  8. * to fully load the template.
  9. */
  10. return;
  11. endif;
  12. ?>
  13.  
  14. <?php // You can start editing here -- including this comment! ?>
  15.  
  16. <?php if ( have_comments() ) : ?>
  17. <h2 id="comments-title">
  18. <?php
  19. printf( _n( 'One thought on &ldquo;%2$s&rdquo;', '%1$s thoughts on &ldquo;%2$s&rdquo;', get_comments_number(), 'newtheme' ),
  20. number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' );
  21. ?>
  22. </h2>
  23.  
  24. <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
  25. <nav id="comment-nav-above">
  26. <h1 class="assistive-text"><?php _e( 'Comment navigation', 'newtheme' ); ?></h1>
  27. <div class="nav-previous"><?php previous_comments_link( __( '&larr; Older Comments', 'newtheme' ) ); ?></div>
  28. <div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', 'newtheme' ) ); ?></div>
  29. </nav>
  30. <?php endif; // check for comment navigation ?>
  31.  
  32. <ol class="commentlist">
  33. <?php
  34. /* Loop through and list the comments. Tell wp_list_comments()
  35. * to use newtheme_comment() to format the comments.
  36. * If you want to overload this in a child theme then you can
  37. * define newtheme_comment() and that will be used instead.
  38. * See newtheme_comment() in newytheme/functions.php for more.
  39. */
  40. wp_list_comments( array( 'callback' => 'newtheme_comment' ) );
  41. ?>
  42. </ol>
  43.  
  44. <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
  45. <nav id="comment-nav-below">
  46. <h1 class="assistive-text"><?php _e( 'Comment navigation', 'newtheme' ); ?></h1>
  47. <div class="nav-previous"><?php previous_comments_link( __( '&larr; Older Comments', 'newtheme' ) ); ?></div>
  48. <div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', 'newtheme' ) ); ?></div>
  49. </nav>
  50. <?php endif; // check for comment navigation ?>
  51.  
  52. <?php
  53. /* If there are no comments and comments are closed, let's leave a little note, shall we?
  54. * But we don't want the note on pages or post types that do not support comments.
  55. */
  56. elseif ( ! comments_open() && ! is_page() && post_type_supports( get_post_type(), 'comments' ) ) :
  57. ?>
  58. <p class="nocomments"><?php _e( 'Comments are closed.', 'newtheme' ); ?></p>
  59. <?php endif; ?>
  60.  
  61. <?php comment_form(); ?>
  62.  
  63. </div><!-- #comments -->

 

Vamos a ver este código poco a poco:

  1. <div id="comments">
  2. <?php if ( post_password_required() ) : ?>
  3. <p class="nopassword"><?php _e( 'This post is password protected. Enter the password to view any comments.', 'newtheme' ); ?></p>
  4. </div><!-- #comments -->
  5. <?php
  6. /* Stop the rest of comments.php from being processed,
  7. * but don't kill the script entirely -- we still have
  8. * to fully load the template.
  9. */
  10. return;
  11. endif;
  12. ?>

 

En primer lugar comprobamos si los comentarios están protegidos por contraseña, si es así se devuelve un return, que evitará que el resto de la plantilla se procese, pero se continuará ejecutando el script.

En el caso de que los comentarios no estén protegidos por contraseña, se comprueba que el post tenga comentarios utilizando la función have_comments(), si es que sí los tiene se genera el html correspondiente.

  1. <?php if ( have_comments() ) : ?>
  2. <h2 id="comments-title">
  3. <?php
  4. printf( _n( 'One thought on &ldquo;%2$s&rdquo;', '%1$s thoughts on &ldquo;%2$s&rdquo;', get_comments_number(), 'newtheme' ),
  5. number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' );
  6. ?>
  7. </h2>

Como ves, aquí se comprueba si hay comentarios, en caso positivo se crea la cabecera de los comentarios.

  1. <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
  2. <nav id="comment-nav-above">
  3. <h1 class="assistive-text"><?php _e( 'Comment navigation', 'newtheme' ); ?></h1>
  4. <div class="nav-previous"><?php previous_comments_link( __( '&larr; Older Comments', 'newtheme' ) ); ?></div>
  5. <div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', 'newtheme' ) ); ?></div>
  6. </nav>
  7. <?php endif; // check for comment navigation ?>

Se crea el sistema de navegación de los comentarios, para mostrar los comentarios más recientes o los más antiguos.

  1. <ol class="commentlist">
  2. <?php
  3. /* Loop through and list the comments. Tell wp_list_comments()
  4. * to use newtheme_comment() to format the comments.
  5. * If you want to overload this in a child theme then you can
  6. * define newtheme_comment() and that will be used instead.
  7. * See newtheme_comment() in newytheme/functions.php for more.
  8. */
  9. wp_list_comments( array( 'callback' => 'newtheme_comment' ) );
  10. ?>
  11. </ol>

Añadimos los comentarios utilizando una llamada a wp_list_comments(), en el que se indica que procese los datos de los comentarios utilizando la función newtheme_comment que encontrará en el archivo functions.php.

Para ver otros argumentos que se le pueden indicar a la función wp_list_comments() ver http://codex.wordpress.org/Function_Reference/wp_list_comments

  1. <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
  2. <nav id="comment-nav-below">
  3. <h1 class="assistive-text"><?php _e( 'Comment navigation', 'newtheme' ); ?></h1>
  4. <div class="nav-previous"><?php previous_comments_link( __( '&larr; Older Comments', 'newtheme' ) ); ?></div>
  5. <div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', 'newtheme' ) ); ?></div>
  6. </nav>
  7. <?php endif; // check for comment navigation ?>

Volvemos a crear el sistema de navegación de los comentarios, pero esta vez al finalizar la lista de comentarios.

  1. <?php
  2. /* If there are no comments and comments are closed, let's leave a little note, shall we?
  3. * But we don't want the note on pages or post types that do not support comments.
  4. */
  5. elseif ( ! comments_open() && ! is_page() && post_type_supports( get_post_type(), 'comments' ) ) :
  6. ?>
  7. <p class="nocomments"><?php _e( 'Comments are closed.', 'newtheme' ); ?></p>
  8. <?php endif; ?>
  9.  
  10. <?php comment_form(); ?>
  11.  
  12. </div><!-- #comments -->

Por último mostramos un mensaje en el caso de que los comentarios estén cerrados, además, mostramos el formulario para enviar un comentario.

El reno Renardo – Crecí en los ochenta

0

[youtube]http://www.youtube.com/watch?v=sTZMHhHMgAc[/youtube]

Ir arriba