Get all wordpress posts and display 3 posts in a row - wordpress

I have a custom post type that use advance custom field to enable users to enter content. I want to get all that posts and display them 3 posts in a row. Let say:
<div class="row">
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
</div>
<div class="row">
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
</div>
...
The problem is that it displays all the posts in the <div class="row"></div> like this:
<div class="row">
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
<div class="col-md-4">...</div>
...
</div>
Below is my code that I have tried so far:
<div class="row">
<?php
$args = array(
'post_type' => 'team',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part('content', 'team');
}
?>
</div>
My content_team.php:
<div class="col-md-4">
<h3><?php the_field('name'); ?></h3>
<img src="<?php the_field('photo'); ?>" alt="<?php the_field('name'); ?>" />
<p class="smallTitle"><?php the_field('position'); ?></p>
<p><?php the_field('biography'); ?></p>
</div>

Try below :-
<div class="row">
<?php
$i = 1;
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part('content', 'team');
if ($i % 3 == 0){ ?>
</div><div class="row">
<?php } ?>
<?php $i++; ?>
<?php } ?>

Related

wp_posts with given design not repeating properly

I used bootstrap 4 to design the blog page. Design is well but when I am using wp_posts design not working properly.
<?php
global $post;
$events = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'events',
'posts_per_page' => 4,
);
$arr_posts = new WP_Query( $events );
$myposts = get_posts( $events );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="row single-event">
<div class="col-6 p-0 m-0">
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
</div>
<div class="col-6 event-desc">
<?php the_title() ?>
<p class="exerpt">
<?php if ( is_category() || is_archive() ) {
echo excerpt(30);
} else {
echo content(30);
}
?>
</p>
<p class="date">Event Date: <span><?php echo get_the_date('F j, Y'); ?></span></p>
Learn More..
</div>
</div>
<?php endforeach;
wp_reset_postdata();
?>
Here is my site https://rccsl.com/events/
Please help. Thanks in advance.
Please use below code
<?php
global $post;
$events = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 4,
);
$arr_posts = new WP_Query( $events );
$myposts = get_posts( $events );
foreach ( $myposts as $post ) { setup_postdata( $post )?>
<div class="row single-event">
<div class="col-6 p-0 m-0">
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
</div>
<div class="col-6 event-desc">
<?php the_title() ?>
<p class="exerpt">
<?php if ( is_category() || is_archive() ) {
echo "test";
} else {
echo "test";
}
?>
</p>
<p class="date">Event Date: <span><?php echo get_the_date('F j, Y'); ?></span></p>
Learn More..
</div>
</div>
<?php } ?>
It seems that the html code is broken when it's output. If you inspect your website with the developers tools you will see that instead of having an output of:
<div class="row single-event">
<div class="col-6 p-0 m-0"></div>
<div class="col-6 p-0 m-0"></div>
</div>
<div class="row single-event">
<div class="col-6 p-0 m-0"></div>
<div class="col-6 p-0 m-0"></div>
</div>
<div class="row single-event">
<div class="col-6 p-0 m-0"></div>
<div class="col-6 p-0 m-0"></div>
</div>
You have as a result:
<div class="row single-event">
<div class="col-6 p-0 m-0"></div>
<div class="col-6 p-0 m-0"></div>
<div class="row single-event">
<div class="col-6 p-0 m-0"></div>
<div class="col-6 p-0 m-0"></div>
</div>
<div class="row single-event">
<div class="col-6 p-0 m-0"></div>
<div class="col-6 p-0 m-0"></div>
</div>
</div>
First of all add a semicolon (;) after the_title()
the_title();
Then try to replace
<p class="exerpt">
<?php if ( is_category() || is_archive() ) {
echo excerpt(30);
} else {
echo content(30);
}
?>
</p>
With:
<div class="exerpt">
<?php if ( is_category() || is_archive() ) {
echo excerpt(30);
} else {
echo content(30);
}
?>
</div>

Wordpress posts page is not showing posts list

Im trying to setup custom posts page. I was trying to use home.php, and custom page template. After that i choose in the setting part "Reading Settings" my blog page. Problem is that when i go on www.domain.tld/blog im getting single blog post page, not a list of blogs. When i switch blog template to some other url, then everything is fine but still is not using custom template. In that case wp are using home.php
home.php
<?php
/**
* Blog listing
*/
?>
<?php get_header() ?>
<div id="blog" class="page_wrapper">
<div class="hedaer_mini_part">
<?php get_template_part('parts/navigation'); ?>
<div class="container">
<hr>
<h1>Blog</h1>
<div class="search">
<?php echo get_search_form( ); ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 0;
$query = new WP_Query(array('category_name' => 'blog', 'post_type' => 'post', 'paged' => $paged));
if ($query->have_posts() || $paged >= 1 && $paged <= $wp_query->max_num_pages) :
?>
</div>
</div>
</div>
<div class="blog_lista">
<div class="container">
<div class="news_list_wrapper">
<?php
// Start the loop.
while ($query->have_posts()) : $query->the_post();
?>
<div class="news_list_item ffs-able">
<a href="<?php the_permalink(); ?>">
<div class="row">
<div class="col-sm-5">
<div class="news_list_image">
<!-- <img src="" /> -->
<?php if(has_post_thumbnail()) { ?>
<img src="<?php the_post_thumbnail_url('list_size'); ?>" alt="<?php the_title() ?>" />
<?php } ?>
</div>
</div>
<div class="col-sm-7">
<div class="news_list_content">
<h3 class="title"><?php the_title() ?></h3>
<div class="body">
<?php echo wp_trim_words(get_the_content(), 90); ?>
</div>
<div class="date">
<?php the_time('j. F Y.'); ?>
</div>
</div>
</div>
</div>
</a>
</div>
<?php
// End the loop.
endwhile;
?>
</div>
<div class="news_pagination">
<hr>
<?php the_posts_pagination(array('mid_size' => 1, 'prev_text' => '<', 'next_text' => '>')); ?>
</div>
<?php
else :
echo 'There is no blog posts..';
endif;
?>
</div>
</div>
</div>
<?php get_footer(); ?>
You need to add these codes to index.php and not home.php. That might fix the issue.

Wordpress Loop repeating twice

Hello I have a while loop in WordPress and I am adding an active class on the first instance of the loop and this works fine but the same instance then again repeats afterwards without the active class.
here is my code any help would be greatly appreciated.
<div class="carousel-inner" role="listbox">
<?php
query_posts( array( 'post_type' => 'deal','dealtype' => 'deals' ) );
while (have_posts()) : the_post();
?>
<?php if( $wp_query->current_post == 0 && !is_paged() ) { ?>
<div class="item active">
<div class="row">
<div class="col-md-6 col-md-offset-1 col-sm-6 col-sm-offset-1">
<?php
$thumb_id = get_field('featured_image');
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'full', true);
$thumb_url = $thumb_url_array[0];
?>
<img src="<?php echo $thumb_url; ?>" class="img-responsive" alt="" />
</div>
<div class="col-md-4 col-sm-4">
<div class="featuredContent">
<div class="contentTitle">
<h2><?php echo get_the_title(); ?> </h2>
</div>
<div class="contentDetails">
<p><?php echo get_field('sub_title'); ?>
</p>
</div>
<?php if(get_field('max_amount')) { ?>
<div class="contentDetails">
<p>Up to
<?php the_field('max_amount'); ?>
Shares</p>
</div>
<?php } ?>
<!--
<div class="contentLink">
<p>invest now</p>
</div>
<div class="contentLink">
<p>follow offering</p>
</div>
-->
</div>
</div>
</div>
</div>
<?php } ?>
<div class="item">
<div class="row">
<div class="col-md-6 col-md-offset-1 col-sm-6 col-sm-offset-1">
<?php
$thumb_id = get_field('featured_image');
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'full', true);
$thumb_url = $thumb_url_array[0];
?>
<img src="<?php echo $thumb_url; ?>" class="img-responsive" alt="" />
</div>
<div class="col-md-4 col-sm-4">
<div class="featuredContent">
<div class="contentTitle">
<h2><?php echo get_the_title(); ?> </h2>
</div>
<div class="contentDetails">
<p><?php echo get_field('sub_title'); ?>
</p>
</div>
<?php if(get_field('max_amount')) { ?>
<div class="contentDetails">
<p>Up to
<?php the_field('max_amount'); ?>
Shares</p>
</div>
<?php } ?>
<!--
<div class="contentLink">
<p>invest now</p>
</div>
<div class="contentLink">
<p>follow offering</p>
</div>
-->
</div>
</div>
</div>
</div>
<?php endwhile; // end of the loop. ?>
</div>
Believe you need to add an else statement after your <?php if ($wp_query->current_post == 0 && !is_paged() ) { ?> statement ends. Should look something like this:
<div class="carousel-inner" role="listbox">
<?php
query_posts( array( 'post_type' => 'deal','dealtype' => 'deals' ) );
while (have_posts()) : the_post();
?>
<?php if ( $wp_query->current_post == 0 && !is_paged() ) { ?>
<div class="item active">
<div class="row">
<div class="col-md-6 col-md-offset-1 col-sm-6 col-sm-offset-1">
<?php
$thumb_id = get_field('featured_image');
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'full', true);
$thumb_url = $thumb_url_array[0];
?>
<img src="<?php echo $thumb_url; ?>" class="img-responsive" alt="" />
</div>
<div class="col-md-4 col-sm-4">
<div class="featuredContent">
<div class="contentTitle">
<h2><?php echo get_the_title(); ?> </h2>
</div>
<div class="contentDetails">
<p><?php echo get_field('sub_title'); ?>
</p>
</div>
<?php if(get_field('max_amount')) { ?>
<div class="contentDetails">
<p>Up to
<?php the_field('max_amount'); ?>
Shares</p>
</div>
<?php } ?>
<!--
<div class="contentLink">
<p>invest now</p>
</div>
<div class="contentLink">
<p>follow offering</p>
</div>
-->
</div>
</div>
</div>
</div>
<?php } else { ?>
<div class="item">
<div class="row">
<div class="col-md-6 col-md-offset-1 col-sm-6 col-sm-offset-1">
<?php
$thumb_id = get_field('featured_image');
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'full', true);
$thumb_url = $thumb_url_array[0];
?>
<img src="<?php echo $thumb_url; ?>" class="img-responsive" alt="" />
</div>
<div class="col-md-4 col-sm-4">
<div class="featuredContent">
<div class="contentTitle">
<h2><?php echo get_the_title(); ?> </h2>
</div>
<div class="contentDetails">
<p><?php echo get_field('sub_title'); ?>
</p>
</div>
<?php if(get_field('max_amount')) { ?>
<div class="contentDetails">
<p>Up to
<?php the_field('max_amount'); ?>
Shares</p>
</div>
<?php } ?>
<!--
<div class="contentLink">
<p>invest now</p>
</div>
<div class="contentLink">
<p>follow offering</p>
</div>
-->
</div>
</div>
</div>
</div>
<?php } ?>
<?php endwhile; // end of the loop. ?>
</div>

Related posts WordPress

I'm looking for a solution to show related posts from a different CPT.
There are two CPT's (Projects and Products) both have multiple sub categories (don't know if that ok...).
When on a: Single Post > Projects CPT > SportsCars
in the footer it must show the latest two posts of: Products CPT > Sportscars
The code i have now just filters on the CPT, not the SubCategrories. This is the code of single_products.php:
<section class="content">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
</section>
<div class="container">
<div class="divider-small"></div>
</div>
<section class="news">
<div class="container">
<h2>Related projects</h2>
<div class="row">
<?php
$wp_query = new WP_Query(array('post_type' => 'projects', 'post__not_in' => array( $post->ID ), 'showposts' => 2));
$i = 0;
if ( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="col-md-6 item">
<div class="<?php if($i++ == 1): echo 'green'; else: echo 'blue'; endif; ?> clearfix">
<div class="col-sm-6">
<div class="text">
<h3><?php the_title(); ?></h3>
<p><?php echo excerpt(20); ?></p>
</div> <!-- end text -->
<div class="meer-news">
Lees meer >
</div> <!-- end meer-news -->
</div> <!-- end col-sm-6 -->
<div class="col-sm-6">
<div class="image">
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail( 'news_latest' );
else:
// backup
endif;
?>
</div> <!-- end image -->
</div> <!-- end col-sm-6 -->
</div>
</div> <!-- end item -->
<?php endwhile; endif; wp_reset_query(); ?>
</div> <!-- end row -->
</div> <!-- end container -->
</section>

not getting the content part in post only thumbnail and title is display

<div class="row" id="content">
<?php
$args = array( 'posts_per_page' =>3, 'category' =>3 );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) :
?>
<div class="col-md-8">
<h2 style="text-transform:uppercase;font-size: 19px;font-weight:bold;"><?php the_title();?></h2>
<div class="col-md-6">
<div class="thumbnails">
<?php the_post_thumbnail('full');?>
</div>
</div>
<div class="col-md-6">
<?php echo the_content(); ?>
<?php echo the_excerpt(); ?>
</div>
<div class="bar3"></div>
</div>
<?php endforeach; ?>
<div class="col-md-4">
<?php get_sidebar(); ?>
</div>
</div>
This is my code .i did not get the content from the the post the thumbnail and the title are display ..please anybody help me to solve the problem.sorry for my poor english.
Replace your code with this...
<div class="row" id="content">
<?php
$args = array( 'posts_per_page' =>3, 'category' =>3 );
$rand_posts = new WP_Query( $args );
if ( $rand_posts->have_posts() ) :
while ( $rand_posts->have_posts() ) : $rand_posts->the_post();
?>
<div class="col-md-8">
<h2 style="text-transform:uppercase;font-size: 19px;font-weight:bold;"><?php the_title();?></h2>
<div class="col-md-6">
<div class="thumbnails">
<?php the_post_thumbnail('full');?>
</div>
</div>
<div class="col-md-6">
<?php the_content(); ?>
<?php the_excerpt(); ?>
</div>
<div class="bar3"></div>
</div>
<?php
endwhile;
endif;
wp_reset_query();
?>
<div class="col-md-4">
<?php get_sidebar(); ?>
</div>
</div>
I hope it will work...
Try this:
<div class="row" id="content">
<?php
$args = array( 'posts_per_page' =>3, 'category' =>3 );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : setup_postdata($post);
?>
<div class="col-md-8">
<h2 style="text-transform:uppercase;font-size: 19px;font-weight:bold;"><?php the_title();?></h2>
<div class="col-md-6">
<div class="thumbnails">
<?php the_post_thumbnail('full');?>
</div>
</div>
<div class="col-md-6">
<?php the_content(); ?>
<?php the_excerpt(); ?>
</div>
<div class="bar3"></div>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<div class="col-md-4">
<?php get_sidebar(); ?>
</div>
</div>
You are missing setup_postdata( $post );
Also when you use the_content(); you don't need to add "echo", same with the other queries start with "the_"
Please check the get_posts query here : http://codex.wordpress.org/Template_Tags/get_posts
Cheers!

Resources