Foros del Web » Creando para Internet » Sistemas de gestión de contenidos » WordPress »

No reconoce una categoría en un bucle

Estas en el tema de No reconoce una categoría en un bucle en el foro de WordPress en Foros del Web. Tengo este código, lo que hace es que en el single.php muestra una plantilla diferente para ciertas categorías. Es decir le digo que si la ...
  #1 (permalink)  
Antiguo 10/01/2013, 13:40
 
Fecha de Ingreso: diciembre-2008
Mensajes: 738
Antigüedad: 15 años, 4 meses
Puntos: 15
No reconoce una categoría en un bucle

Tengo este código, lo que hace es que en el single.php muestra una plantilla diferente para ciertas categorías. Es decir le digo que si la categoría es "colaboradores", use tal loop, si es "carton" use otro, y si es cualquier otra categoría me muestre otro.

El problema es que la categoría "carton" no la reconoce y me muestra la categoría por defecto, mientras que para "colaboradores" si arranca el código del loop asignado. ¿Por qué será?

Aquí está el código, puede parecer largo pero ya explicado es fácil de entender.

Código PHP:
Ver original
  1. <?php
  2. ?>
  3.  
  4. <?php get_header(); ?>
  5.         <!-------PRINCIPAL-------->
  6.         <div id="contenido">
  7.             <div class="contpost">
  8.        
  9.               <?php if (in_category('colaboradores')) { ?>
  10.               <?php if (have_posts()) : ?>
  11.                  <?php while (have_posts()) : the_post(); ?>
  12.                     <?php foreach((get_the_category()) as $category) {
  13.                         $categoria = $category->cat_ID . ' ';
  14.                     }?>
  15.                     <h1 class="single"><?php the_title(); ?></h1>
  16.                     <p class="author3"><?php the_author_posts_link(); ?></p>
  17.                     <div class="postauthor"><b><?php the_time('l j F Y g:i a') ?></b>&nbsp;&nbsp;<?php edit_post_link('Editar'); ?></div>
  18.                     <p><?php the_content(); ?></p>
  19.                    
  20.                 <?php endwhile; ?>
  21.                   <div class="navigation"><p><?php posts_nav_link(); ?></p></div>
  22.                   <?php endif; ?>
  23.                   <?php wp_reset_query(); ?>
  24.                   <div class="clear"></div>
  25.                  
  26.                   <?php comments_template(); ?>  
  27.              
  28.                
  29.                 <?php if (in_category('carton')) { ?>
  30.                 <?php if (have_posts()) : ?>
  31.                  <?php while (have_posts()) : the_post(); ?>
  32.                  <?php $do_not_duplicate = $post->ID; ?>
  33.                     <?php foreach((get_the_category()) as $category) {
  34.                         $categoria = $category->cat_ID . ' ';
  35.                     }?>
  36.                     <h1 class="single"><?php the_title(); ?></h1>
  37.                     <div class="postauthor"><b><?php the_time('l j F Y g:i a') ?></b>&nbsp;&nbsp;<?php edit_post_link('Editar'); ?></div>
  38.                     <p><?php the_content(); ?></p>
  39.                     <div class="postclear">
  40.                                 <div class="postsocial">
  41.                                     <div class="fb">
  42.                                         <iframe src="//www.facebook.com/plugins/like.php?href=<?php the_permalink(); ?>&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=true&amp;font&amp;colorscheme=light&amp;action=like&amp;height=21&amp;appId=134075732530" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe>
  43.                                     </div>
  44.                                     <div class="tw">
  45.                                         <a href="http://twitter.com/share" data-url="<?php the_permalink(); ?>" class="twitter-share-button" data-text="Un freno a las emociones presidenciales" data-count="horizontal" data-via="elcerebrohabla" data-related="elcerebrohabla:Siguenos" data-lang="es">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
  46.                                     </div>
  47.                                     <div class="gl">
  48.                                         <div class="g-plusone" data-size="medium"></div>
  49.                                     </div>
  50.                                     <div class="li">
  51.                                         <script src="//platform.linkedin.com/in.js" type="text/javascript"></script>
  52. <script type="IN/Share" data-url="<?php the_permalink(); ?>" data-counter="right"></script>
  53.                                     </div>
  54.                                 </div>
  55.                             </div>
  56.                 <?php endwhile; ?>
  57.                   <div class="navigation"><p><?php posts_nav_link(); ?></p></div>
  58.                   <?php endif; ?>
  59.                   <?php wp_reset_query(); ?>
  60.                   <div class="estcabezalchico">
  61.                     <h1>Anteriores</h1>
  62.                   </div>
  63.                   <?php $events_query = new WP_Query("cat=15&posts_per_page=12"); ?>
  64.  
  65.                   <?php if(have_posts()) : ?><?php while($events_query->have_posts()) : $events_query->the_post(); ?>
  66.                     <?php if( $post->ID == $do_not_duplicate ) continue; ?>
  67.                         <div class="cuadrorelated">
  68.                             <div class="imagenrelated">
  69.                                 <?php if(has_post_thumbnail()) { ?>
  70.                                     <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('posthumb'); ?></a>
  71.                                 <?php } else {?>
  72.                                     <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/hemeroteca.jpg" /></a>
  73.                                 <?php } ?>
  74.                             </div>
  75.                         </div>
  76.                   <?php endwhile; ?>
  77.                   <?php endif; ?>
  78.                   <div class="clear"></div>
  79.                  
  80.                   <?php comments_template(); ?>  
  81.                 <?php } ?>
  82.                    
  83.                 <?php } else { ?>
  84.                  <?php if (have_posts()) : ?>
  85.                  <?php while (have_posts()) : the_post(); ?>
  86.                  <?php $do_not_duplicate = $post->ID; ?>
  87.                     <?php foreach((get_the_category()) as $category) {
  88.                         $categoria = $category->cat_ID . ' ';
  89.                     }?>
  90.                     <h1 class="single"><?php the_title(); ?></h1>
  91.                     <div class="postauthor"><b><?php the_time('l j F Y g:i a') ?></b>&nbsp;&nbsp;<?php edit_post_link('Editar'); ?></div>
  92.                     <p><?php the_content(); ?></p>
  93.                    
  94.                 <?php endwhile; ?>
  95.                   <div class="navigation"><p><?php posts_nav_link(); ?></p></div>
  96.                   <?php endif; ?>
  97.                   <?php wp_reset_query(); ?>
  98.                   <div class="estcabezalchico">
  99.                     <h1>Relacionadas</h1>
  100.                   </div>
  101.                   <?php $events_query = new WP_Query("cat=15&posts_per_page=12"); ?>
  102.  
  103.                   <?php if(have_posts()) : ?><?php while($events_query->have_posts()) : $events_query->the_post(); ?>
  104.                     <?php if( $post->ID == $do_not_duplicate ) continue; ?>
  105.                         <div class="cuadrorelated">
  106.                             <div class="imagenrelated">
  107.                                 <?php if(has_post_thumbnail()) { ?>
  108.                                     <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('posthumb'); ?></a>
  109.                                 <?php } else {?>
  110.                                     <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/hemeroteca.jpg" /></a>
  111.                                 <?php } ?>
  112.                                 <h1 class="negrocentro"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
  113.                             </div>
  114.                         </div>
  115.                   <?php endwhile; ?>
  116.                   <?php endif; ?>
  117.                   <div class="clear"></div>
  118.                  
  119.                   <?php comments_template(); ?>            
  120.                 <?php } ?>
  121.              
  122.           </div>  
  123.           </div>
  124.         <?php include (TEMPLATEPATH . '/sidebar2.php'); ?>
  125.                
  126.  
  127.  
  128. <?php get_footer(); ?>
  #2 (permalink)  
