Adding if statements to PHP template - php

I have the below template that I would like to add if statements to in order to determine the layout that is displayed based on the $num variable. However, I cannot get my if, elseif statements to work! (newbie!) Help much appreciated...
This is the current template:
<?php
$count = 1;
$query_args = array('post_type' => 'bunch_team' , 'showposts' => $num,
'order_by' => $sort , 'order' => $order);
if( $cat ) $query_args['team_category'] = $cat;
$query = new WP_Query($query_args) ;
ob_start() ;?>
<?php if($query->have_posts()): ?> <!--Our Team Section-->
<section class="team-section">
<div class="auto-container">
<div class="sec-title">
<h2><?php echo balanceTags($title);?></h2>
<div class="separator"></div>
<div class="heading-text"><?php echo balanceTags($sub_title);?>
</div>
</div>
<div class="row clearfix">
<?php while($query->have_posts()): $query->the_post();
global $post ;
$teams_meta = _WSH()->get_meta();
$post_thumbnail_id = get_post_thumbnail_id($post->ID);
$post_thumbnail_url = wp_get_attachment_url(
$post_thumbnail_id );
?>
<!--Member-->
<article class="col-md-3 col-sm-6 col-xs-12 member-column">
<div class="inner-box">
<figure class="image">
<?php the_post_thumbnail('convo_size_team');?>
</figure>
<div class="member-title">
<h4><?php the_title();?></h4>
<p><?php echo convo_set($teams_meta,
'designation');?></p>
</div>
<div class="member-desc">
<p><?php echo convo_trim(get_the_excerpt(),
$text_limit);?></p>
</div>
<?php if($socials = convo_set($teams_meta,
'bunch_team_social')):?>
<ul class="social-links clearfix">
<?php foreach($socials as $key => $value):?>
<li><a href="<?php echo
esc_url(convo_set($value,
'social_link'));?>" class="fa <?php echo
convo_set($value, 'social_icon');?>">
</a></li>
<?php endforeach;?>
</ul>
<?php endif;?>
</div>
</article>
<?php endwhile;?>
</div>
</div>
</section>
<?php endif; ?>
<?php
wp_reset_postdata();
$output = ob_get_contents();
ob_end_clean();
return $output ; ?>
The article layout is what I would like to change depending on the $num variable. For example, if $num == 2, the article layout should be:
<article class="col-md-6 col-sm-6 col-xs-12 member-column">
<div class="inner-box">
<figure class="image">
<?php the_post_thumbnail('convo_size_team');?>
</figure>
<div class="member-title">
<h4><?php the_title();?></h4>
<p><?php echo convo_set($teams_meta, 'designation');?></p>
</div>
<div class="member-desc">
<p><?php echo convo_trim(get_the_excerpt(), $text_limit);?></p>
</div>
<?php if($socials = convo_set($teams_meta, 'bunch_team_social')):?>
<ul class="social-links clearfix">
<?php foreach($socials as $key => $value):?>
<li></li>
<?php endforeach;?>
</ul>
<?php endif;?>
</div>
</article>
If $num == 3, the article layout should be:
<article class="col-md-4 col-sm-6 col-xs-12 member-column">
<div class="inner-box">
<figure class="image">
<?php the_post_thumbnail('convo_size_team');?>
</figure>
<div class="member-title">
<h4><?php the_title();?></h4>
<p><?php echo convo_set($teams_meta, 'designation');?></p>
</div>
<div class="member-desc">
<p><?php echo convo_trim(get_the_excerpt(), $text_limit);?></p>
</div>
<?php if($socials = convo_set($teams_meta, 'bunch_team_social')):?>
<ul class="social-links clearfix">
<?php foreach($socials as $key => $value):?>
<li></li>
<?php endforeach;?>
</ul>
<?php endif;?>
</div>
</article>
If $num == 4, the article layout should be:
<article class="col-md-3 col-sm-6 col-xs-12 member-column">
<div class="inner-box">
<figure class="image">
<?php the_post_thumbnail('convo_size_team');?>
</figure>
<div class="member-title">
<h4><?php the_title();?></h4>
<p><?php echo convo_set($teams_meta, 'designation');?></p>
</div>
<div class="member-desc">
<p><?php echo convo_trim(get_the_excerpt(), $text_limit);?></p>
</div>
<?php if($socials = convo_set($teams_meta, 'bunch_team_social')):?>
<ul class="social-links clearfix">
<?php foreach($socials as $key => $value):?>
<li></li>
<?php endforeach;?>
</ul>
<?php endif;?>
</div>
</article>
Any help would be greatly appreciated. I know roughly how to do i, but cannot got the if statement to correctly show ONLY the right layout based on the $num variable.
Cheers!

