Friday, March 15, 2024
HomeMobile MarketingWordPress: How To Checklist Baby Pages Utilizing A Shortcode

WordPress: How To Checklist Baby Pages Utilizing A Shortcode


We’ve rebuilt the hierarchy of web sites for a number of of our WordPress shoppers, and one of many issues we try to do is set up the data effectively. To do that, we regularly need to create a grasp web page and embrace a menu that routinely lists the pages beneath it. A listing of kid pages, or subpages.

Sadly, there’s no inherent perform or function to do that inside WordPress, so we developed a shortcode so as to add to the shopper’s web site. Right here’s how you should use the shortcode with all its variables populated inside a WordPress put up or web page:

[listchildpages ifempty="No child pages found" order="ASC" orderby="title" ulclass="custom-ul-class" liclass="custom-li-class" aclass="custom-a-class" displayimage="yes" align="aligncenter"]

Breakdown of Utilization:

  • ifempty="No baby pages discovered": This article will be displayed if there are not any baby pages obtainable.
  • order="ASC": This types the listing of kid pages in ascending order.
  • orderby="title": This orders the kid pages by their title.
  • ulclass="custom-ul-class": Applies the CSS class “custom-ul-class” to the <ul> component of the listing.
  • liclass="custom-li-class": Applies the CSS class “custom-li-class” to every <li> component within the listing.
  • aclass="custom-a-class": Applies the CSS class “custom-a-class” to every <a> (hyperlink) component within the listing.
  • displayimage="sure": This consists of the featured picture of every baby web page within the listing.
  • align="aligncenter": This aligns the featured photographs within the middle.

Insert this shortcode immediately into the content material space of a WordPress put up or web page the place you need the listing of kid pages to seem. Keep in mind to customise the values of every attribute to suit the design and construction of your WordPress web site.

Moreover, in case you’d like a brief excerpt describing every web page, the plugin allows excerpts on pages as a way to edit that content material on the web page’s settings.

Checklist Baby Pages Shortcode

perform add_shortcode_listchildpages($atts, $content material = "") { 
    international $put up; 
    $string = '';

    $atts = shortcode_atts(array(
        'ifempty' => '<p>No Information</p>',
        'order' => 'DESC',
        'orderby' => 'publish_date',
        'ulclass' => '',
        'liclass' => '',
        'aclass' => '',
        'displayimage' => 'no',
        'align' => 'alignleft'
    ), $atts, 'listchildpages');

    $args = array(
        'post_type' => 'web page',
        'posts_per_page' => -1,
        'post_parent' => $post->ID,
        'orderby' => $atts['orderby'],
        'order' => $atts['order']
    );

    $father or mother = new WP_Query($args);

    if ($parent->have_posts()) {
        $string .= $content material.'<ul class="'.$atts['ulclass'].'">';
        whereas ($parent->have_posts()) : $parent->the_post();
            $string .= '<li class="'.$atts['liclass'].'">';
            $true = array("y", "sure", "t", "true");
            $showimage = strtolower($atts['displayimage']);
            if (in_array($showimage, $true)) {
                if (has_post_thumbnail($post->ID)) {
                    $image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail'); 
                    $string .= '<a category="'.$atts['aclass'].'" href="'.get_permalink().'" title="'.get_the_title().'">';
                    $string .= '<img src="'.$image_attributes[0].'" width="'.$image_attributes[1].'" peak="'.$image_attributes[2].'" alt="'.get_the_title().'" class="'.$atts['align'].'" /></a>';
                }
            }
            $string .= '<a category="'.$atts['aclass'].'" href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a>';
            if (has_excerpt($post->ID)) {
                $string .= ' - '.get_the_excerpt();
            }
            $string .= '</li>';
        endwhile;
        $string .= '</ul>';
    } else {
        $string = $atts['ifempty'];
    }

    wp_reset_postdata();

    return $string;
}
add_shortcode('listchildpages', 'add_shortcode_listchildpages');

The perform add_shortcode_listchildpages provides a {custom} shortcode

No Information

, which you should use inside WordPress posts or pages to show a listing of kid pages. Right here’s a breakdown of how the code works:

  1. World Put up Variable: The perform begins by declaring the worldwide variable $put up, which is used to entry details about the present put up or web page inside WordPress.
  2. Shortcode Attributes: The shortcode_atts perform units up default values for the shortcode attributes. Customers can override these after they insert the shortcode. Attributes embrace:
    • ifempty: Message to show if there are not any baby pages.
    • order: Order of the kid pages (ASC or DESC).
    • orderby: Standards for ordering baby pages (e.g., publish_date).
    • ulclass: CSS class for the <ul> component.
    • liclass: CSS class for the <li> components.
    • aclass: CSS class for the <a> (anchor) components.
    • displayimage: Whether or not to show the featured picture of the kid pages.
    • align: Alignment of the featured picture.
  3. Question Arguments: The perform units up a WP_Query to retrieve all baby pages of the present web page, sorted in accordance with the required attributes.
  4. Producing the Checklist:
    • If baby pages are discovered, the perform constructs an HTML unordered listing (<ul>), with every baby web page represented by a listing merchandise (<li>).
    • Inside every listing merchandise, the perform checks whether or not to show the featured picture primarily based on the displayimage attribute.
    • The perform additionally creates a hyperlink to every baby web page utilizing the <a> tag, and if obtainable, provides the excerpt of the kid web page.
  5. Output or Default Message: If there are not any baby pages, the perform outputs the message specified by the ifempty attribute.
  6. Reset Put up Knowledge: The wp_reset_postdata perform resets the WordPress question, making certain that the worldwide $put up object is restored to the unique most important question’s put up.
  7. Shortcode Registration: Lastly, the add_shortcode perform registers listchildpages as a brand new shortcode, linking it to the add_shortcode_listchildpages perform, making it obtainable to be used in posts and pages.

This perform is helpful for dynamically itemizing subpages on a father or mother web page, enhancing navigation and group inside a WordPress web site. I’d advocate including it to a {custom} plugin in case you’d like so as to add it to your WordPress web site. Or… you’ll be able to obtain the plugin I printed.

Checklist Baby Pages Shortcode Plugin

I lastly bought round to pushing the code right into a plugin to make it simpler to put in and make the most of, and the Checklist Baby Pages Shortcode plugin was authorized by WordPress right this moment! Please obtain and set up it – in case you prefer it, present a assessment!

WordPress Plugin for Listing Child Pages
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments