Categories
Web Development

Loop and Print Page Children Content in WordPress

This is ideal for when you need to loop through pages to create a list or grid of page content. ¬†Wordpress makes this pretty simple for posts, but it’s not so easy for pages.

In this case I am trying to generate a list of all children of the “Ambassadors” page and create clickable links.

Set up the WP Query

<?php
    // Set up the objects needed
    $my_wp_query = new WP_Query();
    $all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'orderby' => 'menu_order title'));

    // Get the page as an Object
    $ambassadorPage =  get_page_by_title('Ambassadors');

    // Filter through all pages and find Portfolio's children
    $ambassadorPageChildren = get_page_children( $ambassadorPage->ID, $all_wp_pages );
    $childIds = array();
    // Get the IDs so you can loop through a page as normal
    foreach ($ambassadorPageChildren as $childData) {
        $childIds[] = $childData->ID;

    }
    ?>

Loop through the content

<?php // By using a foreach you can use normal printout fields like the_title() etc.
        foreach ($childIds as $childId) { ?>
            <?php $pageloop = new WP_Query("page_id=" . (int) $childId); ?>
                <?php while ( $pageloop->have_posts() ) : $pageloop->the_post(); ?>
                    <div class="small-12 medium-6 large-3 columns">
                        <article id="post-<?php the_ID(); ?>">
                            <a href="<?= get_the_permalink(); ?>"><img src="<?php echo the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>"></a>
                            <header>
                                <p>Ambassador</p>
                                <h2 class="entry-title"><?php the_title(); ?></h2>
                                <h3><?php echo the_field('ambassador_specialism'); ?></h3>
                            </header>
                            <div>
                                <footer>
                                    <p><a href="<?= get_the_permalink(); ?>">Read More</a></p>
                                </footer>
                        </article>
                    </div>
                <?php endwhile;?>
            <?php wp_reset_postdata(); ?>
        <?php } ?>

 

Categories
Web Development

Releasing Content over Time / Drip Campaign with WordPress

Using the Date Registered field in WordPress you can create a page that releases content over time. In this example we are creating a Drip Campaign for content released in a specific category.

In this example I have set up a category called “Campaigns” and within it I have added subcategories called Week 1, Week 2, Week 3 etc. The goal is that every week a new section will be released, so if the user registered, week 1 is available instantly, then 1 week later week 2 becomes available and so on.

$dateRegistered = strtotime($current_user->user_registered);
$now = date('Y-m-d H:i:s', strtotime('now'));
$categoryId = 99;

$catArgs = array('taxonomy' => 'category', 'parent' => $categoryId, 'hide_empty' => false);
$categories = get_terms($catArgs);
$dripIncrement = 7;
$startNumber = 0;
$counter = 0;
$number = 0;
$weeks = array();

foreach ($categories as $category) {

    if ($counter != 0) {
        $number = $number + $dripIncrement;
    }
    $weeks[$category->term_id] = date('Y-m-d H:i:s', strtotime('+' . $number . ' day', $dateRegistered));
    $counter++;
}

$listStyle = '';
foreach ($weeks as $weekId => $releaseDate) {

    // If is enabled
    if ($now >= $releaseDate) {
        $listStyle .= '
            li.cat-item.cat-item-'. $weekId .',
            li.cat-item.cat-item-'. $weekId .' a
             {
                color: green;
            }
        ';
    } else {
        $listStyle .= '
            li.cat-item.cat-item-'. $weekId .',
            li.cat-item.cat-item-'. $weekId .' a {
                color: red;
                pointer-events: none;
                cursor: default;
            }
        ';
    }
}

The above create a loop for for the subcategories in my Category Id, and using an increment of 7 days it will add one type of css if the registered date is within range and another if it is not. We then take the following and add it before the tag.

<style type="text/css">
    <?= $listStyle ?>
</style>

Finally, we use the WordPress list categories function to print a list of all the subcategories, the $args variable allows to tweak things like printing a title, printing if the subcategory has no content, etc.

<?php
    // List subcategories of Campaign
    $args = array(
        'child_of'            => $categoryId, // 10 Week Plan ID
        'title_li'            => false, // Hide Title
        'hide_empty'          => false
    );
?>

<?php wp_list_categories($args) ?>