Skip to content

Instantly share code, notes, and snippets.

@webmandesign
Last active April 12, 2025 11:48
Show Gist options
  • Save webmandesign/da2936930a473c438651a73ea4796b34 to your computer and use it in GitHub Desktop.
Save webmandesign/da2936930a473c438651a73ea4796b34 to your computer and use it in GitHub Desktop.
WebMan Shortcode: Posts
<?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;
} );
@webmandesign
Copy link
Author

webmandesign commented Apr 12, 2025

"WebMan Shortcode: Posts" WordPress plugin

How to install

  1. Download the webman-shortcode-posts.php plugin file.
  2. Upload the file to your server to YOUR_WORDPRESS_FOLDER/wp-content/plugins/ folder.
  3. Activate "WebMan Shortcode: Posts" plugin in Plugins menu in your WordPress dashboard.

How to use

  1. In your page/post content add a Group block and set it to wide alignment.
  2. Add Shortcode block into the Group block.
  3. Type the [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:

<!-- wp:group {"align":"wide","layout":{"type":"constrained"}} -->
<div class="wp-block-group alignwide">
	<!-- wp:heading -->
	<h2 class="wp-block-heading">Events</h2>
	<!-- /wp:heading -->
	<!-- wp:shortcode -->
	[posts type="ctc_event" columns="3" count="3" /]
	<!-- /wp:shortcode -->
</div>
<!-- /wp:group -->

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment