In first img code is working with posts == 5
In second image code getting broken if posts == 3
.row doenst close
How to use wordpress Lopp with Bootstrap grid I wanna to know the best way to optimize my code. This code work correctly but I wanna just to optimize this code I think this isn't the best solution. Please show me how I can optimize this code. Thank you.
<div class="container">
<?php
$args = [
'post_status' => 'publish',
'posts_per_page' => 6,
'no_found_rows' => true,
];
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
if ( $query->current_post === 0 )
{
echo '<div class="row">';
echo '<div class="col-12 col-lg-6">'; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'post 1' ); ?>>
<figure>
<?php the_post_thumbnail('full', array('class' => 'img-fluid') ); ?>
<figure>
<div class="entry">
<div class="entry-title">
<?php the_title(); ?>
</div>
</div>
</article>
<?php
echo '</div>';
}
if ( $query->current_post === 1 )
{
echo '<div class="col-12 col-lg-6">'; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'post 2' ); ?>>
<figure>
<?php the_post_thumbnail('full', array('class' => 'img-fluid') ); ?>
<figure>
<div class="entry">
<div class="entry-title">
<?php the_title(); ?>
</div>
</div>
</article>
<?php
}
if ( $query->current_post === 2 )
{ ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'post 3' ); ?>>
<figure>
<?php the_post_thumbnail('full', array('class' => 'img-fluid') ); ?>
<figure>
<div class="entry">
<div class="entry-title">
<?php the_title(); ?>
</div>
</div>
</article>
<?php
}
if ( $query->current_post === 3 )
{ ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'post 4' ); ?>>
<figure>
<?php the_post_thumbnail('full', array('class' => 'img-fluid') ); ?>
<figure>
<div class="entry">
<div class="entry-title">
<?php the_title(); ?>
</div>
</div>
</article>
<?php
echo '</div>';
echo '</div>';
}
endwhile; wp_reset_postdata(); ?>
</div>
<div class="container">
<div class="row">
<div class="col-6">
<img src="" alt="">
<h1>TITLE</h1>
<div class="exerpt">
exerpt
</div>
</div>
<div class="col-6">
<article class="row">
<div class="col-4">
<img src="" alt="">
</div>
<div class="col-8">
title
</div>
</article>
<article class="row">
<div class="col-4">
<img src="" alt="">
</div>
<div class="col-8">
title
</div>
</article>
<article class="row">
<div class="col-4">
<img src="" alt="">
</div>
<div class="col-8">
title
</div>
</article>
</div>
</div>
<div class="row">
<div class="col-6">
post-4
</div>
<div class="col-6">
post-5
</div>
</div>
You don't need to write the same part of code each time you check a condition.
<div class="container">
<?php
$args = [
'post_status' => 'publish',
'posts_per_page' => 6,
'no_found_rows' => true,
];
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
$post_num = strval($query->current_post + 1);
$post_count = wp_count_posts()->publish;
if ( $query->current_post === 0 || $query->current_post === 4 ) : ?>
<div class="row">;
<?php ;endif; ?>
<?php if ( $query->current_post === 0 || $query->current_post === 1 || $query->current_post === 4 || $query->current_post === 5 ) : ?>
<div class="col-lg-6">'
<?php ;endif; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( $post_num ); ?>>
<figure>
<?php the_post_thumbnail('full', array('class' => 'img-fluid') ); ?>
</figure>
<?php if ( $query->current_post === 0) : ?>
<div class="post_excerpt">
<?php the_excerpt(); ?>
</div>
<?php ;endif; ?>
<div class="entry">
<div class="entry-title">
<?php the_title(); ?>
</div>
</div>
</article>
<?php if ( $query->current_post === 0 || $query->current_post === 3 || $query->current_post === 4 || $query->current_post === 5 || $query->current_post === $post_count-1 ) : ?>
</div>
<?php ;endif; ?>
<?php if ( $query->current_post === 3 || $query->current_post === 5 || $query->current_post === $post_count-1 ) : ?>
</div>
<?php ;endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div>
Related
I'm trying 3 columns layout of related articles, but if the number of related articles is less than 3 to be different layout. If the number of related articles is 2 the first column to have class col-sm-offset-2, and if the columns is 1 - to have class col-sm-offset-4.
$category = get_post_category();
$relatedArticles = get_posts( [
'exclude' => $post->ID,
'category' => $category->term_id,
'posts_per_page' => 3
] );
?>
<?php if ( count( $relatedArticles ) > 0 ): ?>
<section class="related-articles">
<div class="container">
<div class="row">
<h2 class="text-center"><?php echo __( 'Related Articles' ) ?></h2>
<?php foreach ( $relatedArticles as $relatedArticle ): ?>
<div class="col-sm-4">
<article id="" class="post">
<div class="post-image">
<a href="<?php echo get_permalink( $relatedArticle ) ?>">
<?php echo get_the_post_thumbnail( $relatedArticle, 'post-category' ) ?>
</a>
</div>
<h3 class="post-title">
<a href="<?php echo get_permalink( $relatedArticle ); ?>">
<?php echo get_post_title($relatedArticle) ?>
</a>
</h3>
<div class="post-summary">
<p><?php echo get_summery($relatedArticle);?></p>
</div>
</article>
</div>
<?php endforeach; ?>
</div>
</div>
</section>
<?php endif ?>
You can add an if/else clause for the opening tag of the div which contains the article, adding the desired classes (applied only to the first article - I added a counter for that) in the different versions:
$category = get_post_category(); $relatedArticles = get_posts( [ 'exclude' => $post->ID, 'category' => $category->term_id, 'posts_per_page' => 3 ] ); ?>
<?php if ( count( $relatedArticles ) > 0 ): ?>
<section class="related-articles">
<div class="container">
<div class="row">
<h2 class="text-center">
<?php echo __( 'Related Articles' ) ?>
</h2>
<?php $counter = 1; ?>
<?php foreach ( $relatedArticles as $relatedArticle ): ?>
<?php if ( count( $relatedArticles ) >= 3 ) { ?>
<div class="col-sm-4">
<?php } elseif (( count( $relatedArticles ) == 2) && ($counter == 1)) { ?>
<div class="col-sm-4 col-sm-offset-2">
<?php } elseif (( count( $relatedArticles ) == 1) && ($counter == 1)) { ?>
<div class="col-sm-4 col-sm-offset-4">
<?php } ?>
<article id="" class="post">
<div class="post-image">
<a href="<?php echo get_permalink( $relatedArticle ) ?>">
<?php echo get_the_post_thumbnail( $relatedArticle, 'post-category' ) ?>
</a>
</div>
<h3 class="post-title">
<a href="<?php echo get_permalink( $relatedArticle ); ?>">
<?php echo get_post_title($relatedArticle) ?>
</a>
</h3>
<div class="post-summary">
<p>
<?php echo get_summery($relatedArticle);?>
</p>
</div>
</article>
</div>
<?php $counter++; ?>
<?php endforeach; ?>
</div>
</div>
</section>
<?php endif ?>
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-->
I'm working on a Wordpress site. I'm trying to make a category with the id 968 show up on the Wordpress site under the stories and successes div. However for some reason the stories and successes div is not having the content from the category inserted into it. Any information on how I can improve my query or PHP is much appreciated. If I need to update this post with more information please let me know. Below you will see the php template file I am using.
<?php
/**
* Template name: Random Template
*
*
*/
// Custom styles
wp_enqueue_style('stories-styles', get_bloginfo('stylesheet_directory') . '/assets/css/stories.css');
// TEMP -- Get the category from the slug
the_post();
$categorySlugs = array(
'religious-freedom' => 'religious-freedom',
'social-and-economic-justice' => 'social-and-economic-justice',
'civil-and-human-rights' => 'civil-and-human-rights'
);
// Get category
$categorySlugs = $categorySlugs[$post->post_name];
$storyQuery = new WP_Query(array(
'post_type' => array('stories', 'press-releases'),
'category_name' => $categorySlugs,
'posts_per_page' => 4,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
$successesQuery = new WP_Query(array(
'post_type' => 'stories',
'cat' => 968,
'posts_per_page' => 1,
'offset' => 5
));
// Remove yarpp
remove_filter('the_content', array($yarpp, 'the_content'), 1200);
?>
<?php get_header(); ?>
<div class="category-title">
<div class="container">
<h2><?php the_title() ?></h2>
</div>
</div>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
<article class="featured-issue-article">
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail('full', array( 'class' => "img-responsive attachment-post-thumbnail")); ?>
</div>
<?php endif; ?>
<div class="content">
<?php the_content() ?>
</div>
</article>
</div>
</div>
<hr class="large blue" />
<div class="row">
<div class="col-md-8">
<ul class="section-list">
<?php while ($storyQuery->have_posts()) : $storyQuery->the_post(); ?>
<li>
<h3><?php the_title() ?></h3>
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail('post-thumbnail', array( 'class' => "img-responsive attachment-post-thumbnail")); ?>
<?php endif; ?>
<?php if( get_field('custom_excerpt', $values_id)): ?>
<p><?php echo the_field('custom_excerpt'); ?></p>
<?php endif; ?>
<a class="more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Read More...</a>
</li>
<?php endwhile; ?>
</ul>
<?php
set_query_var('category_slug', $categorySlug);
get_template_part('issues-nav', 'bottom')
?>
<hr class="small blue" />
<div class="success-stories">
<h2>Stories & Successes</h2>
<ul>
<?php while ($successesQuery->have_posts()) : $successesQuery->the_post(); ?>
<li>
<h4>
<?php the_title() ?>
</h4>
<?= strip_tags($post->post_excerpt) ?>
<a class="more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Read More...</a>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
<div class="col-md-4">
<?php get_sidebar(); ?>
</div>
</div>
</div>
</div>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
I'm having trouble being able to insert an ad after every 3rd post in Wordpress. This is the code in the theme. I know I need a counter and an if statement.
<section id="recentnews">
<div class="headline"><h2><?php _e( 'Recent News', THB_THEME_NAME ); ?></h2></div>
<?php $args = array(
'posts_per_page' => '5',
'offset' => '5',
'ignore_sticky_posts' => '1'
);
?>
<?php $query = new WP_Query($args); ?>
<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<article class="post">
<div class="row">
<div class="five columns">
<div class="post-gallery">
<?php the_post_thumbnail('recent'); ?>
<?php echo thb_DisplayImageTag(get_the_ID()); ?>
</div>
</div>
<div class="seven columns">
<div class="post-title">
<aside><?php echo thb_DisplaySingleCategory(false); ?></aside>
<h2><?php the_title(); ?></h2>
</div>
<div class="post-content">
<p><?php echo ShortenText(get_the_excerpt(), 150); ?></p>
<?php echo thb_DisplayPostMeta(true,true,true,false); ?>
</div>
</div>
</div>
</article>
<?php endwhile; else: ?>
<article>
<?php _e( 'Please select tags from your Theme Options Page', THB_THEME_NAME ); ?>
</article>
<?php endif; ?>
<a id="loadmore" href="#" data-loading="<?php _e( 'Loading ...', THB_THEME_NAME ); ?>" data-nomore="<?php _e( 'No More Posts to Show', THB_THEME_NAME ); ?>" data-count="5" data-action="thb_ajax_home"><?php _e( 'Load More', THB_THEME_NAME ); ?></a>
</section>
I could show you what I tried and failed at, just to show that I tried. Don't give me negative points for that please.
<section id="recentnews">
<div class="headline"><h2><?php _e( 'Recent News', THB_THEME_NAME ); ?></h2></div>
<?php $args = array(
'posts_per_page' => '5',
'offset' => '5',
'ignore_sticky_posts' => '1'
);
?>
<?php $query = new WP_Query($args); ?>
<?php $i = 1; if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); if($i == 1) : ?>
<article class="post">
<div class="row">
<div class="five columns">
<div class="post-gallery">
<?php the_post_thumbnail('recent'); ?>
<?php echo thb_DisplayImageTag(get_the_ID()); ?>
</div>
</div>
<div class="seven columns">
<div class="post-title">
<aside><?php echo thb_DisplaySingleCategory(false); ?></aside>
<h2><?php the_title(); ?></h2>
</div>
<div class="post-content">
<p><?php echo ShortenText(get_the_excerpt(), 150); ?></p>
<?php echo thb_DisplayPostMeta(true,true,true,false); ?>
</div>
</div>
</div>
</article>
<div class="clear"> </div>
<?php if ( $i == 3|| $i == 9 || $i == 15 ) : ?>
<?php if (function_exists ('adinserter')) echo adinserter (2); ?>
<div class="clear"> </div>
<?php endif; ?><?php endif; ?>
<?php $i++; ?>
<?php endwhile; else: ?>
<article>
<?php _e( 'Please select tags from your Theme Options Page', THB_THEME_NAME ); ?>
</article>
<?php endif; ?>
</section>
Thank you.
You're close. You'll want to increment your counter within the while loop (using $i++), and then use the modulus operator % to determine whether or not the counter is evenly divisible by 3:
<?php if ($query->have_posts()) : $i = 1; while ($query->have_posts()) : $query->the_post(); ?>
<!-- Article -->
<?php if ( $i % 3 == 0 ) : ?>
<!-- Advertisement Here -->
<?php endif; ?>
<?php $i++; endwhile; else: ?>
<!-- Display Notice -->
<?php endif; ?>
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>