https://codepen.io/SitePoint/pen/GQoVey
I am using Tabs and Masonry found here.
I have a total of 4 categories. 4 categories 20 contents
I'm printing this field with wordpress without any problems.
<main role="main" class="grid">
<div class="row">
<div class="col-md">
<div class="starter-template">
<ul class="nav nav-tabs" id="myTab" role="tablist">
<?php
$args = array(
'type' => 'hizmetler',
'parent' => '',
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'kategoriler' /* custom post type texonomy name */
);
$cats = get_categories($args);
foreach ($cats as $cat) {
$cat_id= $cat->term_id;
$cat_name= $cat->name; ?>
<li class="tag is-dark">
<a class="nav-link" id="contact-tab" data-toggle="tab" href="#<?php echo ''.$cat->term_id.''; ?>" role="tab" aria-controls="<?php echo ''.$cat->term_id.''; ?>" aria-selected="false">
<?php echo ''.$cat->name.''; ?>
</a></li>
<?php wp_reset_postdata(); ?>
<?php } ?>
</ul>
I can get category name, id and link as CPT.
The problem starts here.
I don't know how to loop this field.
<div class="tab-content" id="myTabContent">
<div class="tab-pane masonry-container fade show active" id="home" role="tabpanel" aria-labelledby="1-tab">
<div class="card" style="width: 18rem;">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Card with stretched link</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
</div>
</div>
loop area;
<div class="card-body">
<h5 class="card-title">Card with stretched link</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
Go somewhere
</div>
but this field must have a category name related to the parent div.
ex: id="home"
example: <?php echo ''.$cat->term_id.''; ?>
this is what should be
<div class="tab-pane masonry-container fade show active" id="EXAMPLE-CAT-ID-1" role="tabpanel" aria-labelledby="EXAMPLE-CAT-ID-1-tab">
<div class="card" style="width: 18rem;">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">POST 1 TİTLE (cat-1)</h5>
<p class="card-text">POST 1 CONTENT</p>
<p class="card-text">CAT 1 NAME</p>
link Go somewhere
</div>
<div class="card-body">
<h5 class="card-title">POST 2 TİTLE (cat-1)</h5>
<p class="card-text">POST 2 CONTENT</p>
<p class="card-text">CAT 1 NAME</p>
link Go somewhere
</div>
<div class="card-body">
<h5 class="card-title">POST 3 TİTLE (cat-1)</h5>
<p class="card-text">POST 3 CONTENT</p>
<p class="card-text">CAT 1 NAME</p>
link Go somewhere
</div>
</div>
</div>
<div class="tab-pane masonry-container fade show active" id="EXAMPLE-CAT-ID-2" role="tabpanel" aria-labelledby="EXAMPLE-CAT-ID-2-tab">
<div class="card" style="width: 18rem;">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">POST 1 TİTLE (cat-2)</h5>
<p class="card-text">POST 1 CONTENT</p>
<p class="card-text">CAT 2 NAME</p>
link Go somewhere
</div>
<div class="card-body">
<h5 class="card-title">POST 2 TİTLE (cat-2)</h5>
<p class="card-text">POST 2 CONTENT</p>
<p class="card-text">CAT 2 NAME</p>
link Go somewhere
</div>
<div class="card-body">
<h5 class="card-title">POST 3 TİTLE (cat-2)</h5>
<p class="card-text">POST 3 CONTENT</p>
<p class="card-text">CAT 2 NAME</p>
link Go somewhere
</div>
</div>
Place the following code in your functions.php file. You can use it as any shortcode - [tab_posts] or echo do_shortcode('[tab_posts]'); . Just keep in mind to fix html as you need to work your tabs.
function posts_per_tab($atts) {
$output = array();
//Setup our custom post args
$args = array(
'post_type'=>'post',
'posts_per_page' => 3,
'tax_query' => array(
'taxonomy' => 'cat', // Change it with your taxonomy
'field' => 'id',
'terms' => $atts,
)
);
$results = new WP_Query($args);
while($results->have_posts()): $results->the_post();
$output[] .= '<div class="card-body">';
$output[] .= '<h5 class="card-title">'.get_the_title().'</h5>';
$output[] .= '<p class="card-text">'.get_the_content().'</p>';
$output[] .= 'Go somewhere';
$output[] .= '</div>';
endwhile;
wp_reset_query();
return $output;
}
function posts_in_category_tabs() {
$tab_links = array();
$tab_contents = array();
$tab_posts = array();
$cargs = array(
'type' => 'post',
'parent' => '',
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'category'
);
$cats = get_categories($cargs);
if($cats):
foreach($cats as $cat):
//For each category build your tab
$tab_links[] = sprintf('<li>%s</li>',$cat->name);
//We are executing 2nd loop in posts_per_tab function passing category id as parameter
$tab_posts = posts_per_tab($cat->term_id);
//For each tab push the posts per category
$tab_contents[] = sprintf('<div class="tabs-panel">%s</div>',implode( '', $tab_posts ));
endforeach;
wp_reset_postdata();
endif;
//Our navigation
$tab_links = sprintf(
'<ul class="tabs-nav">%s</ul>',
implode( '', $tab_links ),implode( '', $tab_contents )
);
//Change container if needed
return sprintf('<div class="tabs-wrapper">%s %s</div>',$tab_links,implode( '', $tab_contents ));
}
add_shortcode('tab_posts','posts_in_category_tabs');
Related
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>
I wrote a short code for wordpress but the outputs that will be printed is somehow weird
so this is my loop code
function events_accordion_shortcode($category){
// WP_Query arguments
$args = array (
'post_type' => array( 'event' ),
'post_status' => array( 'publish' ),
'nopaging' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
'meta_query' => array(
array(
'key' => 'custom_page_category',
'value' => $category,
)
),
);
// The Query
$events = new WP_Query( $args );
echo '<div class="container">';
// The Loop
$first = true;
if ( $events->have_posts() ) {
echo '<ul class="responsive-table">
<li class="table-header">
<div class="col col-3">Event</div>
<div class="col col-6">date</div>
<div class="col col-6"> location</div>
<div class="col col-6">FOCUS</div>
</li>';
while ( $events->have_posts() ) {
$events->the_post();
$post_id=get_the_ID();
$mydate = get_post_meta($post_id, 'date', true);
$mylocation = get_post_meta($post_id, 'location', true);
$myfocus = get_post_meta($post_id, 'focus', true);
echo '<li class="table-row accordion">' .
'<div class="col col-3" data-label="title">' . the_title() . '</div>' .
'<div class="col col-6" data-label="date">'. $mydate .'</div>' .
'<div class="col col-6" data-label="location">'. $mylocation .'</div>' .
'<div class="col col-6" data-label="focus">'. $myfocus .'</div>' .
'<div class="col col-6" data-label="plus">
<i id="load-more" aria-hidden="true" class="fas fa-plus"></i>
</div>' .
'</li>';
echo '<li class="panel"></li>';
}
} else {
// no posts found
}
echo '</ul></div>';
}
the problem is that the "the_title()" value gets printed outside the li tag, this is the weird output I get:
<div class="container">
<ul class="responsive-table">
<li class="table-header">
<div class="col col-3">Event</div>
<div class="col col-6">date</div>
<div class="col col-6"> location</div>
<div class="col col-6">FOCUS</div>
</li>
event 1
<li class="table-row accordion">
<div class="col col-3" data-label="title"></div>
<div class="col col-6" data-label="date">13/6/2019</div>
<div class="col col-6" data-label="location"></div>
<div class="col col-6" data-label="focus">brain</div>
<div class="col col-6" data-label="plus">
<i id="load-more" aria-hidden="true" class="fas fa-plus"></i>
</div>
</li>
<li class="panel"></li>
</ul>
</div>
as you can see the post title(event 1) has been copied outside the li tag
what am I doing wrong?I dont know what to do
echo '<li class="table-row accordion">' .
'<div class="col col-3" data-label="title">' . get_the_title() . '</div>' .
'<div class="col col-6" data-label="date">'. $mydate .'</div>' .
'<div class="col col-6" data-label="location">'. $mylocation .'</div>' .
'<div class="col col-6" data-label="focus">'. $myfocus .'</div>' .
'<div class="col col-6" data-label="plus">
<i id="load-more" aria-hidden="true" class="fas fa-plus"></i>
</div>' .
'</li>';
echo '<li class="panel"></li>';
you have to write get_the_title(), Because you have already written echo at the beginning of li tag. Read the difference here between get_the_title() and the_title()
I have a loop that should show recent blog posts. The problem is the posts_per_page shows only one post instead of three posts.
I couldn't figure out where I am going wrong.
I tried the following steps but did not help, but did not work for me.
ignore_sticky_posts = > true,
update_post_term_cache=>false,
nopaging=>true
The code is:
<section class="ftco-section" id="blog-section">
<div class="container">
<div class="row justify-content-center mb-5 pb-5">
<div class="col-md-7 heading-section text-center ftco-animate">
<h1 class="big big-2">Blog</h1>
<h2 class="mb-4">Our Blog</h2>
<p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia</p>
</div>
</div>
<div class="row d-flex">
<?php if (have_posts()) :
while (have_posts()) :the_post(); ?>
<div class="col-md-4 d-flex ftco-animate">
<div class="blog-entry justify-content-end">
<a href="<?php the_permalink(); ?>" class="block-20" style="background-image: url('<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>');">
</a>
<div class="text mt-3 float-right d-block">
<div class="d-flex align-items-center mb-3 meta">
<p class="mb-0">
<span class="mr-2">June 21, 2019</span>
Admin
<span class="icon-chat"></span> 3
</p>
</div>
<h3 class="heading"><?php the_title(); ?></h3>
<p><?php echo wp_trim_words(get_the_excerpt(), 30); ?></p>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
</section>
Is this the correct way to loop?
The problem that I'm getting is that now I do not see any of the blog posts.
From what I see in your code, you are displaying your content outside the while loop. Also, you were merging the query arguments array with the $wp_query->query and those are different. You want to use: $wp_query->query_vars
Before you read further, make sure you visit the following links:
query_posts() documentation
The Loop documentation
Your code should be something like:
<?php
global $wpdb;
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 3,
);
$this_query = new WP_Query( $args );
?>
<section class="ftco-section" id="blog-section">
<div class="container">
<div class="row justify-content-center mb-5 pb-5">
<div class="col-md-7 heading-section text-center ftco-animate">
<h1 class="big big-2">Blog</h1>
<h2 class="mb-4">Our Blog</h2>
<p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia</p>
</div>
</div>
<div class="row d-flex">
<?php if ( $this_query->have_posts() ) : ?>
<?php while ( $this_query->have_posts() ) : $this_query->the_post(); ?>
<div class="col-md-4 d-flex ftco-animate">
<div class="blog-entry justify-content-end">
<a href="<?php the_permalink(); ?>" class="block-20" style="background-image: url('<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>');">
</a>
<div class="text mt-3 float-right d-block">
<div class="d-flex align-items-center mb-3 meta">
<p class="mb-0">
<span class="mr-2">June 21, 2019</span>
Admin
<span class="icon-chat"></span> 3
</p>
</div>
<h3 class="heading"><?php the_title(); ?></h3>
<p><?php echo wp_trim_words(get_the_excerpt(), 30); ?></p>
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata();
else : ?>
<p><?php esc_html_e( 'Sorry, no posts found.' ); ?></p>
<?php endif; ?>
</div>
</div>
</section>
How can I create a type of loop in WordPress where an image moves left or right for each post? I.e. first post image will be left, second post image will go right, third post image will go left, and so on...
i am attach image. Provide some code or examples
Thanks in advance
https://imgur.com/Z58QEjb
enter image description here
<?php
$loop = new WP_Query( array( 'post_type' => 'case_studies') );
$Inc = 1;
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php if($Inc==1){ ?>
<div class="col-sm-3 nopadding">
<?php the_post_thumbnail(); ?>
</div>
<div class="col-sm-3 ">
<h2><?php echo get_the_title(); ?></h2>
</div>
<?php }else if($Inc==2){ ?>
<div class="col-sm-3 nopadding">
<?php the_post_thumbnail(); ?>
</div>
<div class="col-sm-3 ">
<h2><?php echo get_the_title(); ?></h2>
</div>
<?php }else if($Inc==3){ ?>
<div class="item">
<div class="col-sm-6 nopadding">
<?php the_post_thumbnail(); ?>
</div>
<div class="col-sm-6">
<h2><?php echo get_the_title(); ?></h2>
</div>
</div>
<?php }else if($Inc==4){ ?>
<div class="item">
<div class="col-sm-6 nopadding">
<?php the_post_thumbnail(); ?>
</div>
<div class="col-sm-6">
<h2><?php echo get_the_title(); ?></h2>
</div>
</div>
<?php } ?>
<?php
if($Inc==4){
$Inc =1;
}
$Inc++;
endwhile;
endif;
wp_reset_postdata();
?>
<?php
$loop = new WP_Query( array( 'post_type' => 'team') );
if ( $loop->have_posts() ) :
$Inc = 0; //start your counter
while ( $loop->have_posts() ) : $loop->the_post();
$person_image = get_field('person_image');
$person_description = get_field('person_description');
?>
<?php if($Inc % 2 == 0){ //if $inc can be created by multiplying 2.?>
<div class="col-md-12">
<div class="col-md-1">
<div>
<img src="<?php bloginfo('template_url'); ?>/images/dot1.png" class="img-responsive dotimages">
</div>
</div> <!--.col-md-1 -->
<div class="col-md-10 row">
<div class="clientimagesarea">
<div class="col-md-6">
<div>
<img src="<?php echo $person_image; ?>" class="img-responsive center-block peopleimages">
<?php //the_post_thumbnail(); ?>
</div>
</div>
<div class="col-md-6">
<div class="clienttext clienttextmarleft">
<?php echo $person_description; ?>
</div>
<div class="col-md-12">
<div class="row ">
<div class="col-md-12 col-xs-12 col-sm-12 paddmargin0">
<div class="col-md-2 col-sm-2 col-xs-4">
<div><img src="<?php bloginfo('template_url'); ?>/images/email-icon2.png" class="img-responsive clientemailicon text-left"></div>
</div>
<div class="col-md-9 col-sm-9 col-xs-8">
<div class="emailid">abx#sitename.com</div>
</div>
</div>
<div class="col-md-12 col-xs-12 col-sm-12 paddmargin0">
<div class="col-md-2 col-sm-2 col-xs-4">
<div><img src="<?php bloginfo('template_url'); ?>/images/call-icon2.png" class="img-responsive clientemailicon2 "></div>
</div>
<div class="col-md-9 col-sm-9 col-xs-8">
<div class="emailid2">+41 79 777 66 45</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php //the_post_thumbnail(); ?>
</div>
<?php }else { //it cant?>
<div class="row col-md-6 col-md-offset-3">
<div class="deviderline">
</div>
</div>
<div class="col-md-12">
<div class="row col-md-10 col-md-offset-1">
<div class="row clientimagesarea">
<div class="col-md-6">
<div class="clienttext">
<p>
<?php echo $person_description; ?>
</p>
</div>
<div class="col-md-12">
<div class="row iconmarginleft">
<div class="col-md-12 col-xs-12 col-sm-12 paddmargin0">
<div class="col-md-2 col-sm-2 col-xs-4">
<div><img src="<?php bloginfo('template_url'); ?>/images/email-icon2.png" class="img-responsive clientemailicon text-left"></div>
</div>
<div class="col-md-9 col-sm-9 col-xs-8">
<div class="emailid">abx#sitename.com</div>
</div>
</div>
<div class="col-md-12 col-xs-12 col-sm-12 paddmargin0">
<div class="col-md-2 col-sm-2 col-xs-4">
<div><img src="<?php bloginfo('template_url'); ?>/images/call-icon2.png" class="img-responsive clientemailicon2 "></div>
</div>
<div class="col-md-9 col-sm-9 col-xs-8">
<div class="emailid2">+91 1234567890</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div>
<img src="<?php echo $person_image; ?>" class="img-responsive center-block peopleimages clienttextmarleft">
</div>
</div>
</div>
</div>
<div class="col-md-1">
<div>
<img src="<?php bloginfo('template_url'); ?>/images/dot3.png" class="img-responsive dotimage3">
</div>
</div>
</div>
<?php } ?>
<?php
$Inc++;
endwhile;
endif;
wp_reset_postdata();
?>
You can follow below code
<div class="row">
<?php
$category = get_the_category();
$slug = $category[0]->slug;
$args = array(
'post_type' => 'post',
'category_name'=>$slug,
'posts_per_page'=> 1,
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();?>
<div class="container">
<div class="img-box">
<?php echo get_the_post_thumbnail(get_the_ID());?>
</img>
</div>
<div class="content">
<?php
$content = get_the_content();
echo wp_trim_words( get_the_content(), 25, '' );
?>
</div>
</div>
<?php endwhile; ?>
</div>
.container{
display: flex;
flex-wrap: wrap;
align-content: space-between;
margin-bottom:40px;
}
.img-box{
width:40%;
}
img{
max-width:100%;
}
.content{
width:60%
}
.container:nth-child(even){
flex-direction: row-reverse;
}
<div class="row">
<div class="container">
<div class="img-box">
<img src="https://dummyimage.com/600x400/000/fff">
</div>
<div class="content">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et.
</div>
</div>
<div class="container">
<div class="img-box">
<img src="https://dummyimage.com/600x400/000/fff">
</div>
<div class="content">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et.
</div>
</div>
Your logic makes sense, I haven't tested this, but this should flib every second item with your code.
<?php
$loop = new WP_Query( array( 'post_type' => 'case_studies') );
if ( $loop->have_posts() ) :
$Inc = 0; //start your counter
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php if($Inc % 2 == 0){ //if $inc can be created by multiplying 2.?>
<div class="col-sm-3 nopadding">
<?php the_post_thumbnail(); ?>
</div>
<div class="col-sm-3 ">
<h2><?php echo get_the_title(); ?></h2>
</div>
<?php }else { //it cant?>
<div class="col-sm-3 ">
<?php echo get_the_title(); ?>
</div>
<div class="col-sm-3 nopadding">
<h2><?php the_post_thumbnail(); ?></h2>
</div>
<?php }
$Inc++; //increment the counter
endwhile;
endif;
wp_reset_postdata();
You have to register your post-type like :
add_action( 'init', 'codex_book_init' );
/**
* Register a book post type.
*
* #link http://codex.wordpress.org/Function_Reference/register_post_type
*/
function codex_book_init() {
$labels = array(
'name' => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
'menu_name' => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
'add_new' => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
'add_new_item' => __( 'Add New Book', 'your-plugin-textdomain' ),
'new_item' => __( 'New Book', 'your-plugin-textdomain' ),
'edit_item' => __( 'Edit Book', 'your-plugin-textdomain' ),
'view_item' => __( 'View Book', 'your-plugin-textdomain' ),
'all_items' => __( 'All Books', 'your-plugin-textdomain' ),
'search_items' => __( 'Search Books', 'your-plugin-textdomain' ),
'parent_item_colon' => __( 'Parent Books:', 'your-plugin-textdomain' ),
'not_found' => __( 'No books found.', 'your-plugin-textdomain' ),
'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'your-plugin-textdomain' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'book', $args );
}
and after the loop is :
$args = array( 'post_type' => 'book', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content() . the_thumbnails();
echo '</div>';
endwhile;
then you can to make some custom fields
i let you working on it
I am making a wp custom theme with bootstrap. I implemented a grid to display the latest posts. I want to display 4 items in a row for larger screens (down to 768px = col-sm-3) and then 2 items per row (col-xs-6). It kinda works but at some point the items are not displaying properly. In particular, when the screen width is between 1200 - 768 or less than 579, 1 row every 4 is broken and only displays 1 item (see images). I'm not sure where the issue is.
I read about the .cleardiv class but I don't understand where I should put it.
You can see the website here. (Please note that the top row with the 4 items is working fine as it is generated before the other 12 in a different row). Just click the "v" arrow to display the rows with the issue.
Thanks
<section class="bg-white" id="in_evidenza">
<div class="container">
<!-- this is the first row which is always visible. no issue here -->
<div class="row"> ...
</div>
<!-- second row: issue here -->
<div class="row" id="news-content">
<?php
$args = array( 'numberposts' => '12', 'category_name' => 'news', 'orderby' => 'date', 'offset' => '4' );
$recent_posts = wp_get_recent_posts( $args );
$i = 1;
foreach( $recent_posts as $recent ):
$post_img_src = "http://www.assatena.it/testbs/wordpress/wp-content/uploads/2016/03/".$i.".png";
$post_title = $recent["post_title"];
$post_id = $recent["ID"];
$post_content = get_post_field('post_content', $post_id);
$post_date = get_the_date("d/m/Y", $post_id);
if(strlen($post_title) >= 19){
$post_title_short = substr($post_title, 0, 19);
$post_title_short.="...";
}else
$post_title_short = $post_title;
$teaser = substr(strip_tags($post_content), 0, 66);
$teaser.=" . . .";
?>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-6" id="col-news-all">
<div class="text-center">
<img class="img-rect" width="160" height="100" alt="" src='<?php echo $post_img_src; ?>' />
<h3><?php echo $post_title_short; ?></h3>
<p>
<?php echo $teaser; ?>
<br> <!-- open -->
<a data-toggle="modal" href="" data-target="#modal-news-<?php echo $post_id; ?>">
<span class="glyphicon glyphicon-volume-up" aria-hidden="true"></span>
</a>
</p>
</div>
</div>
<div id="modal-news-<?php echo $post_id; ?>" class="modal fade in" aria-hidden="false" role="dialog" tabindex="-1">
<div class="modal-content">
<div class="container">
<div class="row">
<div class="col-lg-10 col-lg-offset-1">
<div class="modal-body">
<h2><?php echo $post_title; ?></h2>
<p class="post_date">- <?php echo $post_date; ?> -</p>
<hr class="star-primary">
<img id="news_img" class="img-responsive img-modal" alt="News img" src="<?php echo $post_img_src; ?>" />
<div class=" text-left">
<div class="my_post_content">
<p><?php echo $post_content; ?></p>
</div>
</div>
<div class="text-center"> <!-- close -->
<a data-dismiss="modal" class="btn btn-success btn-xl ">Chiudi</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
$i%=4;
$i++;
endforeach;
?>
</div> <!-- row -->
<div class="text-center">
<a data-toggle="modal" href="" data-target="">
<span id="news-all" class="glyphicon glyphicon-menu-down" aria-hidden="true"></span>
</a>
</div>
</div> <!-- container -->
</section>
images:
Before your $i++ write an if statement to place a clearfix
<?php if ($i % 4 == 0); ?>
<div class="clearfix"></div>
<?php endif; ?>
EDIT: for the mobile you can set another clearfix with $i % 2 == 0 and hide it in desktops via visible-xs