Something like this?
<?php
$count = 1;
$query_args = array(
'post_type' => 'bunch_team',
'showposts' => $num,
'order_by' => $sort,
'order' => $order
);
if ( $cat ) {
$query_args['team_category'] = $cat;
}
$query = new WP_Query( $query_args ); ?>
<?php if($query->have_posts()): ?> <!--Our Team Section-->
<section class="team-section">
<div class="auto-container">
<div class="sec-title">
<h2><?php echo balanceTags($title);?></h2>
<div class="separator"></div>
<div class="heading-text"><?php echo balanceTags($sub_title);?>
</div>
</div>
<div class="row clearfix">
<?php while($query->have_posts()) : $query->the_post();
global $post;
$teams_meta = _WSH()->get_meta();
$post_thumbnail_id = get_post_thumbnail_id($post->ID);
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
if ( $num === 2 ) { // Must be number! Instead use '2'
?>
<article class="col-md-6 col-sm-6 col-xs-12 member-column">
<div class="inner-box">
<figure class="image">
<?php the_post_thumbnail('convo_size_team');?>
</figure>
<div class="member-title">
<h4><?php the_title();?></h4>
<p><?php echo convo_set($teams_meta, 'designation');?></p>
</div>
<div class="member-desc">
<p><?php echo convo_trim(get_the_excerpt(), $text_limit);?></p>
</div>
<?php if($socials = convo_set($teams_meta, 'bunch_team_social')):?>
<ul class="social-links clearfix">
<?php foreach($socials as $key => $value):?>
<li></li>
<?php endforeach;?>
</ul>
<?php endif;?>
</div>
</article>
<?php
} elseif ( $num === 3 ) {
?>
<article class="col-md-4 col-sm-6 col-xs-12 member-column">
<div class="inner-box">
<figure class="image">
<?php the_post_thumbnail('convo_size_team');?>
</figure>
<div class="member-title">
<h4><?php the_title();?></h4>
<p><?php echo convo_set($teams_meta, 'designation');?></p>
</div>
<div class="member-desc">
<p><?php echo convo_trim(get_the_excerpt(), $text_limit);?></p>
</div>
<?php if($socials = convo_set($teams_meta, 'bunch_team_social')):?>
<ul class="social-links clearfix">
<?php foreach($socials as $key => $value):?>
<li></li>
<?php endforeach;?>
</ul>
<?php endif;?>
</div>
</article>
<?php
} elseif ( $num === 4 ) {
?>
<article class="col-md-3 col-sm-6 col-xs-12 member-column">
<div class="inner-box">
<figure class="image">
<?php the_post_thumbnail('convo_size_team');?>
</figure>
<div class="member-title">
<h4><?php the_title();?></h4>
<p><?php echo convo_set($teams_meta, 'designation');?></p>
</div>
<div class="member-desc">
<p><?php echo convo_trim(get_the_excerpt(), $text_limit);?></p>
</div>
<?php if($socials = convo_set($teams_meta, 'bunch_team_social')):?>
<ul class="social-links clearfix">
<?php foreach($socials as $key => $value):?>
<li></li>
<?php endforeach;?>
</ul>
<?php endif;?>
</div>
</article>
<?php
} else {
<article class="col-md-3 col-sm-6 col-xs-12 member-column">
<div class="inner-box">
<figure class="image">
<?php the_post_thumbnail('convo_size_team');?>
</figure>
<div class="member-title">
<h4><?php the_title();?></h4>
<p><?php echo convo_set($teams_meta,
'designation');?></p>
</div>
<div class="member-desc">
<p><?php echo convo_trim(get_the_excerpt(),
$text_limit);?></p>
</div>
<?php if($socials = convo_set($teams_meta,
'bunch_team_social')):?>
<ul class="social-links clearfix">
<?php foreach($socials as $key => $value):?>
<li><a href="<?php echo
esc_url(convo_set($value,
'social_link'));?>" class="fa <?php echo
convo_set($value, 'social_icon');?>">
</a></li>
<?php endforeach;?>
</ul>
<?php endif;?>
</div>
</article>
<?php
}
endwhile;?>
</div>
</div>
</section>
<?php endif;
wp_reset_postdata();
You need to supply $num somehow and be careful if it's number or a string.
Also output buffering is not needed in page templates.

