How to separate posts from WP_Query into 2 columns - php

I need to separate posts from WP_Query into 2 columns: first post will go to right column, 3 other posts will go to left column so the structure will look like this - https://prnt.sc/vc044d
Please help me to improve my code.
Thanks a lot.
function mk_latestposts() {
// the query
$the_query = new WP_Query( array( 'posts_per_page' => 4 ) );
// The Loop
if ( $the_query->have_posts() ) {
$postcount = 0;
$string .= '<ul class="mk-recent-posts-list">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$postcount++;
// if this is the first post
if ( $postcount == 1 ) {
$string .= '<li class="first-post">';
$string .= '' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() . get_the_date() .'</li>';
} else {
// if this is the next post
$string .= '<li class="next-post">';
$string .= '' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() . get_the_date() .'</li>';
}
}
} else {
// no posts found
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('mklatestposts', 'mk_latestposts');

Your code is fine You can do this with CSS. Just add a common class to every <li> Like <li class='single-post'> and then apply this CSS. You can change it from List structure to Div structure whatever you want.
li.single-post{ width: 47%; float: left;} ul.mk-recent-posts-list { width: 100%; }

Related

How to show categories and date on posts

I want to categorise posts and show them on my WordPress website. So in order to achieve that I've written a code. My code is below.
function wpb_postsbycategory() {
// the query
$the_query = new WP_Query( array(
'category_name' => 'travel',
'posts_per_page' => 5
) );
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( has_post_thumbnail() ) {
$string .= '<li>';
$string .= '' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</li>';
} else {
// if no featured image is found
$string .= '<li>' . get_the_title() .'</li>';
}
}
} else {
// no posts found
$string .= '<li>No Posts Found</li>';
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');
This code works perfectly fine. However, I want to show posts meta descriptions as well (i.e post created date and the category). How would I achieve that? Right now It only extracts the post heading and the image. Thanks so much for helping me out.
Try this:
function wpb_postsbycategory() {
// the query
$the_query = new WP_Query( array(
'category_name' => 'travel',
'posts_per_page' => 5
) );
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$string .= '<li>';
$string .= '<a href="' . get_the_permalink() .'" rel="bookmark">';
if ( has_post_thumbnail() ) {
$string .= get_the_post_thumbnail($post_id, array( 50, 50) );
}
// title
$string .= get_the_title();
// date
$string .= get_the_date();
// categories
$categories = get_the_category();
$category_list = join( ', ', wp_list_pluck( $categories, 'name' ) );
$string .= wp_kses_post( $category_list );
// close link
$string .= '</a></li>';
}
// no posts found
$string .= '<li>No Posts Found</li>';
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');
I guess you need/want to add HTML tags around date and category.
For example:
$string .= '<span class="date">'.get_the_date().'</span>';
But that's up to you.
You can change the output of the categories based on the docs here.

How to trim WP category title using functions.php

I am fairly new to WP functions so hoping someone could shed some light on this issue. I have a function that displays five posts from a specific category, then using a shortcode to display the results on certain WP posts/pages. I am having trouble to include wp_trim_words to get_the_title(). I want to limit the title to 5 words and end with "...". I saw many examples in here but none that would fit into my function. Can anyone help please?
function wpb_postsbycategory() {
// the query
$the_query = new WP_Query( array( 'category_name' => '3dgames', 'posts_per_page' => 5 ) );
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( has_post_thumbnail() ) {
$string .= '<p>' . get_the_post_thumbnail($post_id, array( 100, 50) ) . get_the_title() .'<br></p>';
} else {
// if no featured image is found
$string .= '<li>' . get_the_title() .'</li>';
}
}
} else {
// no posts found
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('3dgames', 'wpb_postsbycategory');
// Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');
You should use wp_trim_words() and inside place the title for the post:
wp_trim_words(get_the_title(), 5, '...')
In your case it should be something like this:
if ( has_post_thumbnail() ) {
$string .= '<p>' . get_the_post_thumbnail($post_id, array( 100, 50) ) . (wp_trim_words(get_the_title(), 5, '...')) .'<br></p>';
} else {
// if no featured image is found
$string .= '<li>' . (wp_trim_words(get_the_title(), 5, '...')) .'</li>';
}
For more details on how wp_trim_words() works you could visit the official documentation: https://developer.wordpress.org/reference/functions/wp_trim_words/

How to display posts from specific category in wordpress

Hey guys I have a code which rendered 5 recent posts from a specific category. Here is that
function postsbycategory() {
// the query
$the_query = new WP_Query( array( 'category_name' => 'news', 'posts_per_page' => 5 ) );
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( has_post_thumbnail() ) {
$string .= '<li>';
$string .= '' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</li>';
} else {
// if no featured image is found
$string .= '<li>' . get_the_title() .'</li>';
}
}
} else {
// no posts found
}
$string .= '</ul>';
return $string;
What i wated is that above code display recent posts from specific category from 1 to 5 but i want to display recent posts from 2 to 6. For better understadings focus on this e.g.
Suppose i have a category named Pizza and in that category I have 10 posts named
Post 1
Post 2
Post 3
Post 4
Post 5
Post 6
Post 7
Post 8
Post 9
Post 10
So if I apply above code to my homepage it will display post from category Pizza are
Post 1
Post 2
Post 3
Post 4
Post 5
but i want this
Post 2
Post 3
Post 4
Post 5
Post 6
Yes i want that code to start showing posts from 2 not from 1. So how to do this.
Please help me I am not a web developer.
Just add a counter ($i) in the loop and pull the first 6 posts, skipping the first one using the counter to check.
function postsbycategory() {
// the query
$the_query = new WP_Query( array( 'category_name' => 'news', 'posts_per_page' => 6 ) );
$i=1;
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($i>1){
if ( has_post_thumbnail() ) {
$string .= '<li>';
$string .= '' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</li>';
} else {
// if no featured image is found
$string .= '<li>' . get_the_title() .'</li>';
}
}
++$i;
}
} else {
// no posts found
}
$string .= '</ul>';
return $string;
}