Antiguo 10/01/2013, 19:53
Avatar de ramoncito  
Fecha de Ingreso: febrero-2003
Ubicación: Viña del Mar
Mensajes: 224
Antigüedad: 21 años, 2 meses
Puntos: 10
Respuesta: No reconoce una categoría en un bucle

Tienes un problema con la estructura. Traté de ajustarla a como creo que es.

Por favor prueba este código, traté de comentar pero es un poco largo



Código:
<?php get_header(); ?>
	<!-------PRINCIPAL-------->
* * <div id="contenido">
		<div class="contpost">
		<?php if (in_category('colaboradores')) { ?>
			<?php if (have_posts()) : ?>
* * * * * * * * <?php while (have_posts()) : the_post(); ?>
* * * * * * * * * * <h1 class="single"><?php the_title(); ?></h1>
* * * * * * * * * * <p class="author3"><?php the_author_posts_link(); ?></p>
* * * * * * * * * * <div class="postauthor"><b><?php the_time('l j F Y g:i a') ?></b>&nbsp;&nbsp;<?php edit_post_link('Editar'); ?></div>
* * * * * * * * * * <p><?php the_content(); ?></p>
* * * * * * * * * * 
				<?php endwhile; ?>
* * * * * * * * <div class="navigation"><p><?php posts_nav_link(); ?></p></div>
			<?php endif; ?>
* * * * * * <?php wp_reset_query(); ?>
			<div class="clear"></div>
* * * * * * * * * 
			<?php comments_template(); ?> * 
		<?php
        
		// agrega este cierre
		// corresponde a cerrar in_category('colaboradores') 
		} elseif (in_category('carton')) { ?>
			<?php if (have_posts()) : ?>
				<?php while (have_posts()) : the_post(); ?>
				<?php $do_not_duplicate = $post->ID; ?>
* * * * * * * * <h1 class="single"><?php the_title(); ?></h1>
* * * * * * * * <div class="postauthor"><b><?php the_time('l j F Y g:i a') ?></b>&nbsp;&nbsp;<?php edit_post_link('Editar'); ?></div>
				<p><?php the_content(); ?></p>
* * * * * * * * <div class="postclear">
* * * * * * * * * * * * * * * * <div class="postsocial">
* * * * * * * * * * * * * * * * * * <div class="fb">
* * * * * * * * * * * * * * * * * * * * <iframe src="//www.facebook.com/plugins/like.php?href=<?php the_permalink(); ?>&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=true&amp;font&amp;colorscheme=light&amp;action=like&amp;height=21&amp;appId=134075732530" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe>
* * * * * * * * * * * * * * * * * * </div>
* * * * * * * * * * * * * * * * * * <div class="tw">
* * * * * * * * * * * * * * * * * * * * <a href="http://twitter.com/share" data-url="<?php the_permalink(); ?>" class="twitter-share-button" data-text="Un freno a las emociones presidenciales" data-count="horizontal" data-via="elcerebrohabla" data-related="elcerebrohabla:Siguenos" data-lang="es">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
* * * * * * * * * * * * * * * * * * </div>
* * * * * * * * * * * * * * * * * * <div class="gl">
* * * * * * * * * * * * * * * * * * * * <div class="g-plusone" data-size="medium"></div>
* * * * * * * * * * * * * * * * * * </div>
* * * * * * * * * * * * * * * * * * <div class="li">
* * * * * * * * * * * * * * * * * * * * <script src="//platform.linkedin.com/in.js" type="text/javascript"></script>
<script type="IN/Share" data-url="<?php the_permalink(); ?>" data-counter="right"></script>
* * * * * * * * * * * * * * * * * * </div>
* * * * * * * * * * * * * * * * </div>
* * * * * * * * * * * * * * </div>
* * * * * * * * <?php endwhile; ?>
				<div class="navigation"><p><?php posts_nav_link(); ?></p></div>
			<?php endif; ?>
* * * * * * <?php wp_reset_query(); ?>
		
		<div class="estcabezalchico">
* * * * * * <h1>Anteriores</h1>
* * * * * * * * * </div>
* * * * * * * * * <?php $events_query = new WP_Query("cat=15&posts_per_page=12"); ?>
*
* * * * * * * * * <?php if(have_posts()) : ?>
					<?php while($events_query->have_posts()) : $events_query->the_post(); ?>
* * * * * * * * * * <?php if( $post->ID == $do_not_duplicate ) continue; ?>
* * * * * * * * * * * * <div class="cuadrorelated">
* * * * * * * * * * * * * * <div class="imagenrelated">
* * * * * * * * * * * * * * * * <?php if(has_post_thumbnail()) { ?>
* * * * * * * * * * * * * * * * * * <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('posthumb'); ?></a>
* * * * * * * * * * * * * * * * <?php } else {?>
* * * * * * * * * * * * * * * * * * <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/hemeroteca.jpg" /></a>
* * * * * * * * * * * * * * * * <?php } ?>
* * * * * * * * * * * * * * </div>
* * * * * * * * * * * * </div>
* * * * * * * * * <?php endwhile; ?>
* * * * * * * * * <?php endif; ?>
* * * * * * * * * <div class="clear"></div>
* * * * * * * * * 
* * * * * * * * * <?php comments_template(); ?> *
		<?php
		} else { // cierre de in_category('carton')
		?>* * * * * * 
* * * * * * * * *<?php if (have_posts()) : ?>
* * * * * * * * *<?php while (have_posts()) : the_post(); ?>
* * * * * * * * *<?php $do_not_duplicate = $post->ID; ?>
* * * * * * * * * * <h1 class="single"><?php the_title(); ?></h1>
* * * * * * * * * * <div class="postauthor"><b><?php the_time('l j F Y g:i a') ?></b>&nbsp;&nbsp;<?php edit_post_link('Editar'); ?></div>
* * * * * * * * * * <p><?php the_content(); ?></p>
* * * * * * * * * * 
* * * * * * * * <?php endwhile; ?>
* * * * * * * * * <div class="navigation"><p><?php posts_nav_link(); ?></p></div>
* * * * * * * * * <?php endif; ?>
* * * * * * * * * <?php wp_reset_query(); ?>
* * * * * * * * * <div class="estcabezalchico">
* * * * * * * * * * <h1>Relacionadas</h1>
* * * * * * * * * </div>
* * * * * * * * * <?php $events_query = new WP_Query("cat=15&posts_per_page=12"); ?>
*
* * * * * * * * * <?php if(have_posts()) : ?><?php while($events_query->have_posts()) : $events_query->the_post(); ?>
* * * * * * * * * * <?php if( $post->ID == $do_not_duplicate ) continue; ?>
* * * * * * * * * * * * <div class="cuadrorelated">
* * * * * * * * * * * * * * <div class="imagenrelated">
* * * * * * * * * * * * * * * * <?php if(has_post_thumbnail()) { ?>
* * * * * * * * * * * * * * * * * * <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('posthumb'); ?></a>
* * * * * * * * * * * * * * * * <?php } else {?>
* * * * * * * * * * * * * * * * * * <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/hemeroteca.jpg" /></a>
* * * * * * * * * * * * * * * * <?php } ?>
* * * * * * * * * * * * * * * * <h1 class="negrocentro"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
* * * * * * * * * * * * * * </div>
* * * * * * * * * * * * </div>
* * * * * * * * * <?php endwhile; ?>
* * * * * * * * * <?php endif; ?>
* * * * * * * * * <div class="clear"></div>
* * * * * * * * * 
* * * * * * * * * <?php comments_template(); ?> * * * * * *
* * * * * * * * <?php } ?>
* * * * * * * 
* * * * * </div> *
* * * * * </div>
* * * * <?php include (TEMPLATEPATH . '/sidebar2.php'); ?>
* * * * * * * * 
*
*
<?php get_footer(); ?>
__________________
:pensando: Ramon Martinez... Chile :adios:
  #3 (permalink)  
Antiguo 14/01/2013, 12:43
 
Fecha de Ingreso: diciembre-2008
Mensajes: 738
Antigüedad: 15 años, 4 meses
Puntos: 15
Respuesta: No reconoce una categoría en un bucle

Gracias, sí me funcionó
  #4 (permalink)  
Antiguo 14/01/2013, 21:09
Avatar de ramoncito  
Fecha de Ingreso: febrero-2003
Ubicación: Viña del Mar
Mensajes: 224
Antigüedad: 21 años, 2 meses
Puntos: 10
Respuesta: No reconoce una categoría en un bucle

Buena!

Hay que tener ojo con los cierres de los If
__________________
:pensando: Ramon Martinez... Chile :adios:

Etiquetas: blog, bucle, php, plugin, post, reconoce, widgets
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:11.