Related

Group <li> in to <ul> for every 6 in a while loop

I have a 'while' statement on my WordPress website which grabs the latest posts. I want to group every 6 posts in an ordered list. Is it possible to wrap the <li> elements in a <ul> for every 6 items?
I haven't tried anything to do this yet as not sure if possible. My code below is.
<?php elseif(get_row_layout() == 'posts_section'): ?>
<section class="content posts">
<div class="row">
<div class="columns medium-12">
<ul class="medium-block-grid-3 small-block-grid-2" data-equalizer>
<?php
$catquery = new WP_Query('cat='. get_sub_field('category') .'');
?>
<?php
while($catquery->have_posts()) : $catquery->the_post();
?>
<li data-equalizer-watch>
<a href="<?php the_permalink() ?>" rel="bookmark">
<div class="overlay"></div>
<div class="content">
<p><?php the_title(); ?></p>
<div class="text-center"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></div>
</div>
</a>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
</div>
</div>
</section>
I cant test the following bit of code since you dont provide any other information, but this is how I would solve this:
<?php elseif(get_row_layout() == 'posts_section'): ?>
<section class="content posts">
<div class="row">
<div class="columns medium-12">
<ul class="medium-block-grid-3 small-block-grid-2" data-equalizer>
<?php
$catquery = new WP_Query('cat='. get_sub_field('category') .'');
?>
<?php
$n = 0; //setting a count
while($catquery->have_posts()) : $catquery->the_post();
if ($n % 6 != 0) {
$n += 1 //keep on increasing till its divisble by 6
?>
<li data-equalizer-watch>
<a href="<?php the_permalink() ?>" rel="bookmark">
<div class="overlay"></div>
<div class="content">
<p><?php the_title(); ?></p>
<div class="text-center"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></div>
</div>
</a>
</li>
<?php
}
if ($n % 6 = 0) {
?>
//end the ul tag here and start a new one
</ul>
<ul class="medium-block-grid-3 small-block-grid-2" data-equalizer>
<?php
}
endwhile;
wp_reset_postdata();
?>
</ul>
</div>
</div>
</section>

Wordpress Pagination on custom Loop

I have some code below, and the pagination seems to be failing after the second page. I'm sure that I've done something wrong on one of the loops.
Can someone sanity check my code below for me?
<?php get_header();?>
<!-- Carousel -->
<div class="container fullwidth">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active" style="background:url(<?php echo esc_url( home_url( '/' ) ); ?>wp-content/uploads/2018/09/testphoto.jpg);">
<div class="container">
<div class="carousel-caption microstory">
<h1>Backstage Blog</h1>
</div>
</div>
</div><!-- End Item -->
</div><!-- End Carousel Inner -->
</div>
</div>
<!-- End Carousel -->
<!-- Posts -->
<div class="container paddingtop paddingbottom">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// are we on page one?
if(1 == $paged) { ?>
<div class="row row-eq-height paddingbottom">
<?php $query = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=1' ); ?>
<?php if ( $query->have_posts() ) :?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col col-lg-6 col-md-6 col-sm-6 col-xs-12 postpaddingbottom">
<div class="col-lg-12 col-xs-12">
<div class="featuredimage"><?php the_post_thumbnail('full'); ?></div>
<div class="blog-column">
<div class="titledatemain">
<h4><?php the_title(); ?></h4>
<ul class="blog-detail">
<li><i class="fa fa-calendar"></i> <?php the_time('F jS, Y'); ?></li>
</ul>
<p><?php the_excerpt(); ?></p>
</div>
Read More
</div>
</div>
</div>
<?php endwhile;?>
<?php endif; ?>
<?php $query2 = new WP_Query( 'post_type=advert&order=DESC&orderby=date&posts_per_page=1' ); ?>
<?php if ( $query2->have_posts() ) :?>
<?php while ( $query2->have_posts() ) : $query2->the_post();
$advert_url = get_post_meta($post->ID, 'url', true);
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<div class="col col-lg-6 col-md-6 col-sm-6 col-xs-12 postpaddingbottom">
<a href="<?php echo $advert_url ?>"><div class="col-lg-12 col-xs-12 advertbox" style="background:url(<?php echo $featured_img_url ?>);">
</div></a>
</div>
<?php endwhile; endif; ?>
</div>
<?php
$query = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=3&offset=1&paged='. $paged ); ?>
<?php if ( $query->have_posts() ) :?>
<div class="row row-eq-height paddingbottom">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col col-lg-4 col-md-4 col-sm-4 col-xs-12 postpaddingbottom">
<div class="col-lg-12 col-xs-12">
<div class="featuredimage blogimagesmall" style="background:url(<?php the_post_thumbnail_url('full'); ?>);"></div>
<div class="blog-colum">
<div class="titledate">
<h4><?php the_title(); ?></h4>
<ul class="blog-detail">
<li><i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?></li>
</ul>
</div>
</div>
</div>
</div>
<?php endwhile;?>
</div>
<?php endif; ?>
<?php } else { ?>
<?php
$query = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=12&offset=4&paged='. $paged ); ?>
<?php if ( $query->have_posts() ) :?>
<div class="row row-eq-height paddingbottom">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col col-lg-4 col-md-4 col-sm-4 col-xs-12 postpaddingbottom">
<div class="col-lg-12 col-xs-12">
<div class="featuredimage blogimagesmall" style="background:url(<?php the_post_thumbnail_url('full'); ?>);"></div>
<div class="blog-colum">
<div class="titledate">
<h4><?php the_title(); ?></h4>
<ul class="blog-detail">
<li><i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?></li>
</ul>
</div>
</div>
</div>
</div>
<?php endwhile;?>
</div>
<?php
wp_reset_postdata();
endif; ?>
<?php } ?>
<div class="row paddingbottom">
<div class="pagenav">
<div class="alignleft"><?php previous_posts_link('Newer Posts', $query- >max_num_pages) ?></div>
<div class="alignright"><?php next_posts_link('Older Posts', $query->max_num_pages) ?></div>
</div>
</div>
</div>
<!-- End Posts -->
<?php get_footer();?>
I'm sure that there is just a mistake in my loop. I have essentially got a different layout on the first page to the rest of the pages (as you will see).
It just seems that after page 2, when you go to the 3rd page, it just keeps displaying the 2nd page content.
Any help would be great.
Here is the updated code:
<?php /* Template Name: Blog Page */ ?>
<?php get_header();?>
<!-- Carousel -->
<div class="container fullwidth">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active" style="background:url(<?php echo esc_url( home_url( '/' ) ); ?>wp-content/uploads/2018/09/testphoto.jpg);">
<div class="container">
<div class="carousel-caption microstory">
<h1>Backstage Blog</h1>
</div>
</div>
</div><!-- End Item -->
</div><!-- End Carousel Inner -->
</div>
</div>
<!-- End Carousel -->
<!-- Posts -->
<div class="container paddingtop paddingbottom">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// are we on page one?
$query = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=12&paged='. $paged );
if(1 == $paged) { ?>
<?php if ( $query->have_posts() ) { ?>
<div class="row row-eq-height paddingbottom">
<?php $temp_query2 = $wp_query2; ?>
<?php $query3 = new WP_Query( 'post_type=post&order=DESC&orderby=date&posts_per_page=1' ); ?>
<?php while ( $query3->have_posts() ) : $query3->the_post(); ?>
<div class="col col-lg-6 col-md-6 col-sm-6 col-xs-12 postpaddingbottom">
<div class="col-lg-12 col-xs-12">
<div class="featuredimage"><?php the_post_thumbnail('full'); ?></div>
<div class="blog-column">
<div class="titledatemain">
<h4><?php the_title(); ?></h4>
<ul class="blog-detail">
<li><i class="fa fa-calendar"></i> <?php the_time('F jS, Y'); ?></li>
</ul>
<p><?php the_excerpt(); ?></p>
</div>
Read More
</div>
</div>
</div>
<?php endwhile; ?>
<?php $wp_query2 = $temp_query2; ?>
<?php $temp_query = $wp_query; ?>
<?php $query2 = new WP_Query( 'post_type=advert&order=DESC&orderby=date&posts_per_page=1' ); ?>
<?php while ( $query2->have_posts() ) : $query2->the_post();
$advert_url = get_post_meta($post->ID, 'url', true);
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<div class="col col-lg-6 col-md-6 col-sm-6 col-xs-12 postpaddingbottom">
<a href="<?php echo $advert_url ?>"><div class="col-lg-12 col-xs-12 advertbox" style="background:url(<?php echo $featured_img_url ?>);">
</div></a>
</div>
<?php endwhile; ?>
<?php $wp_query = $temp_query; ?>
</div>
<div class="row row-eq-height paddingbottom">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col col-lg-4 col-md-4 col-sm-4 col-xs-12 postpaddingbottom">
<div class="col-lg-12 col-xs-12">
<div class="featuredimage blogimagesmall" style="background:url(<?php the_post_thumbnail_url('full'); ?>);"></div>
<div class="blog-colum">
<div class="titledate">
<h4><?php the_title(); ?></h4>
<ul class="blog-detail">
<li><i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?></li>
</ul>
</div>
</div>
</div>
</div>
<?php endwhile;
rewind_posts();
}?>
</div>
<?php } else { ?>
<div class="row row-eq-height paddingbottom">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col col-lg-4 col-md-4 col-sm-4 col-xs-12 postpaddingbottom">
<div class="col-lg-12 col-xs-12">
<div class="featuredimage blogimagesmall" style="background:url(<?php the_post_thumbnail_url('full'); ?>);"></div>
<div class="blog-colum">
<div class="titledate">
<h4><?php the_title(); ?></h4>
<ul class="blog-detail">
<li><i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?></li>
</ul>
</div>
</div>
</div>
</div>
<?php endwhile;?>
</div>
<?php wp_reset_postdata(); ?>
<?php } ?>
<div class="row paddingbottom">
<div class="pagenav">
<div class="alignleft"><?php previous_posts_link('Newer Posts', $query->max_num_pages) ?></div>
<div class="alignright"><?php next_posts_link('Older Posts', $query->max_num_pages) ?></div>
</div>
</div>
</div>
<!-- End Posts -->
<?php get_footer();?>
You should see i'm now running one loop which is nice.. but I have put the first post in its own loop within a loop to show the first post. The issue i'm having now is that I want to on the main loop and only on the first page, have an offset of 1. But I can't work out how to do this just on the first page :(

