i am searching since a while for a solution to set my custom post loop like this:
first post> img left, content right // second post> content left, img right
this is my code so far:
<div class="container">
<?php $loop = new WP_Query( array( 'post_type' => 'profile', 'posts_per_page' => 10 ) ); ?>
<?php if (have_posts()) : while(have_posts()) : the_post(); $i++; if(($i % 2) == 0) : ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
<div class="row centered wow fadeInUpBig" data-wow-duration="2s">
<div class="col col-4">
<?php the_post_thumbnail(600); ?>
</div>
<div class="col col-6">
<section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span>
<h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
<?php the_content();?>
</section>
</div>
</div>
</article>
<?php else : ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
<div class="row centered wow fadeInUpBig" data-wow-duration="2s">
<div class="col col-6">
<section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span>
<h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
<?php the_content();?>
</section>
</div>
<div class="col col-4">
<?php the_post_thumbnail(600); ?>
</div>
</div>
</article>
<?php endif; endwhile; endif; ?>
</div>
I know, this question has been asked a couple of times, and i tried already this and this and i read this but nothing works for me. What am i doing wrong?
I imagine its only outputting one post, and that post is actually the post content attached to the page. The problem is how you are initialising your additional loop within the page. You are creating a new post object - but you are not assigning it to the if / while statements:
<?php if (have_posts()) : while(have_posts()) : the_post(); $i++; if(($i % 2) == 0) : ?>
should be:
<?php if ($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post(); $i++; if(($i % 2) == 0) : ?>
Notice the addition of the $loop variable where you are setting your post object and arguments.
i got this code (not altering) working already:
<div class="container">
<?php $loop = new WP_Query( array( 'post_type' => 'profile', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
<div class="row centered wow fadeInUpBig" data-wow-duration="2s">
<div class="col col-4">
<?php the_post_thumbnail(600); ?>
</div>
<div class="col col-6">
<section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span>
<h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
<?php the_content();?>
</section>
</div><!--.end col-->
</div><!--.end row-->
</article>
<?php endwhile; wp_reset_query(); ?>
</div><!--.end container-->
Related
I am trying to paginate archived post in WordPress when I click "next button" but it doesn't work. Please guide me to do it in a simple way. My code is given below:
<div class="grid-wrapper ">
<?php $args = array(
'posts_per_page' => 3,
'paged' => $paged
);
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="inner-blog">
<div class="row">
<div class="col-lg-4 col-md-2">
<div class="post-thumbnail">
<img class="img-fluid" src="<?php the_post_thumbnail_url(); ?>">
</div>
</div>
<div class="col-lg-8 col-md-9">
<h2 class="post-title"><?php the_title();?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php wp_reset_postdata(); ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
<ul class="pagination pull-right">
<li><?php echo get_next_posts_link( 'Next Page', $the_query->max_num_pages ); ?> »</li>
<li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
</ul>
</div>
</div>
The PHP code seems to be blocking or interfering with my advanced custom fields as it doesn't display unless I remove the PHP code then it does. I can't figure out where the issue is.
Any help is much appreciated.
PHP
<?php if(strpos($_SERVER['REQUEST_URI'], 'gaeilge') !== false) {
$newsCat = 'cat=5,7&showposts=3';
} else {
$newsCat = 'cat=6,8&showposts=3';
}; ?>
Advanced Custom Fields
<div class="carousel-item active">
<div class="row py-5">
<?php if( have_rows('block') ): ?>
<?php while( have_rows('block') ): the_row();
// vars
$content = get_sub_field('content');
?>
<div class="col-lg-4 col-md-4">
<?php if(strpos($_SERVER['REQUEST_URI'], 'gaeilge') !== false) { ?> <!--Check if url contains the word "items" -->
<h2 class="fw-b c-blue mt-0">Ár bhFís</h2>
<?php } else { ?>
<h2 class="fw-b c-blue mt-0">Our Vision</h2>
<?php } ?>
</div>
<div class="col-lg-8 col-md-8">
<p class="c-blue mb-0"><?php echo $content; ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
Fixed it by just changing how News articles where added.
<div class="row pt-4 pb-3">
<?php
// args
$args = array(
'posts_per_page' => -1,
'post_type' => 'post'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="col-lg-4 col-md-4 col-sm-6 mb-5">
<div class="w-100 mb-2 px-2">
<img class="w-100" src="<?php $featimage = the_post_thumbnail_url('news-image'); ?>" alt="">
<p class="text-muted mt-4 mb-2"><?php echo get_the_date('dS M, Y'); ?></p>
<h3 class="c-blue"><?php the_title(); ?></h3>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
</div>
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
In my theme I have a blog page and a homepage (without blog loop). Now I would like to add some blog posts on my homepage (like most recent).
I am using the blog page as a template for the homepage ( I copied and renamed the original), wich works good. But I can't make any changes to it without changing the original blog page (the loop is called in an external php).
I Have no idea were to start.
This is the piece on the homepage calling the blog loop:
<?php if(have_posts()) : the_post(); ?>
<div id="main">
<div class="central-wrapper clearfix">
<div id="center" class="fullwidth">
<div id="content">
<?php if($gallery_select && $gallery_position == 1) : ?>
<?php codeus_gallery($gallery_select, $gallery_size, $gallery_style); ?>
<?php endif; ?>
<div class="inner">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="pagination"><div class="page-links-title">' . __( 'Pages:', 'codeus' ) . '</div>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<div class="bloghome">
<?php codeus_blog_list(); ?>
</div>
<?php if($quickfinder_position == 2) : ?>
<?php codeus_quickfinder($quickfinder_select); ?>
<?php endif; ?>
<?php if($portfolio_position == 2) : ?>
<?php codeus_portfolio(implode(',',$portfolio_select), $portfolio_size, $portfolio_count, $portfolio_filter, $portfolio_title); ?>
<?php endif; ?>
<?php if($gallery_select && $gallery_position == 3) : ?>
<?php codeus_gallery($gallery_select, $gallery_size, $gallery_style); ?>
<?php endif; ?>
</div>
</div><!-- #content -->
</div><!-- #center -->
</div><!-- .central-wrapper -->
</div><!-- #main -->
<?php endif; ?>
</div><!-- wrap end -->
And this is the piece in the external php called blog.php
<?php if($blog_loop->have_posts()) : ?>
<div class="blog_list">
<ul class="styled">
<?php while ( $blog_loop->have_posts() ) : $blog_loop->the_post(); ?>
<li class="clearfix">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'codeus_post_list_image'); ?>
<div class="comment-info">
<?php echo get_the_date('d'); ?>
<div class="date-month"><?php echo get_the_date('M'); ?></div>
</div>
<div class="post-info">
<h3><?php the_title(); ?></h3>
<?php if($image_url[0]) : ?>
<div class="post-image">
<div class="image wrap-box shadow middle">
<div class="shadow-left"></div><div class="shadow-right"> </div>
<img src=" <?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endif; ?>
<div class="text clearfix"><?php the_excerpt(); ?></div>
<?php codeus_author_info(get_the_ID()); ?>
</div>
</div>
</li>
<?php $post = $portfolio_posttemp; wp_reset_postdata(); ?>
<?php endwhile; ?>
</ul>
<?php codeus_pagination($page_num, $pages_count); ?>
</div>
<?php endif; ?>
Remember, I don't want to make changes in the original blog.php, because the normal blog page needs to work as it is now. I would like to make the blog work on the homepage also, but just with a maximum of 3 posts showing, perhaps of a certain category.
Help would be so welcome!!
I think I found a solution, I edited the homepage template like this:
<!-- wrap start --><div class="content-wrap">
<?php if(have_posts()) : the_post(); ?>
<div id="main">
<div class="central-wrapper clearfix">
<div id="center" class="fullwidth">
<div id="content">
<div class="inner">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="pagination"><div class="page-links-title">' . __( 'Pages:', 'codeus' ) . '</div>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<div class="bloghome">
<?php
$page_num = (get_query_var('paged')) ? get_query_var('paged') : 1;
$items_per_page = 2;
$blog_loop = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => $items_per_page,
'paged' => $page_num
));
$pages_count = ceil($blog_loop->found_posts/$items_per_page);
global $post;
$portfolio_posttemp = $post;
?>
<?php if($blog_loop->have_posts()) : ?>
<div class="blog_list">
<ul class="styled">
<?php while ( $blog_loop->have_posts() ) : $blog_loop->the_post(); ?>
<li class="clearfix">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'codeus_post_list_image'); ?>
<div class="comment-info">
<?php echo get_the_date('d'); ?>
<div class="date-month"><?php echo get_the_date('M'); ?></div>
</div>
<div class="post-info">
<h3><?php the_title(); ?></h3>
<?php if($image_url[0]) : ?>
<div class="post-image">
<div class="image wrap-box shadow middle">
<div class="shadow-left"></div><div class="shadow-right"></div>
<img src="<?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endif; ?>
<div class="text clearfix"><?php the_excerpt(); ?></div>
<?php codeus_author_info(get_the_ID()); ?>
</div>
</div>
</li>
<?php $post = $portfolio_posttemp; wp_reset_postdata(); ?>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
</div>
</div>
</div><!-- #content -->
</div><!-- #center -->
</div><!-- .central-wrapper -->
</div><!-- #main -->
<?php endif; ?>
</div><!-- wrap end -->
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>