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

Entradas aleatoria descontrolan los post

Estas en el tema de Entradas aleatoria descontrolan los post en el foro de WordPress en Foros del Web. Hola a todos! Implemente un codigo para entradas aleatorias en mi blog el problema es que las entradas comunes en mi web se descontrolan, osea ...
  #1 (permalink)  
Antiguo 14/02/2014, 19:22
Avatar de Jkey  
Fecha de Ingreso: julio-2012
Mensajes: 28
Antigüedad: 11 años, 8 meses
Puntos: 0
Entradas aleatoria descontrolan los post

Hola a todos! Implemente un codigo para entradas aleatorias en mi blog el problema es que las entradas comunes en mi web se descontrolan, osea también tienen el mismo orden que las aleatorias. Pongo mi codigo:

Código:
get_header(); ?>

	<ul class="random_images_container">

	<?php $posts = get_posts('orderby=rand&numberposts=8'); foreach($posts as $post) { ?>
		<li>
			<div class="random_image">
				 <!-- image -->  
			    <?php the_post_thumbnail(); ?>
			    <!-- description div -->  
			    <div class='random_description'>  
			    <!-- description content -->  		    

			    <?php if(strlen(get_the_title())>28)
				{
					$variabletitle = get_the_title();
					$titulomostrado = substr($variabletitle,0,28);
					$title_random = $titulomostrado."...";
				}else{
					$title_random = get_the_title();
				}

				?>

			    	<div class='random_description_content'><a href="#"><?php echo $title_random; ?></a></div>  
			    <!-- end description content -->  
			    </div>  
			    <!-- end description div -->
			</div>
		</li>
	<?php  } ?>
	</ul>
	

	<div id="content-middle">

		<div id="home_ads">
		</div>

	<div id="primary" class="content-area">
		
		<main id="main" class="site-main" role="main">
		<?php if ( have_posts() ) : ?>

			<?php /* Start the Loop */ ?>

			<?php while ( have_posts() ) : the_post(); ?>
			<div id="home_post_middle">
				<div id="home_post_tumbnail"><?php the_post_thumbnail(); ?></div>
					<?php
						/* Include the Post-Format-specific template for the content.
						 * If you want to override this in a child theme, then include a file
						 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
						 */
						get_template_part( 'content', get_post_format() );
					?>
			</div>

			<?php endwhile; ?>

			<?php next_posts_link( '<div class="nav-previous">Viejas entradas</div>' ); ?>
			<?php previous_posts_link( '<div class="nav-next">Nuevas entradas</div>' ); ?>

		<?php else : ?>

			<?php get_template_part( 'content', 'none' ); ?>

		<?php endif; ?>

		</main><!-- #main -->
	</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
Si yo uso esto para reiniciar los post también queda mal:

