How to Make HTML Element Not Loop Inside Loop In WordPress - php

is this possible to make the post list like this in WordPress?
<div class="large-image">
<img src="http://example.com">
<h1>Title</h1>
</div>
<div class="post-row"><!-- this is not a loop -->
<div class="post-item"><!-- this is a loop -->
<img src="http://example.com">
<h1>Title</h1>
</div>
<div class="post-item"><!-- this is a loop -->
<img src="http://example.com">
<h1>Title</h1>
</div>
</div>
I have try with this code below, but the <div class="post-row"> always included in the loop.
<?php
if ( have_posts() ) : $post_count = 0;
while ( have_posts() ) : the_post();
if ( $post_count = 0 ) { ?>
<div class="large-image">
<img src="http://example.com">
<h1>Title</h1>
</div>
<?php } ?>
<div class"post-row">
<?php if ( $post_count > 0 ) { ?>
<div class="post-item"><!-- this is a loop -->
<img src="http://example.com">
<h1>Title</h1>
</div>
<?php } ?>
</div><!-- /post-row -->
<?php
endwhile
endif;
?>
What I wanted is <div class="post-row"> not looping.
I have try some answer in stackoverflow, but the post-row always looping.
Hope I get best answer.

You must move the post-row div above the while loop, you just missed placed it that's all
<?php if ( have_posts() ) : $post_count = 0;
if ( $post_count == 0 ) { ?>
<div class="large-image">
<img src="http://example.com">
<h1>Title</h1>
</div>
<?php } ?>
<div class"post-row">
<?php
while ( have_posts() ) : the_post();
if ( $post_count > 0 ) { ?>
<div class="post-item"><!-- this is a loop -->
<img src="http://example.com">
<h1>Title</h1>
</div>
<?php }
$post_count ++;
endwhile; ?>
</div><!-- /post-row -->
<?php endif; ?>

Related

PHP code for running "being watched" videos from selected category

In this code videos are shown at random. I wanted to show them as selected category (i-e= 'links' category). How can I write this code to work for the links category?
<?php if( !defined('ABSPATH') ) exit; global $awpt; ?>
<div class="being_watched">
<div class="heading">
<?php $h_tag = $awpt['live_videos_heading']; $title = $awpt['beingwatched_title'];
echo '<'.$h_tag.'>'.$title.'</'.$h_tag.'>' ?>
</div>
<div class="row">
<div class="col-md-12">
<ul class="Thumbnail_List">
<?php
$per_page = $awpt['watched_now_videos'];
query_posts('showposts='.$per_page.'&orderby=rand');
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php do_action( 'bestia_thumbnail_compatibility' ); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<?php wp_reset_query(); ?>
</ul>
</div>

Wrap every three WordPress posts in div

