Odd/even html in php loop not alternating properly - php

I've been searching through questions/answers on here, but can't seem to find something that works with my code.
Most of the solutions I've found cause the whole page to get a 500 error. They're generally just snippets of the odd/even php, and aren't working for me to easily integrate with the custom post type loop php I have. I'm probably just not putting it in the right place, but nothing seem to be working.
I'm not super great with php, but this is the one thing I've always had an issue getting to work.
Goal:
Odd posts have the headshot on the left, bio info on the right.
Even posts have the headshot on the right, bio info on the left.
Below is my code, which does load on the page (no 500 error), but doesn't output the alternating layout, just outputs the same layout as if I didn't have the odd/even code.
<?php // theloop
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $loop = new WP_Query( array( 'post_type' => 'team', 'posts_per_page' => -1, 'order' => 'ASC') ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php if ($wp_query->current_post % 2 == 0): ?>
<div class="row team-member"> <!--ODD LAYOUT // HEADSHOT LEFT - BIO RIGHT-->
<div class="container">
<div class="row is-table-row">
<div class="col-sm-6 headshot" style="background-image:url(<?php the_field('bio_photo'); ?>);">
<div class="box">
</div>
</div>
<div class="col-sm-6 bio cream-bg">
<div class="box">
<?php if( get_field('additional_logo') ): ?>
<div class="additional-logo"><img src="<?php the_field('additional_logo'); ?>"></div>
<?php endif; ?>
<h2><?php the_field('name'); ?></h2>
<div class="bio-content"><?php the_field('bio'); ?></div>
<div class="contact-container">
<h4>Contact me!</h4>
<?php if( get_field('phone_number') ): ?>
<p><i class="fa fa-phone" aria-hidden="true"></i> <?php the_field('phone_number'); ?></p>
<?php endif; ?>
<p><i class="fa fa-envelope" aria-hidden="true"></i> <?php the_field('email'); ?></p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="row team-member"> <!--EVEN LAYOUT // HEADSHOT RIGHT - BIO LEFT-->
<div class="container">
<div class="row is-table-row">
<div class="col-sm-6 bio cream-bg">
<div class="box">
<?php if( get_field('additional_logo') ): ?>
<div class="additional-logo"><img src="<?php the_field('additional_logo'); ?>"></div>
<?php endif; ?>
<h2><?php the_field('name'); ?></h2>
<div class="bio-content"><?php the_field('bio'); ?></div>
<div class="contact-container">
<h4>Contact me!</h4>
<?php if( get_field('phone_number') ): ?>
<p><i class="fa fa-phone" aria-hidden="true"></i> <?php the_field('phone_number'); ?></p>
<?php endif; ?>
<p><i class="fa fa-envelope" aria-hidden="true"></i> <?php the_field('email'); ?></p>
</div>
</div>
</div>
<div class="col-sm-6 headshot" style="background-image:url(<?php the_field('bio_photo'); ?>);">
<div class="box">
</div>
</div>
</div>
</div>
</div>
<?php endif ?>
<?php endwhile; wp_reset_query(); ?>
Is there anything I've done wrong here, or a better solution to this? I'm frustrated that this is theoretically a simple request that I just can't seem to make work properly. Any help is much appreciated!

OK, pro tip: programmers are lazy. We like the DRY principle. We don't like to duplicate code, nor do we like to maintain giant blocks of duplicated code.
So, below is a modified version of your loop that is somewhat simpler, with less duplication. I would encourage you to consider other ways to reduce duplication, for example, using CSS classes (floats, possibly) to alternate which is on the left or right, and only render one version of the HTML one time.
The specific problem is that you're not accessing the $current_post property of the correct query object. You should be using $loop->current_post instead of $wpdb->current_post. However, to be super clear / explicit, I would manually set a counter, and use that instead:
<?php // theloop
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $loop = new WP_Query( array( 'post_type' => 'team', 'posts_per_page' => -1, 'order' => 'ASC') ); ?>
<?php
// initialize the counter here
$post_count = 0;
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="row team-member">
<div class="container">
<div class="row is-table-row">
<?php
// move the if condition here to reduce / simplify code
// reference (and increment) the counter
if ($post_count++ % 2 == 0): ?>
<div class="col-sm-6 headshot" style="background-image:url(<?php the_field('bio_photo'); ?>);">
<div class="box">
</div>
</div>
<div class="col-sm-6 bio cream-bg">
<div class="box">
<?php if( get_field('additional_logo') ): ?>
<div class="additional-logo"><img src="<?php the_field('additional_logo'); ?>"></div>
<?php endif; ?>
<h2><?php the_field('name'); ?></h2>
<div class="bio-content"><?php the_field('bio'); ?></div>
<div class="contact-container">
<h4>Contact me!</h4>
<?php if( get_field('phone_number') ): ?>
<p><i class="fa fa-phone" aria-hidden="true"></i> <?php the_field('phone_number'); ?></p>
<?php endif; ?>
<p><i class="fa fa-envelope" aria-hidden="true"></i> <?php the_field('email'); ?></p>
</div>
</div>
</div>
<?php else: ?>
<div class="col-sm-6 bio cream-bg">
<div class="box">
<?php if( get_field('additional_logo') ): ?>
<div class="additional-logo"><img src="<?php the_field('additional_logo'); ?>"></div>
<?php endif; ?>
<h2><?php the_field('name'); ?></h2>
<div class="bio-content"><?php the_field('bio'); ?></div>
<div class="contact-container">
<h4>Contact me!</h4>
<?php if( get_field('phone_number') ): ?>
<p><i class="fa fa-phone" aria-hidden="true"></i> <?php the_field('phone_number'); ?></p>
<?php endif; ?>
<p><i class="fa fa-envelope" aria-hidden="true"></i> <?php the_field('email'); ?></p>
</div>
</div>
</div>
<div class="col-sm-6 headshot" style="background-image:url(<?php the_field('bio_photo'); ?>);">
<div class="box">
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>

Related

How to change the WordPress search results page (using Betheme and Ajax search lite)

I'm currently working on a website for a client and I want to change the search results page. I tried a lot of things but when I search for something on the website I get search results, but without a image or description. My client want the website to show a image and a description.
Here is my search.php code:
/**
* The search template file.
*
* #package Betheme
* #author Muffin group
* #link https://muffingroup.com
*/
get_header();
$translate['search-title'] = mfn_opts_get('translate') ? mfn_opts_get('translate-search-title','Ooops...') : __('Ooops...','betheme');
$translate['search-subtitle'] = mfn_opts_get('translate') ? mfn_opts_get('translate-search-subtitle','No results found for:') : __('No results found for:','betheme');
$translate['published'] = mfn_opts_get('translate') ? mfn_opts_get('translate-published','Published by') : __('Published by','betheme');
$translate['at'] = mfn_opts_get('translate') ? mfn_opts_get('translate-at','at') : __('at','betheme');
$translate['readmore'] = mfn_opts_get('translate') ? mfn_opts_get('translate-readmore','Read more') : __('Read more','betheme');
?>
<div id="Content">
<div class="content_wrapper clearfix">
<div class="sections_group">
<div class="section">
<div class="section_wrapper clearfix">
<?php if( have_posts() && trim( $_GET['s'] ) ): ?>
<div class="column one column_blog">
<div class="blog_wrapper isotope_wrapper">
<div class="posts_group classic">
<?php
while ( have_posts() ):
the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class( array('post-item', 'clearfix', 'no-img') ); ?>>
<div class="post-desc-wrapper">
<div class="post-desc">
<?php if( mfn_opts_get( 'blog-meta' ) ): ?>
<div class="post-meta clearfix">
<div class="author-date">
<span class="author"><span><?php echo esc_html($translate['published']); ?> </span><i class="icon-user"></i> <?php the_author_meta('display_name'); ?></span>
<span class="date"><span><?php echo esc_html($translate['at']); ?> </span><i class="icon-clock"></i> <?php echo esc_html(get_the_date()); ?></span>
</div>
</div>
<?php endif; ?>
<div class="post-title">
<h2><?php the_title(); ?></h2>
</div>
<div class="post-excerpt">
<?php the_excerpt(); ?>
</div>
<div class="post-footer">
<div class="post-links">
<i class="icon-doc-text"></i> <?php echo esc_html($translate['readmore']); ?>
</div>
</div>
</div>
</div>
</div>
<?php
endwhile;
?>
</div>
<?php
if(function_exists( 'mfn_pagination' )):
echo mfn_pagination();
else:
?>
<div class="nav-next"><?php next_posts_link(esc_html__('← Older Entries', 'betheme')) ?></div>
<div class="nav-previous"><?php previous_posts_link(esc_html__('Newer Entries →', 'betheme')) ?></div>
<?php
endif;
?>
</div>
</div>
<?php else: ?>
<div class="column one search-not-found">
<div class="snf-pic">
<i class="themecolor <?php echo esc_attr(mfn_opts_get('error404-icon', 'icon-traffic-cone')); ?>"></i>
</div>
<div class="snf-desc">
<h2><?php echo esc_html($translate['search-title']); ?></h2>
<h4><?php echo esc_html($translate['search-subtitle']) .' '. esc_html($_GET['s']); ?></h4>
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php if( is_active_sidebar( 'mfn-search' ) ): ?>
<div class="sidebar four columns">
<div class="widget-area clearfix <?php echo esc_attr(mfn_opts_get('sidebar-lines')); ?>">
<?php dynamic_sidebar( 'mfn-search' ); ?>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php get_footer();
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<div class="post-img">
<img src="<?php echo $featured_img_url; ?>"/>
</div>
use above code in while loop to show image by using post id.

showing only first post when clicking read more of any post wordpress posts

Here is showing only last created post when i am clicking on readmore button of post.
remember i have called all posts on another template(our_program.php inplace of index.php) all posts are showing well but on single.php here is only only one recent post showing when i am clicking on read more of any post. my single.php code is below
<?php $args = array( 'post_type' => 'post');
query_posts($args);
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section class="talent-development" id="telent-development">
<div class="container">
<div class="row">
<div class="section-content-text clearfix">
<div class="col-sm-12">
<h2><?php the_title(); ?></h2>
</div>
<div class="col-sm-5">
<div class="ourProgramImg">
<?php if( has_post_thumbnail()){
the_post_thumbnail();
}
?>
</div>
</div>
<div class="col-sm-7">
<div class="ourProgramContent">
<p>
<?php the_content();?>
</p>
</div>
</div>
</div>
<!--<div class="col-sm-6">
<div class="section-content-img">
<img src="img/sec.jpg" alt="" title="" />
</div>
</div>-->
</div>
</div>
</section>
<?php endwhile; else: ?>
<p> Not post Found </p>
<?php endif; ?>
and post page(permalink) code below
<?php $args = array( 'post_type' => 'post');
query_posts($args);
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section class="talent-development" id="telent-development">
<div class="container">
<div class="row">
<div class="section-content-text clearfix">
<div class="col-sm-12">
<h2><?php the_title(); ?></h2>
</div>
<div class="col-sm-5">
<div class="ourProgramImg">
<?php if( has_post_thumbnail()){
the_post_thumbnail();
}
?>
</div>
</div>
<div class="col-sm-7">
<div class="ourProgramContent">
<p>
<?php echo substr(get_the_excerpt(), 0, 400); //the_content();?>
</p>
Read More
</div>
</div>
</div>
<!--<div class="col-sm-6">
<div class="section-content-img">
<img src="img/sec.jpg" alt="" title="" />
</div>
</div>-->
</div>
</div>
</section>
<?php endwhile; else: ?>
<p> Not post Found </p>
<?php endif; ?>

How can I loop into two different DIVS without repeating in wordpress custom post

this is the code of my custom post.I am not so expert in php.So Please help me in this matter.
<?php $featuresitems=new WP_Query(array( 'post_type'=>'scbleftfeatures' )); ?>
<?php while( $featuresitems->have_posts()) : $featuresitems->the_post(); ?>
<div class="col-md-6 left-grid">
<div class="features-grid1">
<div class="feature">
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<div class="icon5">
<?php the_post_thumbnail('features_icon_size'); ?>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="col-md-6 right-grid">
<div class="features-grid1">
<div class="feature">
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<div class="icon5">
<?php the_post_thumbnail(); ?>
</div>
<div class="clearfix"></div>
</div>
</div>
<?php endwhile; ?>
Question is a little vague but I'm assuming what you actually want to do is just change the classes left-grid and right-grid for every second div. In that case you don't have to repeat the whole divs, just change the classes. This can done with help of a "counter" ($i).
<?php
$featuresitems = new WP_Query(array(
'post_type' => 'scbleftfeatures'
));
$i = 0;
?>
<?php
while( $featuresitems->have_posts()) : $featuresitems->the_post();
$i_is_even = ($i % 2 == 0);
?>
<div class="col-md-6 <?php if ($i_is_even) {echo 'left-grid'; } else { echo 'right-grid'; }; ?>">
<div class="features-grid1">
<div class="feature">
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<div class="icon5">
<?php the_post_thumbnail('features_icon_size'); ?>
</div>
<div class="clearfix"></div>
</div>
</div>
<?php
$i ++;
endwhile;
?>
If you're curious about the % operator I suggest you check out the Modulus operator on http://php.net/manual/en/language.operators.arithmetic.php
And if you actually want to alter between different divs you can put the whole divs inside the if/else blocks instead of just the classes.
Edit:
Not sure why you'd want this as it seems you're using the bootstrap grid anyways.

Display posts from a Wordpress custom post type category

I have a custom post type set up called TESTIMONIALS and two CPT categories set up which are CLIENT TESTIMONALS & CLINIC TESTIMONIALS
I am trying to display only the posts from the CLIENT TESTIMONALS CPT category.
What would I need to add to the below to achieve this?
<div role="tabpanel" class="tab-pane fade" id="profile">
<?php query_posts('post_type=testimonials'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="testimonial-holder wrap ">
<div class="three-quarters">
<h2>
<?php the_title(); ?>
</h2>
<div class="testi">
<?php the_content(); ?>
</div>
</div>
<div class="four-col right center">
<div class="testimonial-autor-image"> <img src="<?php the_field('author_image_or_clinic_logo'); ?>" alt="Author Image">
<div class="mt20">
<?php the_field('testimonial_author'); ?>
</div>
</div>
</div>
</div>
<?php endwhile; // end of the loop. ?>
</div>
You can use something like this.
<?php
$type = 'testimonials';
$args=array(
'post_type' => $type,
'category'=>'CPT',
'post_status' => 'publish'
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="testimonial-holder wrap ">
<div class="three-quarters">
<h2>
<?php the_title(); ?>
</h2>
<div class="testi">
<?php the_content(); ?>
</div>
</div>
<div class="four-col right center">
<div class="testimonial-autor-image"> <img src="<?php the_field('author_image_or_clinic_logo'); ?>" alt="Author Image">
<div class="mt20">
<?php the_field('testimonial_author'); ?>
</div>
</div>
</div>
</div>
<?php
endwhile;
}
?>

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