Wordpress older post page error

I have a problem with a custom wordpress template.
I've made a categories page. Works fine on the firs view.
But when I make click on 2nd page for older posts, the browser shows me a 404 error.
I think my query needs something more, but I don't know what is or if this is the problem.
Can somebody help, please?
This is my code:
<?php
/**
* The template for displaying Category pages
*/
?>
<?php get_header(); ?>
<?php get_template_part( 'partials/linea-content', 'page' ); ?>
<div class="index categories">
<div class="col-xs-12 pre-content">
<div class="container">
<?php //Para añadir contenido se interesa ?>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-xs-12 categories-title">
<h2>Categoria: <span><?php echo get_category(get_query_var('cat'))->name; ?></span></h2>
</div>
<div id="posts" class="col-xs-12 col-sm-9">
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// Cambiamos los argumentos para buscar desde la última
$args = array(
'posts_per_page' => 2,
'orderby' => 'date',
'order' => ASC,
'category__in' => get_category(get_query_var('cat'))->cat_ID,
'paged' => $paged
);
// '&orderby=date&order=ASC&category=' . get_category(get_query_var('cat'))->cat_ID;
// volvemos a crear la consulta principal
$aux = query_posts( $args );
$cont_items = 1;
?>
<?php if ( have_posts() ) : ?>
<div class="row items-row">
<div class="col-xs-12">
<?php while ( have_posts() ) : the_post(); ?>
<!-- post -->
<div id="post-id-<?php echo $post->ID; ?>" class="item col-xs-12 col-sm-6">
<div class="row">
<div class="col-xs-12 text-center item-content">
<span class="meta-category">
<span class="meta-category-inner">
<?php
$cat = get_the_category($post->ID);
?>
<?php echo $cat[0]->name; ?>
</span>
</span>
<?php
global $wpdb;
$ppbv_tablename = $wpdb->prefix . 'popular_by_views';
$currentRow = $wpdb->get_row("SELECT * FROM {$ppbv_tablename} WHERE post_id = {$post->ID}");
$curView = 0;
if(isset($currentRow))
{
$curView = $currentRow->views;
}
?>
<div class="item-title-content">
<a href="<?php the_permalink(); ?>" title="Popsicase">
<h2 class="item-title col-xs-10 col-xs-offset-1"><?php the_title(); ?></h2>
</a>
</div>
<p class="date-cat">
<small class="col-xs-12">
<i class="fa fa-link"></i> <strong><?php the_author(); ?></strong> | <span><i class="fa fa-calendar"></i> <?php echo get_the_date();?> | <i class="fa fa-eye"></i> <?php echo $curView; ?></span>
</small>
</p>
<?php
$img_url = get_template_directory_uri() . '/assets/img/podcast.jpg';
if (get_the_post_thumbnail())
{
$img_url = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
}
?>
<a href="<?php the_permalink(); ?>" title="Popsicase">
<div class="item-thumbnail" style="background:url(<?php echo $img_url; ?>) no-repeat center center;">
</div>
</a>
<div class="row">
<div class="col-xs-6 text-right">
<?php /*<span class="comment"> Comentarios: <?php comments_number( '0','1','%'); ?></span>*/ ?>
</div>
</div>
<div class="item-excerpt text-left">
<div class="col-xs-12">
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
</div>
<?php if ($cont_items % 2 == 0) : ?>
</div>
</div>
<div class="row items-row">
<div class="col-xs-12">
<?php
endif;
$cont_items++;
?>
<?php endwhile; ?>
</div>
</div>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<?php
the_posts_pagination( array(
'mid_size' => 4,
'prev_text' => __( 'Artículos antiguos', 'textdomain' ),
'next_text' => __( 'Artículos nuevos', 'textdomain' ),
) );
?>
<?php /*
<div class="nav-previous alignleft"><?php next_posts_link( 'Artículos antiguos' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Artículos nuevos' ); ?></div>
*/ ?>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<div class="hidden-xs col-sm-3 sidebar">
<div class="row">
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('main-sidebar')) : ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<div class="col-xs-12 pos-content">
<div class="container">
<?php //Para añadir contenido se interesa ?>
</div>
</div>
<div class="clearfix"></div>
</div>

Wordpress show latest category posts from custom query

in my category.php template i want to show the latest entries only if are of the same category, and when i execute the custom query shows every entries.
This is my code, how could i fix the error?
Code:
<?php
/**
* The template for displaying all single posts.
*
* #package Mundo Geek
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main-single" class="site-main" role="main">
<div id="row">
<?php
$ultimas = new WP_Query();
$ultimas -> query('showposts=3');
while($ultimas -> have_posts()) : $ultimas ->the_post();
?>
<div class="category_page_last col-xs-12 col-sm-4">
<a href="<?php the_permalink(); ?>">
<div class="thumb">
<?php
if(has_post_thumbnail()){
//echo '<img src="'.$url.'"/>';x
$backgroundImageUrl = "('".wp_get_attachment_url( get_post_thumbnail_id($post->ID) )."')";
echo '<div class="category_page_img-background" style="background-image: url'.$backgroundImageUrl.'"></div>';}
else{
$default_thumb = "('".get_bloginfo( 'stylesheet_directory' )."/images/default-thumbnail.jpg')";
echo '<div class="category_page_img-background" style="background-image: url'.$default_thumb.'"></div>';
}
?>
<div class="encimaimagen"></div>
</div>
<div class="meta"><h1><?php the_title(); ?></h1></div>
</a>
</div>
<?php endwhile; ?>
</div>
<div class="row">
<div class="col-sm-8">
<section id="category_page">
<?php
$ultimas = new WP_Query();
$ultimas -> query('showposts=40');
while($ultimas -> have_posts()) : $ultimas ->the_post();
?>
<article class="col-xs-12 archivo row">
<a href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
<div class="hidden-xs col-sm-4 col-md-3"><?php the_post_thumbnail( 'thumbnail' ); ?></div>
<div class="col-sm-8 col-md-9"><?php the_excerpt(); wp_reset_postdata();?></div>
</article>
<?php endwhile; ?>
</section>
</div>
<div class="col-sm-4 hidden-xs"><?php get_sidebar(); ?></div>
<div class="index_archivo col-xs-12">
<a href="<?php $url = home_url( $path = 'index.php/archivo', $scheme = relative ); echo $url;?>">
<h2>Ver todas las entradas</h2>
</a>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>
Thank you guys!
To display latest posts from a certain category, your code needs to look something like this:
<ul>
<?php
global $post;
$myposts = get_posts('numberposts=5&offset=1&category=3');
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><?php the_title(); ?> </li>
<?php endforeach; ?>
</ul>
Source: https://wordpress.org/support/topic/recent-posts-from-specific-category

Wordpress Loop in custom pages template for blog list is not working

I have created a Blog list through wordpress custom page template and assigned the same by creating a blog page.
But I am wondering the loop is correct but its not displaying any result.
http://projects.dev2d.com/msleximus/blog/
What to do. My Code ....
<?php
/*
Template Name: Blog
*/
get_header(); ?>
<!-- #primary -->
<div role="main" class="main">
<section class="page-top">
<div class="container">
<div class="row">
<div class="span12">
<ul class="breadcrumb">
<li>Home <span class="divider">/</span></li>
<li class="active">
<?php wp_title(); ?>
</li>
</ul>
</div>
</div>
<div class="row">
<div class="span12">
<h2> Blog </h2>
</div>
</div>
</div>
</section>
<div class="container">
<div class="row">
<div class="span9">
<?php
if ( is_page() ) {
$category = get_post_meta( $posts[0]->ID, 'category', true );
$cat = get_cat_ID( $category );
}
if ( $cat ) :
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$post_per_page = 4; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies
$args=array (
'category__in' => array( $cat ),
'post_type'=> 'post',
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page' => $post_per_page,
'ignore_sticky_posts' => $do_not_show_stickies
);
$temp = $wp_query; // assign original query to temp variable for later use
global $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="blog-posts">
<article <?php post_class() ?> id="post-<?php the_ID(); ?>class="post post-medium-image">
<div class="row">
<div class="span4">
<div class="post-image">
<div class="flexslider flexslider-center-mobile flexslider-simple" data-plugin-options='{"controlNav":false, "animation":"slide", "slideshow": false, "maxVisibleItems": 1}'>
<ul class="slides">
<li> <img class="img-rounded" src="<?php the_post_thumbnail('medium'); ?>" alt="featured image"></li>
</ul>
</div>
</div>
</div>
<div class="span5">
<div class="post-content">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a></h2>
<?php the_content( 'read more »' ); ?>
</div>
</div>
</div>
<div class="row">
<div class="span9">
<div class="post-meta"> <span><i class="icon-calendar"></i>
<?php the_time( 'F jS, Y' ) ?>
</span> <span><i class="icon-user"></i> By <a href="#">
<?php the_author() ?>
</a> </span> <span><i class="icon-tag"></i>
<?php the_tags( 'Tags: ', ', ', '<br />' ); ?>
,</span> <span><i class="icon-comments"></i>
<?php comments_popup_link( 'No Comments »', '1 Comment »', '% Comments »' ); ?>
Read more... </div>
</div>
</div>
</article>
<?php endwhile; ?>
<div class="pagination pagination-large pull-right">
<div class="alignleft">
<?php next_posts_link( '« Older Entries' ) ?>
</div>
<div class="alignright">
<?php previous_posts_link( 'Newer Entries »' ) ?>
</div>
</div>
</div>
</div>
<?php endif; // if ( $wp_query->have_posts() ) ?>
<?php $wp_query = $temp; //reset back to original query ?>
<div class="span3">
<aside class="sidebar">
<?php get_search_form(); ?>
<?php get_sidebar(); ?>
<div class="tabs">
<ul class="nav nav-tabs">
<li class="active"><i class="icon-star"></i> Popular</li>
<li>Recent</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="popularPosts">
<?php fanciedmedia_popular_posts(5); ?>
</div>
<div class="tab-pane" id="recentPosts">
</div>
</div>
</div>
<hr />
</aside>
</div>
<?php else : ?>
<div class="row">
<div class="span12">
<div class="post-content">
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
</div>
</div>
</div>
<?php endif; // if ( $cat ) ?>
</div>
</div>
</div>
<?php get_footer(); ?>
Use this one and do let me know ..
<div class="container">
<div class="row">
<div class="span9">
<div class="blog-posts">
<?php query_posts('category_name = Category&showposts=10'); ?>
<?php while (have_posts()) : the_post() ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>class="post post-medium-image">
<div class="row">
<div class="span4">
<div class="post-image">
<div class="flexslider flexslider-center-mobile flexslider-simple" data-plugin-options='{"controlNav":false, "animation":"slide", "slideshow": false, "maxVisibleItems": 1}'>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
} ?>
</div>
</div>
</div>
<div class="span5">
<div class="post-content">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a></h2>
<?php the_excerpt(); ?>
Read more... </div>
</div>
</div>
<div class="row">
<div class="span9">
<div class="post-meta"> <span><i class="icon-calendar"></i>
<?php the_time( 'F jS, Y' ) ?>
</span> <span><i class="icon-user"></i> By <a href="#">
<?php the_author() ?>
</a> </span> <span><i class="icon-tag"></i>
<?php the_tags( 'Tags: ', ', ', '<br />' ); ?>
,</span> <span><i class="icon-comments"></i>
<?php comments_popup_link( 'No Comments »', '1 Comment »', '% Comments »' ); ?>
</div>
</div>
</div>
</article>
<?php endwhile; ?>
<?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>
<div class="pagination pagination-large pull-right">
<div class="alignleft">
<div class="nav-next alignright">
<?php previous_posts_link( 'Newer posts' ); ?>
</div>
</div>
<div class="alignright">
<div class="nav-previous alignleft">
<?php next_posts_link( 'Older posts' ); ?>
</div>
</div>
</div>
<?php } ?>
</div>
</div>

Categories