Problema con mostrar posts de una categoría PHP Wordpress

  • Autor Autor drlitross
  • Fecha de inicio Fecha de inicio
drlitross

drlitross

Delta
Programador
Verificado
Verificación en dos pasos activada
Verificado por Whatsapp
Hola buenas, soy un, programador que trabajo con python y algo de java. Realmente no tengo mucha idea de php y del codex de wordpress. Os explico, quiero mostrar los post de una categoría en concreto con el codigo de abajo (el cual funciona en ambitos generales, muestra todas los post de todas las categorías), pero quiero que muestre tan solo de una en concreto. Mi pregunta es. ¿Como se haría?, imagino que es con <?php is_category ()?> pero realmente no me funciona. Espero vuestra respuesta, gracias anticipadas y un saludo!

PHP:
<div class="clust-container galmode-apps">
                <h2 class="clust-heading">Rantamplan</h2>
                <ol class="card-list">
                    <?php while (have_posts()) { the_post();?>
                    <li>
                        <a href="<?php the_permalink(); ?>" title="<?php the_title();?>" class="card-click-target" aria-hidden="true" tabindex="-1"></a>
                        <div class="icon">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><img src="<?php bloginfo('template_url'); ?>/tt.php?w=100&h=100&src=<?php echo get_the_post_thumbnail_url(get_the_ID(), 'thumbnail'); ?>" alt="<?php the_title();?>" class="gal_listthumb" height="115"></a>
                        </div>
                        <div class="details mt10">
                            <div class="title">
                                <a href="<?php the_permalink(); ?>" title="<?php the_title();?>">
                                    <?php the_title();?>
                                </a>
                            </div>

                           <div align="center"><?php $min=1236; $max=2142; echo rand($min,$max); ?> Clin</div>
							<div align="center"><?php $min=612; $max=1136; echo rand($min,$max); ?> Clan</div>
                            <div class="rating"><span class="<?php
  $input = array("rating-static rating-35", "rating-static rating-40", "rating-static rating-45", "rating-static rating-50");
  $rand_keys = array_rand($input, 2);
  echo $input[$rand_keys[0]];
?>">Rated 45/5</span></div>
                        </div>
                        <div class="download"><a href="/descargar/<?php the_ID(); ?>">Descargar</a></div>
                    </li>
                    <?php } ?>
                </ol>

            </div>
 
[MENTION=47483]drlitross[/MENTION] yo lo eh hecho de esta forma, donde el cat=1 es el id de la categoria y showposts=50 el total de post a mostrar! :encouragement:

PHP:
<?php $recent = new WP_Query('cat=1&showposts=50'); while($recent->have_posts()) : $recent->the_post(); ?>

mas informacion aqui Class Reference/WP Query « WordPress Codex
 
Se cae la web cuando pongo el codigo [MENTION=1597]YeltsinGraphs[/MENTION] Me está costando un montón. Muchas gracias por tu respuesta!
 
Esto te debe funcionar, cambiar 10 por el numero de posts que quieras y 1 por el id de la categoria que necesites:
PHP:
$args=array(
    'posts_per_page' => 10, 
    'category' => 1,
);
$cats = new WP_Query( $args );
if ( $cats->have_posts() ) {
	echo '<ul>';
	while ( $cats->have_posts() ) {
		$cats->the_post();
		echo '<li>' . get_the_title() . '</li>';
	}
	echo '</ul>';
	wp_reset_postdata();
} else {
	// no posts found
}

Se cae la web cuando pongo el codigo [MENTION=1597]YeltsinGraphs[/MENTION] Me está costando un montón. Muchas gracias por tu respuesta!

- - - Actualizado - - -

Si quieres utilizar el ejemplo del otro usuario aqui esta el codigo, se la paso agregar el final del while por eso no funcionaba :
PHP:
$recent = new WP_Query('cat=1&showposts=10'); while($recent->have_posts()) : $recent->the_post(); endwhile;
 
Última edición:
Se cae la web cuando pongo el codigo [MENTION=1597]YeltsinGraphs[/MENTION] Me está costando un montón. Muchas gracias por tu respuesta!

Seria asi!

PHP:
<div class="clust-container galmode-apps">
                <h2 class="clust-heading">Rantamplan</h2>
                <ol class="card-list">

                    <?php $recent = new WP_Query('cat=1&showposts=50'); while($recent->have_posts()) : $recent->the_post(); ?>

                    <li>
                        <a href="<?php the_permalink(); ?>" title="<?php the_title();?>" class="card-click-target" aria-hidden="true" tabindex="-1"></a>
                        <div class="icon">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><img src="<?php bloginfo('template_url'); ?>/tt.php?w=100&h=100&src=<?php echo get_the_post_thumbnail_url(get_the_ID(), 'thumbnail'); ?>" alt="<?php the_title();?>" class="gal_listthumb" height="115"></a>
                        </div>
                        <div class="details mt10">
                            <div class="title">
                                <a href="<?php the_permalink(); ?>" title="<?php the_title();?>">
                                    <?php the_title();?>
                                </a>
                            </div>

                           <div align="center"><?php $min=1236; $max=2142; echo rand($min,$max); ?> Clin</div>
                            <div align="center"><?php $min=612; $max=1136; echo rand($min,$max); ?> Clan</div>
                            <div class="rating"><span class="<?php
  $input = array("rating-static rating-35", "rating-static rating-40", "rating-static rating-45", "rating-static rating-50");
  $rand_keys = array_rand($input, 2);
  echo $input[$rand_keys[0]];
?>">Rated 45/5</span></div>
                        </div>
                        <div class="download"><a href="/descargar/<?php the_ID(); ?>">Descargar</a></div>
                    </li>

                    <?php endwhile; ?>

                </ol>

            </div>
 
Muchisimas gracias a ambos [MENTION=26720]emoski[/MENTION] y [MENTION=1597]YeltsinGraphs[/MENTION] . Funcionan correctamente ambos códigos, gracias por vuestra paciencia y consideración!
 
Atrás
Arriba