Código:
<?php $posts = get_posts('orderby=date'); foreach($posts as $post) { ?>
Probé también con <?php wp_reset_postdata(); ?> y nada, nada funciona las entradas siguen saliendo aleatorias...
Alguien me puede ayudar?


Muchas gracias!
  #2 (permalink)  
Antiguo 15/02/2014, 08:50
Avatar de zanguanga
Moderadora
 
Fecha de Ingreso: julio-2009
Ubicación: España
Mensajes: 1.686
Antigüedad: 14 años, 9 meses
Puntos: 429
Respuesta: Entradas aleatoria descontrolan los post

Si pones el código completo será más fácil. No podemos adivinar cómo has creado el Loop ¿o no lo has creado?
__________________
Mi blog personal | Mi G+
  #3 (permalink)  
Antiguo 16/02/2014, 17:42
Avatar de Jkey  
Fecha de Ingreso: julio-2012
Mensajes: 28
Antigüedad: 11 años, 8 meses
Puntos: 0
Respuesta: Entradas aleatoria descontrolan los post

Cita:
Iniciado por zanguanga Ver Mensaje
Si pones el código completo será más fácil. No podemos adivinar cómo has creado el [URL="http://codex.wordpress.org/The_Loop"]Loop[/URL] ¿o no lo has creado?
Gracias!

Mi index:

Código:
<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package _s
 */

get_header(); ?>

	<ul class="random_images_container">

	<?php $posts = get_posts('orderby=rand&numberposts=8'); foreach($posts as $post) { ?>
		<li>
			<div class="random_image">
				 <!-- image -->  
			    <?php the_post_thumbnail(); ?>
			    <!-- description div -->  
			    <div class='random_description'>  
			    <!-- description content -->  		    

			    <?php if(strlen(get_the_title())>28)
				{
					$variabletitle = get_the_title();
					$titulomostrado = substr($variabletitle,0,28);
					$title_random = $titulomostrado."...";
				}else{
					$title_random = get_the_title();
				}

				?>

			    	<div class='random_description_content'><a href="<?php echo get_permalink(); ?>"><?php echo $title_random; ?></a></div>  
			    <!-- end description content -->  
			    </div>  
			    <!-- end description div -->
			</div>
		</li>
	<?php  } ?>
	</ul>
	
	<div id="content-middle">

		<div id="home_ads">
		</div>

	<div id="primary" class="content-area">
		
		<main id="main" class="site-main" role="main">
		<?php if ( have_posts() ) : ?>

			<?php /* Start the Loop */ ?>

			<?php while ( have_posts() ) : the_post(); ?>
			<div id="home_post_middle">
				<a href="<?php echo get_permalink(); ?>"><div id="home_post_tumbnail"><?php the_post_thumbnail(); ?></div></a>
					<?php
						/* Include the Post-Format-specific template for the content.
						 * If you want to override this in a child theme, then include a file
						 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
						 */
						get_template_part( 'content', get_post_format() );
					?>
			</div>

			<?php endwhile; ?>

			<?php next_posts_link( '<div class="nav-previous">Viejas entradas</div>' ); ?>
			<?php previous_posts_link( '<div class="nav-next">Nuevas entradas</div>' ); ?>

		<?php else : ?>

			<?php get_template_part( 'content', 'none' ); ?>

		<?php endif; ?>

		</main><!-- #main -->
	</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Mi header:

Código:
<?php
/**
 * The Header for our theme.
 *
 * Displays all of the <head> section and everything up till <div id="content">
 *
 * @package _s
 */
?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php wp_title( '|', true, 'right' ); ?></title>
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/random.js"></script>

<?php wp_head(); ?>
</head>

<body <?php body_class(); ?>>
<div id="page" class="hfeed site">

	<header id="masthead" class="site-header" role="banner">
			<div class="text-top"><div class="text-top-center"><span>Esta página deFeed fue personalizada para nuestros lectores en Español. <a href="#">Volver a la versión en ingles</a></span></div></div>
			<div id="title-header"><div id="logo"><a href="<?php echo get_site_url(); ?>">FeeD</a></div></div>
			<!-- <h2 class="site-description"><?php bloginfo( 'description' ); ?></h2> -->
			
			<nav id="site-navigation" role="navigation">
				<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?> 
			</nav><!-- #site-navigation -->
	</header><!-- #masthead -->

	<div id="content" class="site-content">
  #4 (permalink)  
Antiguo 21/02/2014, 15:29
Avatar de zanguanga
Moderadora
 
Fecha de Ingreso: julio-2009
Ubicación: España
Mensajes: 1.686
Antigüedad: 14 años, 9 meses
Puntos: 429
Respuesta: Entradas aleatoria descontrolan los post

Para el random post esta estructura básica (agregando las variaciones que necesites, imágenes, etc) debería funcionarte.

Código PHP:
Ver original
  1. <ul>
  2. <?php
  3. $args = array( 'posts_per_page' => 8, 'orderby' => 'rand' );
  4. $rand_posts = get_posts( $args );
  5. foreach ( $rand_posts as $post ) :
  6.   setup_postdata( $post ); ?>
  7.     <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
  8. <?php endforeach;
  9. wp_reset_postdata(); ?>
  10. </ul>

Lo que te falta, creo yo, es poner fin al primer bucle y restablecer la variable $post. Para ello puede servirte la función wp_reset_postdata(); que estás viendo en la línea 9.
__________________
Mi blog personal | Mi G+

Etiquetas: aleatoria, blog, entradas, php, post, theme
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 08:04.