voglio il conteggio dei post figli

Home » Snippets » voglio il conteggio dei post figli
0

Created with:

Visibility: 

public

Creator: info@codeleven.it

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: My WordPress Plugin
 * Description: Count the number of child posts
 * Version: 1.0
 * Author: Your Name
 * Author URI: https://www.example.com
 * Text Domain: wpturbo
 */

// Hook into the 'init' action
add_action('init', 'wpturbo_count_child_posts');

function wpturbo_count_child_posts() {
    // Get the current post ID
    $parent_post_id = get_the_ID();

    // Get the number of child posts for the current post
    $child_post_count = count(get_children(array('post_parent' => $parent_post_id)));

    // Output the number of child posts
    echo 'Number of child posts: ' . $child_post_count;
}
				

Explanation:

In this code, we define a function wpturbo_count_child_posts() and hook it into the init action using add_action(). Inside the function, we first get the ID of the current post using get_the_ID(). Then, we use the get_children() function to get an array of all the child posts of the current post, passing the post_parent parameter with the value of the parent post ID. We then use the count() function to count the number of child posts. Finally, we echo the number of child posts.

Please note that this code assumes that you are calling the wpturbo_count_child_posts() function within the WordPress loop. If you are calling it outside the loop, you will need to modify the code accordingly to get the correct parent post ID.

Register an account to save your snippets or go Pro to get more features.