I'd like to wrap every three posts in a new row (div) with a total of nine posts on a page. There's an empty row at the end. I thought this (I need to wrap every 4 wordpress posts in a div) would work, but I have more posts on pages 2, 3, 4, etc. Below is a simplified version of my code. $i = 1.
<div class="row">
<?php while ( have_posts() ) : the_post(); ?>
<div class="column">
</div>
<?php if ($i % 3 == 0 ) : ?>
</div> <!-- .row -->
<div class="row">
<?php endif; $i++; ?>
<?php endwhile; ?>
</div> <!-- .row -->
You can use get_next_post() to check whether next post exists or not.
<?php if ($i % 3 == 0 ) : ?>
</div> <!-- .row -->
<?php
$next_post = get_next_post();
if (!empty( $next_post )): ?>
<div class="row">
<?php endif; ?>
I used this post (https://www.nosegraze.com/display-wordpress-posts-2-3-columns/) to solve my problem.
$i = 0;
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( $i == 0 ) : ?>
<div class="row">
<?php endif; ?>
<div class="column">
</div> <!-- .column -->
<?php
$i++;
if( $i == 3 ) :
$i = 0; ?>
</div> <!-- .row -->
<?php endif; ?>
<?php endwhile; ?>
<?php if ( $i > 0 ) : ?>
</div> <!-- .row -->
<?php endif; ?>

using an if statement inside a while with wordpress

Hi guys I'm using a while loop to display my posts and while I have posts I want to display the odd and even post numbers differently. I currently have the following code:
<div class="container">
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
</div>
<?php query_posts('post_type=portfolio&post_status=publish&posts_per_page=8&paged='. get_query_var('paged'));
if( have_posts() ):
echo '<div>';
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
$i = 0;
if($i % 2 == 0)
{?>
<div class="class1">
html content 1 to go here
</div>
<?php
$i++;
}else{
?>
<div class="class2">
html content 2 to go here
</div>
<?php $i++; } ?>
<?php endif; ?>
<?php endwhile; ?>
<div class="container">
<div class="navigation">
<span class="newer"><?php previous_posts_link(__('« See Previous Case Studies','example')) ?></span>
<span class="older"><?php next_posts_link(__('See Next Case Studies »','example')) ?></span>
</div>
</div> <!-- /.navigation -->
<?php else: ?>
<div id="post-404" class="noposts">
<p><?php _e('None found.','example'); ?></p>
</div><!-- /#post-404 -->
<?php endif; wp_reset_query(); ?>
I haven't used php for a while, so I'm unsure the best way to do the above. I'm currently getting a few errors relating to syntax such as unexpected end of file or unexpected } on line...
Can anyone figure out where I am going wrong?
Reverse Position
<?php endwhile; ?>
<?php endif; ?>
First While statement should get complete than if.

pagination custom post type with multiple WP_Query loop

after adding the function of pagination to Functions.php and recall it in template-product-listing.php
there is nothing shown in result.
I have a big problom with this...
could you find and resolve the problem?
thnx
<article class="row">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="single-head" class="sixteen columns">
<h1><?php the_title(); ?></h1>
<?php if (has_excerpt()) { the_excerpt(); } ?>
</div>
<div class="row">
<nav id="portfolio-filters" class="sixteen columns">
<?php _e('Show All', 'ci_theme'); ?>
<?php
$args = array(
'hide_empty' => 0
);
$skills = get_terms('product-category', $args);
?>
<?php foreach ( $skills as $skill ) : ?>
<?php echo $skill->name; ?>
<?php endforeach; ?>
</nav><!-- /portfolio-filters -->
</div>
<div id="portfolio-items" class="row">
<?php $ci_product_query = new WP_Query('post_type=product&posts_per_page=4'); ?>
<?php if ( $ci_product_query-> have_posts() ) : while ( $ci_product_query->have_posts() ) : $ci_product_query->the_post(); ?>
<?php $item_skills = wp_get_object_terms($post->ID, 'product-category'); ?>
<article class="<?php ci_e_setting('product_columns'); ?> columns <?php foreach ( $item_skills as $item_skill ) : echo $item_skill->slug.' '; endforeach; ?> columns portfolio-item">
<a href="<?php echo get_permalink(); ?>" title="<?php echo esc_attr(get_the_title()); ?>" class="fb">
<?php the_post_thumbnail('ci_portfolio_slider', array('class'=>'scale-with-grid')); ?>
</a>
<div class="portfolio-desc">
<h3><?php the_title(); ?></h3>
<p class="desc"><?php echo mb_substr(get_the_excerpt(), 0, 70); ?>...</p>
</div>
</article><!-- /portfolio-item -->
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
</div><!-- /portfolio-items -->
<?php get_template_part('part', 'call_to_action'); ?>
<?php endwhile; endif; ?>
<div class="pagination">
<?php wp_pagination(); ?>
</div>
</article>
if your sure there is a post_type called products and there is posts in it...
try:
<div id="portfolio-items" class="row">
<?php $ci_product_query = new WP_Query(array('post_type'=>'product', 'posts_per_page'=> 4); ?>
<?php if ( $ci_product_query-> have_posts() ) : while ( $ci_product_query->have_posts() ) : $ci_product_query->the_post(); ?>
You know you are starting a new wp_query for every post in have_posts() ? you might want to rethink what you are trying to achieve!

Find oldest post — WordPress

Thanks to some answers on here, I've managed to distinguish my posts into the latest post and all the rest. However, it needs to be the oldest post. Here is my current loop:
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; $c=0;?>
<?php while (have_posts()) : the_post(); ?>
<!-- first post -->
<?php $c++;
if( $c == 1) :?>
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="down">a</div>
</div>
</div>
<?php else :?>
<!-- second post -->
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="up">b</div>
</div>
</div>
<?php endif; ?>`
I saw somewhere that you can use a while loop to target the last post from a post.length. However, I am unsure how to implement this.
Yes, you're right. Use count.
Suppose the total posts is 5 for $total_posts = count($posts);.
You'll have to check your $counter for total - 1 as the array is $posts[0], $posts[1], $posts[2], $posts[3], $posts[4].
<?php
if ( have_posts() ) :
$counter = 0;
$total_posts = count($posts) - 1;
while ( have_posts() ) :
the_post();
if( $counter == $total_posts ) {
?>
<div class="container"><?php // CUSTOM CODE FOR LAST ARTICLE ?></div>
<?php
} else {
?>
<div class="container"><?php // CUSTOM CODE FOR THE REST OF ARTICLES ?></div>
<?php
}
$counter++;
endwhile;
endif;
Note that you only need to open <?php and close ?> when changing from PHP to HTML, using them at every line is very confusing.

Categories