Copy paste PHP code for Fast Display Categories Post Grid Shortcode

Display Custom Post Grid by Category ID Shortcode in WordPress with No Plugin

If you're looking for a sleek, fast, and productivity-enhanced way to showcase posts in a custom grid layout drawd from category IDs, shortcodes are your best friend in . They provide a flexible and user-friendly solution to display content kinetically without requiring complex coding knowledge.

In this report, we'll peer into the concept of displaying a custom post grid by category ID using shortcode, highlighting its benefits, and why using our advanced shortcode solution can strikingly improve your website's performance and .


What is a Custom Post Grid?

A custom post grid is a structured layout designed to showcase your WordPress posts. Unlike the default list view, a grid layout:

  • Makes your content visually appealing.
  • Allows better content .
  • Find a Better Solution fors user navigation on your site.

With a custom grid, you can fit how your posts are displayed, whether by specific categories, tags, or other parameters.


Why Use a Shortcode for Custom Post Grids?

Shortcodes are snippets of text you place in posts, pages, or widgets to add kinetic functionality. A few examples we like are-, you can use recent_posts_in_cat cat=”YOUR CATEGORY ID NUMBER” posts=”20″   enclosed with the proper shortcode brackets [ ] to display posts from a specific category in a grid layout. Just post the code at the bottom in your functions.php and it works out of the box! impacts of shortcodes include:

  • Nice Card Design with Feature Image and Exceprt
  • Ease of Use: Add functionality without theme files.
  • Reusability: Use the same shortcode across multiple pages.
  • Flexibility: Customize parameters like the number of posts, layout, or categories.

Category ID in WordPress

Category IDs are one-off identifiers assigned to each category in WordPress. a category ID in your shortcode lets you:

  1. Filter posts displayed in the grid.
  2. Target specific content for improved relevance.
  3. Avoid displaying unrelated or unnecessary posts.

Why Our Shortcode Solution Stands Out

Our shortcode solution is improved for speed and reliability. Here's what makes it one-off:

1. Super-Fast Performance

We've designed our shortcode to get results at lightning speed. By using WP-CLI transients, the server minimizes delays when fetching related posts. This ensures that even high-traffic websites can rely on fast-loading grids.

2. A time-saving hack with Server Management

WP-CLI transients store temporary data in the database to reduce repetitive queries. This reduces server load and eliminates delays, especially when handling large databases or frequent queries.

3. User-Friendly Implementation

With minimal setup, you can create kinetic grids using simple shortcode parameters like:

5 Simple Steps to Find a WordPress Category ID Number ( the URL)

Finding the category ID number in WordPress is straightforward if you know where to look. This one-off identifier is often needed when configuring plugins, customizing themes, or managing categories.


1. Create Positive to the Categories Section

To locate the category ID, you first need to go to the WordPress admin dashboard:

  1. Log in to your WordPress admin area.
  2. Go to Posts on the left-hand menu.
  3. Select Categories from the dropdown.

2. Identify the Desired Category

In the Categories page, you'll see a list of all the categories you've created. Locate the category for which you want to find the ID.


3. Hover Over the Category Name

When you hover over a category name, you'll see a small menu below it with options like Edit, Quick Edit, Delete, and View. Click on Edit to open the category editing screen.


4. Check the URL in the Browser Address Bar

Once you're on the category editing page, look at the URL in your browser's address bar. The URL will look something like this:

 

Look Here:

  • The tag_ID=15 part indicates the category ID.
  • In this example, the category ID number is 15.

Just add this at the bottom to your functions.php and use the following shortcode anywhere in your site:

recent_posts_in_cat cat=”YOUR CATEGORY ID NUMBER” posts=”20″   enclosed with the proper shortcode brackets [ ]

Code to add to functions.php:

/**
* Increase the Smoothness ofd shortcode with built-in transient .
* Usage:

No recent posts found.


*/
function optimized_recent_posts_shortcode($atts) {

// Define shortcode attributes and set defaults
$atts = shortcode_atts( array(
‘cat' => ”, // Category ID
‘posts' => '20', // Number of posts
), $atts, ‘recent_posts_in_cat' );

// Cache key for transient
$cache_key = ‘recent_posts_cat_' . $atts[‘cat'] . ‘_' . $atts[‘posts'];
$cache_duration = 12 * HOUR_IN_SECONDS; // Cache duration

// Check if transient exists and is valid
$cached_output = get_transient($cache_key);
if ($cached_output) {
return $cached_output; // Return cached output if exists
}

// Prepare query arguments
$args = array(
‘cat' => intval( $atts[‘cat'] ),
‘posts_per_page' => intval( $atts[‘posts'] ),
‘fields' => ‘ids', // Only fetch IDs for lightweight query
);

// Query posts
$post_ids = get_posts($args);

if (empty($post_ids)) {
return ‘

No recent posts found.

';
}

// Generate HTML for output
ob_start();
echo ‘

';
echo ‘
';

foreach ($post_ids as $post_id) {
$title = get_the_title($post_id);
$permalink = get_permalink($post_id);
$thumbnail = get_the_post_thumbnail($post_id, ‘medium', array(‘style' => ‘max-width: 100%; height: auto; border-radius: 5px;', ‘loading' => ‘lazy'));
$excerpt = wp_trim_words(get_post_field(‘post_excerpt', $post_id), 20);
$date = get_the_date(”, $post_id);

?>


}

echo ‘

'; // End grid
echo ‘
'; // End inner-wrap

$output = ob_get_clean(); // Get buffered output

// Store the output in a transient
set_transient($cache_key, $output, $cache_duration);

return $output;
}

// Register the shortcode
add_shortcode(‘recent_posts_in_cat', ‘ improved_recent_posts_shortcode');

 

//End Code

 

 


5. Note Down the Category ID

Write down the ID number for subsequent time ahead use. This is the one-off identifier for that particular category, which you can use for setting filters, assigning specific functions, or other configurations.


Why Knowing the Category ID is Useful

Deciding firmly upon the category ID can help you:

  • Apply plugins or widgets to specific categories.
  • Exclude or include categories in menus or posts.
  • Fine-tune themes for improved site functionality.

Table: Common Uses of Category IDs

Use CaseExample
Exclude a category from the homepageExclude category ID 15 using a plugin.
Customize widgets for categoriesDisplay widgets only for category ID 8.
Filter posts by category IDShow posts from category ID 12.

Pro Tip

For a quicker way to find the category ID without editing, simply hover over the category name in the list. The category ID will also appear in the URL preview at the bottom-left corner of your browser.


FAQs

1. What is a category ID in WordPress?

A category ID is a one-off number assigned to each category in WordPress. It helps in identifying and managing categories for various tasks.

2. Can I find the category ID without opening the category edit page?

Yes, you can hover over the category name on the Categories page, and the URL preview in your browser will show the category ID.

3. Is there a plugin to find category IDs in WordPress?

Yes, several plugins like Reveal IDs can display IDs for posts, categories, and tags directly in the admin panel.

4. Are category IDs necessary for all plugins?

Not all plugins require category IDs, but they are essential for those that rely on specific category filtering or customization.

5. Can I change a category ID?

No, category IDs are system-generated and cannot be changed manually.

video web designing