wp_posts with given design not repeating properly - wordpress

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>

Related

get posts query wordpress

i try to get posts via query wordpress
i need to fetch the posts thats's in spacfic taxonamy
تصميم تطبيقات
<?php
get_query_var('taxonomy');
$taxonomy = 'eposd_category';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy
if ( $terms && !is_wp_error( $terms ) ) :
?>
<?php foreach ( $terms as $term ) { ?>
<div class="col-sm-3">
<div class="item">
<div class="load">
<?php echo z_taxonomy_image($lug term->term_s); ?>
<div class="load-icon text-center">
<i class="fa fa-play"></i>
</div>
</div>
<div class="item-content">
<h2><?php echo $term->name; ?></h2>
</div>
</div>
</div>
<?php } ?>
<?php endif;?>
تصميم العاب

How to dynamic bootstrap 4 tabs in wordpress

I want to display the product in the tab category.
I want it to display 4 items in each tab but it currently displays all product.
Do you have a solution to help me with this?
I do not want the products to be displayed in blocks, it should be displayed in tabular.
Currently displays all products from all categories but does not display each product as a specific category.
This is my code.
<div class="container pt-5">
<div class="text-center mb-4">
<h2 class="section-title px-5"><span class="px-2">Category products</span></h2>
</div>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<?php
$categories = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => 1,
) );
foreach ( $categories as $key => $cat ) { ?>
<li class="nav-item" role="presentation">
<a class="nav-link " id="<?php echo $cat->slug; ?>" data-toggle="tab" href="#<?php echo $cat->slug; ?>" role="tab" aria-controls="<?php echo $cat->slug; ?>" aria-selected="true"><?php echo $cat->name; ?></a>
</li>
<?php } ?>
</ul>
<?php foreach ( $categories as $key => $cat ) {
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'product_cat' => $cat->slug,
'hide_empty' => 1,
'orderby' => 'name',
'order' => 'ASC'
);
?>
<div class="tab-content" id="<?php echo $cat->slug; ?>">
<?php
$products = new WP_Query( $args );
if( $products->have_posts() ) : while ( $products->have_posts() ) : $products->the_post(); ?>
<div class="tab-pane<?php echo $cat->slug; ?>" id="<?php echo $cat->slug; ?>" role="tabpanel" aria-labelledby="home-tab">
<div class="container pt-2">
<div class="row px-xl-5 pb-3">
<div class="col-lg-3 col-md-6 col-sm-12 pb-1">
<div class="card product-item border-0 mb-4">
<div class="card-header product-img position-relative overflow-hidden bg-transparent border p-0">
<?php the_post_thumbnail('', array('class' => 'img-fluid w-100')); ?>
</div>
<div class="card-body border-left border-right text-center p-0 pt-4 pb-3">
<h6 class="text-truncate mb-3"><?php the_title(); ?></h6>
<div class="d-flex justify-content-center">
<h6><?php echo "". " " . get_woocommerce_currency_symbol().$product->get_price(); ?></h6>
</div>
</div>
<div class="card-footer d-flex justify-content-between bg-light border">
<i class="fas fa-eye text-primary mr-1"></i>View Detail
<i class="fas fa-shopping-cart text-primary mr-1"></i> Add to Card
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); endif; ?>
</div>
<?php } ?>
</div>
Your loop was off a little bit, you also used the attribute ID multiple times. This should work for you.
<div class="container pt-5">
<div class="text-center mb-4">
<h2 class="section-title px-5"><span class="px-2">Category products</span></h2>
</div>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<?php
$product_cats = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => 1,
)
);
foreach ( $product_cats as $key => $product_cat ) {
?>
<li class="nav-item" role="presentation">
<a class="nav-link " id="<?php echo esc_attr( $product_cat->slug ); ?>-tab" data-toggle="tab" href="#<?php echo esc_attr( $product_cat->slug ); ?>" role="tab" aria-controls="<?php echo esc_attr( $product_cat->slug ); ?>" aria-selected="false"><?php echo esc_html( $product_cat->name ); ?></a>
</li>
<?php } ?>
</ul>
<div class="tab-content">
<?php
$c = 0;
foreach ( $product_cats as $key => $product_cat ) {
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'product_cat' => $product_cat->slug,
'hide_empty' => 1,
'orderby' => 'name',
'order' => 'ASC',
);
?>
<div class="tab-pane <?php echo ( 0 === $c ) ? 'show active' : 'fade'; ?>" id="<?php echo esc_attr( $product_cat->slug ); ?>" role="tabpanel" aria-labelledby="<?php echo esc_attr( $product_cat->slug ); ?>-tab"
<div class="container pt-2">
<div class="row px-xl-5 pb-3">
<?php
$products = new WP_Query( $args );
if ( $products->have_posts() ) :
while ( $products->have_posts() ) :
$products->the_post();
?>
<div class="col-lg-3 col-md-6 col-sm-12 pb-1">
<div class="card product-item border-0 mb-4">
<div class="card-header product-img position-relative overflow-hidden bg-transparent border p-0">
<?php the_post_thumbnail( '', array( 'class' => 'img-fluid w-100' ) ); ?>
</div>
<div class="card-body border-left border-right text-center p-0 pt-4 pb-3">
<h6 class="text-truncate mb-3"><?php the_title(); ?></h6>
<div class="d-flex justify-content-center">
<h6><?php echo '' . ' ' . get_woocommerce_currency_symbol() . esc_attr( $product->get_price() ); ?></h6>
</div>
</div>
<div class="card-footer d-flex justify-content-between bg-light border">
<i class="fas fa-eye text-primary mr-1"></i>View Detail
<i class="fas fa-shopping-cart text-primary mr-1"></i> Add to Card
</div>
</div>
</div>
<?php
endwhile;
?>
<?php
wp_reset_postdata();
endif;
?>
</div>
</div>
<?php
$c++;
}
?>
</div>
</div>

