• Resolved ArielZusya

    (@arielzusya)


    I’d like to count the number of children-pages of four different parent-pages, add those numbers together, and output the result in a post. The idea is that a static post could update to reflect the number (as referenced above) without being edited. Can this be done? If so, how? Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You could use get_children for each of the parent posts, something like:

    $parent_id = 7;
    $child_pages_args = array(
        'post_parent' => $parent_id,
        'post_type'   => 'page',
        'numberposts' => -1,
        'post_status' => 'published'
    );
    $child_pages = get_children( $child_pages_args );
    $child_pages_count = count($child_pages);

    You may also want to use a Transient so you’re not querying the database on each page load.

    Thread Starter ArielZusya

    (@arielzusya)

    Thanks so much for your response. I think that gets me started. It does the math for me, which is fantastic!

    This next question may be even more mundane than the last, so please bare with me. Ultimately, I’d like to embed the result in the middle of a static post. In other words, in the middle of the post there will be something like, “we now have X subtopics from which to learn all about the main topic in greater detail.” I assume I’ll need to put the counting code in a function and then call the function somehow… or is this where custom fields come in to play? I’ve never used them before. Or perhaps there is some other way of which I am unaware to accomplish this goal. Thoughts? Thanks so much!

    Thread Starter ArielZusya

    (@arielzusya)

    I think I’ve got a solution. I used a plugin called shortcode exec php. Serves my purpose perfectly. Thanks so much for your help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘simple math in loop’ is closed to new replies.