WordPress Dividing posts over columns dynamically - php

I've been building a WordPress template based on inuitcss to get a responsive page.
In this template, I'm attempting to get the WP posts from a certain category, and divide them over a predefined amount of columns. To achieve this I'm counting the amount of posts from category and compute the necessary data for the posts to split up evenly.
I've been using this (last on the page) example and updated several deprecated functions.
The issue I am facing at the moment is that all columns are loading the same posts, and they are only loading the last two instead of all available posts in the category.
The code below is what I am using at the moment, the result can be found at http://www.alikivanderkruijs.com/wp (click on 'Projects').
I've been sitting on this for a while and can't seem to get it right. I hope someone can help me out!
<?php
$proj_posts = get_posts('cat=15');
$count_proj_posts = count($proj_posts);
$split_proj_posts = round($count_proj_posts/2);
?>
<div class="gw">
<div id="menuwrap">
<h3 class="work">PROJECTS</h3>
<div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php query_posts('cat=15&showposts=' . $split_proj_posts . ''); ?>
<?php $posts = get_posts('posts_per_page=' . $split_proj_posts . '&offset=0'); foreach ($posts as $post) : the_post(); ?>
<?php static $proj_count1 = 0; if ($proj_count1 == $split_proj_posts) { break; } else { ?>
<div <?php post_class(); ?>>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<?php $proj_count1++; } ?>
<?php endforeach; ?>
</div>
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php rewind_posts(); ?>
<?php query_posts('cat=15&showposts=' . $split_proj_posts . ''); ?>
<?php $posts = get_posts('posts_per_page=' . $split_proj_posts . '&offset=' . $split_proj_posts . ''); foreach ($posts as $post) : the_post(); ?>
<?php static $proj_count2 = 0; if ($proj_count2 == $split_proj_posts) { break; } else { ?>
<div <?php post_class(); ?>>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</div>
<?php $proj_count2++; } ?>
<?php endforeach; ?>
</div>
</div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
</div>

You are overcomplicating it, simply get all the posts and count up until half, then echo out the closing and opening tags to create a new row:
<div class="gw">
<div id="menuwrap">
<h3 class="work">PROJECTS</h3>
<div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php
$args = array(
'numberposts' => -1,
'posts_per_page' => '',
'offset' => 0,
'category' => '15',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true);
$posts_array = get_posts($args);
$split_at = round(count($posts_array)/2)-1;
$count = 0;
foreach ($posts_array as $post_object):setup_postdata($post);
?>
<div <?php post_class(); ?>>
<h4><a href="<?php the_permalink() ?>" rel="bookmark"
title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h4>
<?php the_content(); ?>
</div>
<?php
//IF we are halfway through, close 1st row and
start a new one
if ($count == $split_at) {
?>
</div>
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php
}
$count++; ?>
<?
endforeach;
?>
</div>
</div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
</div>
</div>
I have left the full args array, incase it is usefull to others, but you only need to use the cat argument in your case.
EDIT ok, setup_postdata is flawed, just checked the docs and it doesnt work as expected. The suggested workaround is to set the global post object like so:
setup_postdata( $GLOBALS['post'] =& $post_object )
Change that section should fix that issue. The count problem is still weird though.

Related

PHP Wordpress loop causing cards to render inside cards instead of separately

I have a simple loop on a wordpress homepage that pulls in 3 random articles. It's intermittently rendering the cards inside the other cards instead of 3 separate cards. I feel like it's a timing issue being caused from pulling them in randomly. Is there a way to force them to render separately?
<div class="row">
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => '3'
);
$loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="card" data-url="<?php echo the_permalink(); ?>">
<div class="card-body">
<?php the_post_thumbnail('homepage-thumbs', array( 'class' => 'aligncenter' )); ?>
<h3><?php the_title(); ?></h3>
<span><i class="fal fa-user"></i> by <?php the_author(); ?></span>
<p></p>
<p><?php echo substr(get_the_content(), 0, 128); ?>...</p>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
Your code works in my test. However, I would use the wp_trim_words to create your excerpt rather than what you did, which could cut a word in the middle.
<div class="row">
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => '3'
);
$loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="card" data-url="<?php echo the_permalink(); ?>">
<div class="card-body">
<?php the_post_thumbnail('homepage-thumbs', array( 'class' => 'aligncenter' )); ?>
<h3><?php the_title(); ?></h3>
<span><i class="fal fa-user"></i> by <?php the_author(); ?></span>
<p></p>
<p><?php
$theContent = get_the_content();
// strip out any shortcodes from the excerpt
$theContent = strip_shortcodes( $theContent );
// wp_trim_words($content, number_of_words, read_more_text)
echo wp_trim_words( $theContent, 30 , '...' );?></p>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
Try to add wp_reset_postdata after your loop, like
<?php endwhile; wp_reset_postdata(); ?>

WordPress loop alternate rows and columns each two posts with bootstrap

I want to create a loop for wordpress that returns each two posts inside its own div and alternating columns every new row (see example)... Im not experimented in php enough to make this happen. I dont manage to get it working appropiatly. And see how to make the last div to bee 100% width if it does not have another column.
I would appreciate your support to make this happen since I tried many things and still no luck. (im using visual composer bootstrap classes, it does work but not as expected.This is the example I want to create
This is my code:
<?php
$args = array(
'posts_per_page' => '-1',
'post_type' => 'inversion',
'category_name' => '',
'order' => 'DESC',
'orderby' => 'DATE'
);
$the_query = new WP_Query( $args );?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="vc_row">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); $i++; $imagen = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<?php if(($i % 2) == 2) : ?>
<div class="vc_col-sm-6">
<div class="vc_row vc_row-fluid">
<div class="vc_col-sm-6 cont-izq">
<h3><?php the_title(); ?></h3>
</div>
<div class="vc_col-sm-6 cont-der" >
<a class="click-info">Más Información</a>
<div class="img-dentro kenburns-top" style="background:url(<?php echo $imagen; ?>)no-repeat; background-size:cover;">
</div>
</div>
</div>
</div>
<?php else : ?>
<div class="vc_col-sm-6">
<div class="vc_row vc_row-fluid">
<div class="vc_col-sm-6 cont-der" >
<a class="click-info">Más Información</a>
<div class="img-dentro kenburns-top" style="background:url(<?php echo $imagen; ?>)no-repeat; background-size:cover;">
</div>
</div>
<div class="vc_col-sm-6 cont-izq">
<h3><?php the_title(); ?></h3>
</div>
</div>
</div>
<?php endif; endwhile; ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
[EDIT]Try this:
<?php
$args = array(
'posts_per_page' => '-1',
'post_type' => 'inversion',
'category_name' => '',
'order' => 'DESC',
'orderby' => 'date',
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="vc_row">
<?php
$float_class = '';
while ( $the_query->have_posts() ) :
$the_query->the_post();
if ( $the_query->current_post &&
$the_query->current_post % 2 === 0 ) {
$float_class = $float_class ? '' : 'vc_pull-right';
}
$imagen = get_the_post_thumbnail_url( get_the_ID(), 'full' );
?>
<div class="vc_col-sm-6">
<div class="vc_row vc_row-fluid">
<div class="vc_col-sm-6 cont-der <?php echo $float_class; ?>">
<a class="click-info">Más Información</a>
<div class="img-dentro kenburns-top" style="background:url('<?php echo esc_url( $imagen ); ?>') no-repeat; background-size:cover;">
</div>
</div>
<div class="vc_col-sm-6 cont-izq">
<h3><?php the_title(); ?></h3>
</div>
</div>
</div>
<?php endwhile; // end have_posts() loop ?>
</div><!-- .vc_row -->
<?php endif; // end have_posts() check ?>
<?php wp_reset_query(); ?>

How do you print an entire post into a page (wordpress)

Hi I've made a custom post type 'work_fields' that calls in information from yet another custom post type 'members' into the post, and now I'm trying to make a PAGE TEMPLATE that shows a list of the titles of custom post type 'work_fields', and when you click a title, the whole post('work_fields') will show up on a div called 'single-post-container' below the titles. right now I've got everything working fine, but I want to display a post in the div 'single-post-container' when the page loads. (as of now, just the titles of the posts are displayed and there is nothing in the div). How do I get the div to display the most recent post of custom post type 'work_fields' on page load? This is the code for the custom page template.
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<h2><?php the_title(); ?></h2>
</div>
</div>
<div class="row halfsection">
<div class="small-12 medium-10 large-offset-1 columns">
<div class="category_container">
<?php
$args = array('post_type' => 'work_fields',);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<p class="category_item"><a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a></p>
<?php endwhile; endif; ?>
</div>
</div>
</div>
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<hr>
</div>
</div>
<div id="single-post-container">
//THIS IS WHERE THE POST CONTENTS SHOWS BUT I WANT THE MOST RECENT POST TO BE HERE ON PAGE LOAD, BEFORE I CLICK ANY OTHER POST//
</div>
Thank you! Your help is much appreciated!
Just use the WP_query twice by getting recent posts in the arguments,
$args2 = array('post_type' => 'work_fields', 'orderby' => 'ID', 'order'=> 'DESC' , 'posts_per_page' => 5);
$query2 = new WP_Query( $args2 );
?><div id="single-post-container"><?php
// The Loop
if ( $query2->have_posts() ) {
echo '<ul>';
while ( $query2->have_posts() ) {
$query2->the_post();
echo '<li>' . get_the_content() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
}
?></div><?php
Put the div outside loop. It will show the content of recent 5 posts.
If everything above the div single-post-container is working fine then for this specific div you can load most recent post by using code below. Be sure to reset previous post data using wp_reset_postdata()
Codex Documentation. https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'work_fields',
'post_status' => 'publish',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
So I just recreated the post type markup on my page. I'm sure there's a better way to do this but for times sake I had to at least make it work. I also tried using a jquery onclick function and just click the first title after everything loads, but there was an error that just kept pushing all of the titles so I pretty much gave up.
here's the code
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<h2><?php the_title(); ?></h2>
</div>
</div>
<div class="row halfsection">
<div class="small-12 medium-10 large-offset-1 columns">
<div class="category_container">
<?php
$args = array(
'post_type' => 'work_fields',
);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<p class="category_item"><a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a></p>
<?php endwhile; endif; ?>
</div>
</div>
</div>
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<hr>
</div>
</div>
<div id="single-post-container">
<?php
$args2 = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'work_fields',
'post_status' => 'publish',
'posts_per_page' => 1,
);
$query2 = new WP_Query( $args2 );
if ( $query2->have_posts() ) : ?>
<?php $post = get_post($_POST['id']); ?>
<div id="single-post post-<?php the_ID(); ?>">
<?php while ( $query2->have_posts() ) : $query2->the_post(); ?>
<div class="row section">
<div class="small-12 medium-7 large-offset-1 columns">
<h2><?php the_title(); ?></h2>
<h3>소개</h3>
<p class="halfsection"><?php the_field('work_fields_intro'); ?></p>
<h3>주요서비스</h3>
<p class="halfsection"><?php the_field('work_fields_service'); ?></p>
<h3>주요실적</h3>
<p class="halfsection"><?php the_field('work_fields_accomplishment'); ?></p>
</div>
<?php endwhile; endif; ?>
<div class="small-6 medium-3 large-2 columns large-offset-1 end">
<?php
$posts = get_field('team_member');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<div class="member_container halfsection">
<div class="member"><?php the_post_thumbnail(); ?></div>
<p class="member_name"><?php the_title(); ?></p>
<ul class="members_info">
<li><?php the_field('members_position'); ?></li>
<li><?php the_field('members_e-mail'); ?></li>
<li><?php the_field('members_phone'); ?></li>
</ul>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>

Wordpress pagination not visible from loop

I'm doing a blog with Wordpress and the post pagination doesn't appear. The post list is shown correctly, but there is only 5 post (I have 8) and pagination isn't working.
<?php
if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="all-post">
<?php
$args = array('category' => '');
$myposts = get_posts($args);
foreach ($myposts as $post) : setup_postdata($post); ?>
<div class="post">
<div class="post-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="post-details">
<div class="post-meta">
<span class="date"><?php the_time('j M, Y'); ?></span>
<!-- Categories -->
<span class="categories">
<?php
$category_ids = get_all_category_ids();
?><?php
$args = array('orderby' => 'slug', 'parent' => 0);
$categories = get_categories($args);
foreach ($categories as $category)
{
echo '<a href="' . get_category_link(
$category->term_id
) . '" rel="bookmark" class="category">' . $category->name . '' . $category->description . '</a>';
} ?>
</span>
</div>
<h2 class="post-title">
<?php the_title(); ?>
</h2>
Read More
</div>
</div>
<?php endforeach;
wp_reset_postdata(); ?>
</div>
<?php endwhile; ?>
<?php next_posts_link('Older posts'); ?>
<?php previous_posts_link('Newer posts'); ?>
<?php else: ?>
<?php endif; ?>
You maybe didn't set any posts_per_page value and the default is 5. If you want to modifiy it for the whole website blog/archive pages, modify the posts_per_page value in the Reading Settings page.
In a $args array, modify as folow:
$args = array(
'posts_per_page'=> -1, // unlimited
'category' => ''
);
$posts = get_posts($args);
I'm prudent with my answer (that why I give 2 methods to achieve this), because I don't know with your given code, if the main loop $args is modify.
I hope this could nevertheless help you.

Loop CPT by ID through Wordpress loop

I'm trying to loop only custom post types using the WP loop, but only shows the ones I give by ID.
This is my "normal" loop right now:
<?php $args = array(
'post_type' => 'referenties', 'posts_per_page' => 5, 'order' => 'DESC',
); ?>
<?php
$number = 0;
query_posts($args);
if(have_posts()):
?>
<!-- /Carousel script -->
<div class="container">
<div class="carousel-loop">
<div id="myCarousel" class="carousel slide">
<ol class="carousel-indicators">
<?php while(have_posts()): the_post(); ?>
<li data-target="#myCarousel" data-slide-to="<?php echo $number++; ?>"></li>
<?php endwhile; ?>
</ol>
<div class="controle-buttons">
<a class="carousel-control left" href="#myCarousel" data-slide="prev"><i class="fa fa-chevron-circle-left"></i></a>
<a class="carousel-control right" href="#myCarousel" data-slide="next"><i class="fa fa-chevron-circle-right"></i></a>
</div>
<!-- Carousel items -->
<div class="carousel-inner">
<?php while(have_posts()): the_post(); ?>
<!-- Carousel nav -->
<div class="item">
<div class="col-sm-2">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php the_post_thumbnail(array(150,150)); // Declare pixel size you need inside the array ?>
</div>
<div class="col-sm-4">
<h4><?php the_title(); ?></h4>
<?php $bedrijf = get_field('naam_bedrijf'); ?>
<?php $feest = get_field('feest'); ?>
<?php $link = get_field('mylink'); ?>
<?php echo '<p>Bedrijfsnaam: ' . $bedrijf . '</p>'; ?>
<?php $post_object = get_field('mylink');
if( $post_object ): $post = $post_object; setup_postdata( $post ); ?>
<p>Feest type: <a style="color:#ff6600" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php endif; ?>
</div><?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<div class="col-sm-4 col-sm-offset-1">
<h4>Opmerking</h4>
<p><?php echo custom_field_excerpt_longer(); ?></p>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php echo '<p>' . wp_review_show_total() . '</p>'; ?>
</div>
<?php endif; ?>
</div>
<?php endwhile; ?>
But I only want to show post ID's: 2706, 2462, 2514, 2511 and 2505.
The loop is shown in a carousel, this works fine. But I just want the ID's to show and not all posts added.
Use something like this:
<?php
$args = array(
'post_type' => 'referenties',
'post__in' => array(2706, 2462, 2514, 2511, 2505),
'order' => 'DESC',
);
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//post content output goes here
}
// Restore original Post Data
wp_reset_postdata();
} else {
// no posts found
}
The post__in () argument uses array with desired post ids to retrieve.
Don't use query_posts for custom queries. Too much that can go wrong.
Hope this helps :)

Categories