Wordpress query category name and related posts

I am trying to query Wordpress custom posts and categories they are related to.
Query acts strangely, displays category name and all customs posts, not even related to that category (keeps repeating till all categorie names are displayed.)
Example image : Wordpress custom query display
My query code:
<div id="page-content-wrapper">
<div class="container-fluid">
<div class="lookbook-header">
<div class="wrap">
<p class="text-left">lookbook</p>
</div>
</div>
<?php
$taxonomy = 'lookbook_categories';
$terms = get_terms($taxonomy);
$args=array(
'taxonomy' => 'lookbook_categories'
'post_type' => 'lookbook',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if ( $terms && !is_wp_error( $terms ) ) :
foreach ( $terms as $term ) { ?>
<div class="lookbook-category">
<p class="text-center">
<?php echo $term->name; ?>
</p>
</div>
<?php
if( $my_query->have_posts() ) {
echo '';
$count=0;
while ($my_query->have_posts()) : $my_query->the_post();
if($count == 3) {?>
<div class="row">
<?php }
?>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4 no-padding">
<div class="lookbook-item">
<div class="hvrbox">
<?php
$image = get_field('lookbook_image');
if( !empty($image) ): ?> <img src="<?php echo $image['url']; ?>" alt="news" class="img-responsive" />
<?php endif; ?>
<div class="hvrbox-layer_top">
<div class="hvrbox-text">
<div class="separator"></div>
<h3><?php the_title();?></h3>
<div class="separator"></div>
<p>
<?php the_field('excerpt');?>
</p>
</div>
</div>
</div>
</div>
</div>
<?php
$count++;
if($count == 3) echo '</div>';
endwhile;
}
}
endif;
wp_reset_query();
?>
</div>
</div>
I would like to display category name + posts related to category (not all posts over and over again)
You want to pass in an array to get_terms like:
$terms = get_terms( array(
'taxonomy' => 'lookbook_categories',
'hide_empty' => false,
) );
you can read more about it here: https://developer.wordpress.org/reference/functions/get_terms/

Get all wordpress posts and display 3 posts in a row

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 } ?>

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