WPquery + add row every nth element - wordpress

So the case if following - i've got a wpquery like on the code below.
<section class="row service_block_row bgf" id="page-<?php the_ID(); ?>">
<div class="container">
<div class="row">
<div class="col-sm-12">
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<div class="row">
<div class="col-sm-12 col-lg-3">
<h1><?php the_title(); ?></h1>
</div>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
</div>
</div>
</div>
</section>
What I would like to achieve is to have the loop working like that:
<ROW>
<COL-LG-3>
<COL-LG-3>
<COL-LG-3>
<COL-LG-3>
</ROW>
SO in fact what i would lke to achieve is to have 4 elements inside row without creating different loops. I know i should use some counter but i have no clue how;/
thanks

Add new row after 4 cols
<section class="row service_block_row bgf" id="page-<?php the_ID(); ?>">
<div class="container">
<div class="row">
<div class="col-sm-12">
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) :
$count=0;
?>
<div class="row">
<?php while ( $parent->have_posts() ) : $parent->the_post();
$count++;
?>
<div class="col-sm-12 col-lg-3">
<h1><?php the_title(); ?></h1>
</div>
<?php
if($count%4==0)
{
echo '</div><div class="row">';
}
endwhile; ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
</div>

Related

Blog Pagination Not showing in my custom template

I created a custom blog template in my WPLMS Wordpress theme, with wp-query. But I'm unable to show the pagination. I tried to build a shortcode its working fine but in the last im unable to show my pagination.
//blog section 2
add_shortcode('da-blog', 'da_blog_post');
function da_blog_post($attr, $content = null){
$attributes = extract( shortcode_atts(array(
'title' => 'blog post',
'subtitle' => 'latest news',
), $attr) );
ob_start(); ?>
<section class="exthree_blog">
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="all_exthree_blogs">
<div class="row">
<?php
$blogcontent = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 6,
'category__not_in' => array( 75 )
));
?>
<?php while($blogcontent->have_posts()) : $blogcontent->the_post(); ?>
<div class="col-md-4">
<div class="single_exthree_blog">
<div class="exthree_box">
<div class="exthree_box_img">
<!-- <img src="img/shutterstock_770159185.jpg" class="img-responsive"> -->
<a href="<?php echo get_permalink() ?>">
<?php if(has_post_thumbnail(get_the_ID())){ ?>
<?php echo get_the_post_thumbnail(get_the_ID(),'medium'); ?>
<?php }else{
$image = vibe_get_option('default_course_avatar');
?>
<img src="<?php echo vibe_sanitizer($image,'url'); ?>" />
<?php
}
$name = get_the_author_meta( 'display_name' );
?>
</a>
</div>
<div class="exthree_box_fortag">
</div>
<div class="boxxx">
<div class="exthree_box_title">
<h4><?php echo get_the_title(); ?></h4>
</div>
<div class="exthree_box_categor_list">
<?php echo get_the_category_list(); ?>
</div>
<div class="exthree_box_desc">
<p><?php echo get_the_excerpt(); ?></p>
</div>
<div class="exthree_box_button">
Continue<img src="http://localhost/gostudent/wp-content/uploads/2020/02/Layer-2.png">
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php echo "helooooooooooooooo"; ?>
<?php $big = 999999999; // need an unlikely integer
$links = paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $next_the_query->max_num_pages,
"type" => "list"
));
echo $links; ?>
</div>
</div>
</div>
</div>
</div>
</section>
<?php return ob_get_clean();
wp_reset_postdata();
}

Splitting WordPress Loops

