WordPress: How To Record Baby Pages Utilizing A Shortcode

0
1


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

Sadly, there’s no inherent perform or characteristic to do that inside WordPress, so we developed a shortcode so as to add to the consumer’s web site. Right here’s how you need to use the shortcode with all its variables populated inside a WordPress publish 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 youngster pages discovered": This article will be displayed if there are not any youngster pages obtainable.
  • order="ASC": This kinds 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 youngster web page within the listing.
  • align="aligncenter": This aligns the featured photographs within the heart.

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

Moreover, for those who’d like a brief excerpt describing every web page, the plugin allows excerpts on pages to be able to edit that content material on the web page’s settings.

Record Baby Pages Shortcode

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

    $atts = shortcode_atts(array(
        'ifempty' => '<p>No Data</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].'" top="'.$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 Data

, which you need to 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 Publish Variable: The perform begins by declaring the worldwide variable $publish, which is used to entry details about the present publish 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 youngster pages.
    • order: Order of the kid pages (ASC or DESC).
    • orderby: Standards for ordering youngster 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 youngster pages of the present web page, sorted in accordance with the required attributes.
  4. Producing the Record:
    • If youngster pages are discovered, the perform constructs an HTML unordered listing (<ul>), with every youngster 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 youngster 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 youngster pages, the perform outputs the message specified by the ifempty attribute.
  6. Reset Publish Knowledge: The wp_reset_postdata perform resets the WordPress question, guaranteeing that the worldwide $publish object is restored to the unique most important question’s publish.
  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 beneficial for dynamically itemizing subpages on a father or mother web page, enhancing navigation and group inside a WordPress web site. I’d suggest including it to a {custom} plugin for those who’d like so as to add it to your WordPress web site. Or… you possibly can obtain the plugin I revealed.

Record 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 Record Baby Pages Shortcode plugin was authorized by WordPress right this moment! Please obtain and set up it – for those who prefer it, present a assessment!

WordPress Plugin for Listing Child Pages

LEAVE A REPLY

Please enter your comment!
Please enter your name here