Wordpress - Include static image after every X post through wp_query

I have a somewhat complicated design.
I want to use a CPT to output it. This is my CPT in the wp_query:
/**
* Management Team Shortcode
**/
function team_query() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'management-team',
'order' => 'DESC',
);
$posts = get_posts( $args );
if ( !empty( $posts ) ) {
$flag = 0;
foreach ($posts as $counter => $p) {
$counter++;
if ( $flag <= 2 ) {
$flag++;
}
$role = get_field( "role" );
$name = get_field( "team_member_name" );
$bio = get_field( "bio" );
$profile = get_the_post_thumbnail_url( $p->ID, 'full' );
$flip = get_field( "flip_content" );
$html_out = '<article class="team-member">';
// Do stuff with each post here
if ( $flag % 2 == 0 ) {
//add image after second post like
$html_out .= '<img src="http://www.ankitdesigns.com/demo/rawafid/wp-content/themes/rawafid-systems/assets/img/mt-1.jpg" alt="Safety Whistle" />';
}
if ( $counter % 6 == 0 ) {
$flag = 0;
//add image after sixth post like
$html_out .= '<img src="http://www.ankitdesigns.com/demo/rawafid/wp-content/themes/rawafid-systems/assets/img/mt-2.jpg" alt="Safety Whistle" />';
}
$html_out .= '<div class="meta-team"><h6>' . $role . '</h6>' . '<h4>' . $name . '</h4>' . '<p>' . $bio . '</p></div>';
$html_out .= '</article>';
}
} else {
// No results
$html_out = 'No Management Team Members Found.';
}
return $html_out;
}
add_shortcode( 'show_management_team', 'team_query' );
After 2 posts I'd like to add a static img and after 4 posts another static and after 2 more posts another static img, etc.
I'm open to suggestions on a better approach.
I'm trying to think of an alternative method. Maybe using Visual composer to build a grid?
Hey Darren here is the logic, I think that you can modify my functions and implement it in your code.
/**
* Management Team Shortcode
**/
function team_query() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'management-team',
'order' => 'ASC',
);
$posts = get_posts( $args );
if ( !empty( $posts ) ) {
$flag = 0;
foreach ($posts as $counter => $p) {
$counter++;
if ( $flag <= 2 ) {
$flag++;
}
$title = get_the_title($p->ID);
$link = get_the_permalink($p->ID);
$featured_img = get_the_post_thumbnail_url( $p->ID, 'full' );
$html_out = '<article class="recent-project" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">';
// Do stuff with each post here
if ( $flag % 2 == 0 ) {
//add image after second post like
// $html .= <img src="css/images/myimage1.png" alt="" />
}
if ( $counter % 6 == 0 ) {
$flag = 0;
//add image after sixth post like
// $html .= <img src="css/images/myimage2.png" alt="" />
}
$html_out .= '<div class="container"><div class="meta-project"><h6>Latest Project</h6>' . '<h2>' . $title . '</h2>' . '<a class="btn btn-lg btn-tertiary" href="' . $link . '">' . 'Discover' . '</a></div></div>';
$html_out .= '</article>';
}
} else {
// No results
echo "Nothing to show";
}
return $html_out;
}
Cheers :)

Wordpress: disable links for get_the_category_list function?

Hey my theme uses this function to display the categories a post has, but it also creates links which I would like to get rid of. I prefer php rather than a javascript solution.
Here is the code:
<p class="postmeta">
<?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?>
<?php
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( __( ', ', 'gridster' ) );
if ( $categories_list && gridster_categorized_blog() ) :
?>
<?php /*printf( __( '%1$s', 'gridster' ), $categories_list );*/ echo $categories_list; ?>
<?php endif; // End if categories ?>
<?php endif; // End if 'post' == get_post_type() ?>
</p>
Link to the codex reference
How can I void those links?
or get rid of links all together from DOM (but this might create more work as the actual text is between the <a> tags
HTML
<p class="postmeta">
<a target="_blank" href="http://whatever.com/category/default/" title="View all posts in Default" rel="category tag">Default</a>
</p>
Thanks!!
If you just want a text-string returned and use the native get_the_categories() function, you could simply wrap it in PHP's strip_tags() function to remove all HTML that is returned:
echo strip_tags( get_the_category_list(__( ', ', 'gridster' ));
You can try modifying this to fit your needs:
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
(From http://premium.wpmudev.org/blog/how-to-get-a-wordpress-category-name-without-the-link/ )
One option is to copy the original function and adapt to your needs. Modified to remove the <a> tags but not tested, compare to the original, adapt if needed, and add it to the theme's functions.php:
function category_list_so_22771587( $separator = '' ) {
global $wp_rewrite;
$categories = get_the_category( $post_id );
$thelist = '';
$i = 0;
foreach ( $categories as $category ) {
if ( 0 < $i )
$thelist .= $separator;
switch ( strtolower( $parents ) ) {
case 'multiple':
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, true, $separator );
$thelist .= $category->name;
break;
case 'single':
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, false, $separator );
$thelist .= $category->name;
break;
case '':
default:
$thelist .= $category->name;
}
++$i;
}
return $thelist;
}
And modify your template to:
$categories_list = category_list_so_22771587( __( ', ', 'gridster' ) );
You can retrieve categories, extract the 'name' column of each items, and implode each values with a comma:
<?= implode(', ', (array_column(get_the_category(), 'name')))?>
Or you can retrieve categories list with link, and remove a tags :
<?= strip_tags(get_the_category_list(', ')) ; ?>
Do you just want to hide the links? If so use css:
.postmeta a {display: none}
Edit, you can also "disable" those links in css:
.postmeta a {
pointer-events: none;
cursor: default;
}

Categories