I want to display a grid of thumbnails & post titles which are custom post types. I am also using fullpage.js which displays content full width & height of the browser window. Within each fullpage 'section', I want to show 6x thumbnails/titles.
How can I split the loop in order to achieve this effect? Here is my code so far:
<?php
$work_args = array(
'post_type' => 'bp_work_post_type',
'post_status' => 'publish',
'posts_per_page' => 6,
'offset' => 6
);
$work_query = new WP_Query( $work_args );
?>
<?php if ( $work_query->have_posts() ) : ?>
<div class="section">
<?php while ( $work_query->have_posts() ) : $work_query->the_post(); ?>
<div class="post-grid">
//Grid Content in here
</div>
<?php endwhile;?>
</div>
<?php endif; ?>
you can use boostrap css to split the div's.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<?php
$work_args = array(
'post_type' => 'bp_work_post_type',
'post_status' => 'publish',
'posts_per_page' => 6,
'offset' => 6
);
$work_query = new WP_Query( $work_args );
?>
<?php if ( $work_query->have_posts() ) : ?>
<div class="section row">
<?php while ( $work_query->have_posts() ) : $work_query->the_post(); ?>
<div class="post-grid col-md-2">
//Grid Content in here
</div>
<?php endwhile;?>
</div>
<?php endif; ?>
Use the modulus symbol (%)
<?php
$work_args = array(
'post_type' => 'bp_work_post_type',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$work_query = new WP_Query( $work_args );
$nb_posts = $work_query->post_count;
$post_per_section = 6;
?>
<?php if ( $work_query->have_posts() ) : ?>
<div class="section">
<?php $count=0; ?>
<?php while ( $work_query->have_posts() ) : $work_query->the_post(); $count++; ?>
<div class="post-grid"></div>
<?php if($count % $post_per_section == 0 && $nb_posts !== $post_per_section ): ?>
</div><div class="section">
<?php endif;?>
<?php endwhile;?>
</div>
<?php endif; ?>

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/

WP Query Multiple Shortcodes not working on same page or template page

I created a Shortcode slider which gets ids of different pages and show display slider. Shordcode works fine but issue is that whenever i am copy past shortcode multiple times on same page/ template page it shows only first one.
This issue occurs only only when i past same type of shortcode but if i past any other shortcode on same page it works fine.
Here is my code
add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
function objectx_pages_list_func( $atts ) {
global $post;
ob_start();
extract( shortcode_atts( array('ids' => '1186'), $atts ) );
$id_array = explode(',', $ids);
$pages_query = new WP_Query( array(
'post_type' => 'page',
'post__in' => $id_array,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $pages_query->have_posts() ) { ?>
<div class="carousel-wrapper">
<div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
<?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
<div class="row">
<div class="col-md-7">
<div class="img-rooms">
<a href="<?php the_permalink(); ?>">
<img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
</div>
</div>
<div class="col-md-5">
<div class="detail-rooms">
<h2 class="title-room "><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable_pages = ob_get_clean();
wp_reset_postdata();
return $myvariable_pages;
}
}
Here is shortcode
[objectx-pages-list id="15,16,17"]
[objectx-pages-list ids="25,26,27"]
here you can see live example
http://objextheme.wpengine.com/
This one working fine
ROOFTOP PATIO & LOUNGE
but this is not working
This Week At Vertigo Sky Lounge
Please guide me where i am doing mistake. Thanks
add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
function objectx_pages_list_func( $atts ) {
global $post;
ob_start();
extract( shortcode_atts( array('ids' => '1186'), $atts ) );
$id_array = explode(',', $ids);
$pages_query = new WP_Query( array(
'post_type' => 'page',
'post__in' => $id_array,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $pages_query->have_posts() ) { ?>
<div class="carousel-wrapper">
<div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
<?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
<div class="row">
<div class="col-md-7">
<div class="img-rooms">
<a href="<?php the_permalink(); ?>">
<img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
</div>
</div>
<div class="col-md-5">
<div class="detail-rooms">
<h2 class="title-room "><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable_pages = ob_get_clean();
wp_reset_postdata();
return $myvariable_pages;
}
}
Here is the error i noticed. id="carousel-rooms" Id repeating on
same page. that is why only one time it runs perfect.

Wordpress posts pagination

I'am creating a website at Wordpress and in my website there exist a post type called "news". What I ask for is I'm showing 5 posts in a page but I couldn't show the "next page" button. My code is below, if anyone can help.
<div class="row10 offset1">
<div class="news">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'news', 'posts_per_page' => 5, 'paged' => $paged );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="row-fluid">
<div class="well-small">
<?php the_title(); ?>
<?php the_content(); ?>
<?php the_date("d.m.Y"); ?>
</div>
</div>
<?php
endwhile;
?>
</div>
<div class="pull-right">
<ul class="pager">
<li> <?php echo previous_posts_link();?></li>
<li><?php echo next_posts_link();?></li>
</ul>
</div>
<div class="clearfix"></div>
</div>
I've solved my problem, but I don't know how it happened. =) I've added query_posts($args); and it worked properly.
<div class="span10 offset1">
<div class="">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'haber', 'posts_per_page' => 3, 'paged' => $paged );
$loop = new WP_Query( $args );
query_posts($args); <--- I added this line and it worked --->
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="row-fluid">
<div class="well-small">
<?php the_title(); ?>
<?php the_content(); ?>
<?php the_date("d.m.Y"); ?>
</div>
</div>
<?php
endwhile;
?>
</div>
<div class="pull-right">
<ul class="pager">
<li> <?php previous_posts_link("Geri"); ?></li>
<li> <?php next_posts_link("İleri"); ?></li>
</ul>
</div>
<div class="clearfix"></div>
</div>

Resources