Last active
April 12, 2025 11:48
-
-
Save webmandesign/da2936930a473c438651a73ea4796b34 to your computer and use it in GitHub Desktop.
WebMan Shortcode: Posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined( 'WPINC' ) ) exit; | |
/** | |
* Plugin Name: WebMan Shortcode: Posts | |
* Description: Usage: [posts type="post_type" columns="3" count="3" /] | |
* Version: 1.0.0 | |
* Author: WebMan Design, Oliver Juhas | |
* Author URI: https://www.webmandesign.eu/ | |
* License: GPL-3.0-or-later | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt | |
*/ | |
add_shortcode( 'posts', function( $atts ) { | |
$atts = shortcode_atts( | |
[ | |
'type' => 'post', | |
'count' => 3, | |
'columns' => 3, | |
'class' => 'alignwide', | |
], | |
$atts, | |
'posts' | |
); | |
$post_type = sanitize_title( $atts['type'] ); | |
// Remove possible query modifications applied by the theme. | |
remove_all_actions( 'pre_get_posts' ); | |
$query_args = [ | |
'post_type' => $post_type, | |
'posts_per_page' => absint( $atts['count'] ), | |
'no_found_rows' => true, | |
'ignore_sticky_posts' => true, | |
'post_status' => 'publish', | |
]; | |
$query = new WP_Query( $query_args ); | |
if ( ! $query->have_posts() ) { | |
return '<p>No posts found.</p>'; | |
} | |
$class = array_filter( array_merge( | |
[ | |
'shortcode-posts', | |
'posts', | |
'posts-list', | |
'columns-' . absint( $atts['columns'] ) | |
], | |
explode( ' ', $atts['class'] ) | |
) ); | |
$template_path = 'templates/parts/content/content'; | |
ob_start(); | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
get_template_part( $template_path, $post_type ); | |
} | |
$entries = ob_get_clean(); | |
// Lower the heading structure. | |
$entries = str_replace( | |
[ | |
'<h3', '/h3', | |
'<h2', '/h2', | |
], | |
[ | |
'<h4', '/h4', | |
'<h3', '/h3', | |
], | |
$entries | |
); | |
return '<div class="' . esc_attr( implode( ' ', $class ) ) . '">' . $entries . '</div>' | |
. '<style>@media(min-width: 880px){.shortcode-posts .entry{width:calc(50% - var(--posts_list_gap));max-width:calc(50% - var(--posts_list_gap))}}@media(min-width: 1280px){.shortcode-posts.columns-3 .entry{width:calc(33.33% - var(--posts_list_gap));max-width:calc(33.33% - var(--posts_list_gap))}}@media(min-width: 1280px){.shortcode-posts.columns-4 .entry{width:calc(25% - var(--posts_list_gap));max-width:calc(25% - var(--posts_list_gap))}}</style>'; | |
} ); | |
add_filter( 'all_plugins', function( $all_plugins ) { | |
foreach ( $all_plugins as $file => $data ) { | |
if ( basename( __FILE__ ) == $file ) { | |
$all_plugins[ $file ]['Description'] .= | |
PHP_EOL . 'Available post types: ' | |
. '"' . implode( '", "', (array) get_post_types( [ 'public' => true ] ) ) . '"'; | |
break; | |
} | |
} | |
return $all_plugins; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"WebMan Shortcode: Posts" WordPress plugin
How to install
webman-shortcode-posts.php
plugin file.YOUR_WORDPRESS_FOLDER/wp-content/plugins/
folder.How to use
[posts type="post" columns="3" count="3" /]
shortcode into the Shortcode block.Here is an example you can copy and paste into your page/post content:
Shortcode attributes
type
- Set this to post type you want to display. Default =post
. (For the list of all available public post types on your website check the "WebMan Shortcode: Posts" plugin description in the list of plugins in your WordPress dashboard.)columns
- Number of columns of the posts list. Min =2
, Max =4
, Default =3
.count
- Number of posts to display. Default =3
.class
- Optional CSS class set on the posts list container. Default =alignwide
.