WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] simple math in loop (4 posts)

  1. ArielZusya
    Member
    Posted 3 years ago #

    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!

  2. jkovis
    Member
    Posted 3 years ago #

    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.

  3. ArielZusya
    Member
    Posted 3 years ago #

    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!

  4. ArielZusya
    Member
    Posted 3 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.