Shortcodes

Content

  1. Overview
  2. Current Limitations
  3. Add a Shortcode with Parameter to a Layout
  4. Add a Shortcode with Parameters in the Pre-Loop or Post-Loop Fields
  5. Show Category/Taxonomy Description on NON-Archive Pages via Shortcode
  6. Show Custom Page Title for Same LoopBuddy on Different Pages via Shortcode
  7. Add Previous and Next Post Links on Single Post Pages
  8. Add Previous and Next Title Links on Single Post Pages
  9. Add the Post Author Avatar
  10. Add Navigation from the WP-PageNavi Plugin
  11. Add a Link to Post Comments in the Post List (Archive Page)
  12. Add Post Commetns to a Single Post or to Posts on the Post List &#40Archive Page&#41
  13. Add Comment Form to a Single Post or to Posts in the Post List &#40Archive Page&#41
  14. Add Content from a Custom Field to a Layout
  15. Add Number of Post Views
  16. Add Number of Comments
  17. Add Archive Page Headers

Overview

Shortcodes basically allow almost any plugin or custom code to be plugged in anywhere in WordPress.

LoopBuddy supports shortcodes, allowing you to put any shortcode, whether from a plugin or your own, into any place in LoopBuddy Layout, including the Pre-Loop Text / HTML , Post-Loop Text / HTML and No Results Text / HTML sections. This probably makes the already powerful LoopBuddy the only most powerful way to control all your content, loops and display on your site.

Info on editing or adding stuff to Functions.php

Current Limitations (LoopBuddy 1.2.X and Earlier)

Loopbuddy does not process shortcodes with parameters, such as [pb_slideshow group="2"] . There is a workaround, following are examples on how to add such a shortcode to the layout for each post, and an example on how to add this to the Pre-Loop or Post-Loop fields (ironically, using another shortcode). The methods described in the next two examples will work for all shortcodes that use parameters, e.g. DisplayBuddy plugins, Gravity forms etc.

Add a Shortcode with Parameters to a Layout

Add the shortcode (in this example, we will use [pb_slideshow group="2"] , to show a slideshow) to a custom field in your post. In this example, the custom field is named post-slideshow .

Then, add the following code at the end of your functions.php file (but before the closing ?> , if any).

Code

function insert_my_custom_field_shortcode() {
    global $post;
    
    return do_shortcode( get_post_meta( $post->ID , "post-slideshow" , true ) );
}
add_shortcode('insert-slideshow-in-post', 'insert_my_custom_field_shortcode');

Usage

Drag a shortcode into the LoopBuddy layout of your post, and add the [insert-slideshow-in-post] shortcode. The function will retrieve the contents of the custom field ( post-slideshow ) and insert it where defined in the layout.

Add a Shortcode with Parameters in the Pre-Loop or Post-loop Fields

If you want to add a shortcode for the entire page (or if you want to add the same shortcode to each post layout) (in this example, [pb_videoshowcase group="0"] , to show a videoshowcase group), add the following code at the end of your functions.php file (but before the closing ?> , if any).

Code

function get_preloop_videos() {

    return do_shortcode( "[pb_videoshowcase group='0']" );
    
}
add_shortcode('my-videos', 'get_preloop_videos');

Usage

You can then add the shortcode [my-videos] to the LoopBuddy layout editor. Note that the name of the shortcode can be anything you want, as long as it matches the code in your functions.php.

Show Category/Taxonomy Description on NON-Archive Pages via Shortcode

Category Descriptions are a little more sophisticated than other post related variables because if there is more than one category being used, then it will be hard for WordPress to display ALL descriptions nicely OR one category (which one?).

  • Outside of archive pages, to display Category Descriptions (or any taxonomy description) you have to do the following:
    • 1 : Put the following anywhere (usually near the end of the file) in your theme's custom functions.php file (it creates a shortcode you'll use to display the description of a Category or any Taxonomy):
    add_action( 'init', 'lb_cat_init' );
    function lb_cat_init() {
        add_shortcode('taxdescription', 'lb_the_description');
    } //end lb_cat_init
    function lb_the_description() {
        global $post;
        if ( !is_object( $post ) ) return;
        $terms = get_post_meta( $post->ID, 'description_terms', true );
        $taxonomy = get_post_meta( $post->ID, 'description_taxonomy', true );
        $return = '';
        if ( $terms && $taxonomy ) {
            if ( !is_array( $terms ) ) {
                $terms = explode( ',', $terms );
            }
            foreach ( $terms as $term_name ) {
                $term = get_term_by( 'slug', $term_name, $taxonomy );
                if ( !$term ) continue;
                $description = get_term_field( 'description', $term->term_id, $term->taxonomy );
                if ( empty( $description ) || !$description ) continue;
                $return .= apply_filters( 'pb_the_content', $description );
            }
        }
        return $return;
    } //end lb_the_description

      • 2 : In the posts you want to show the category description, add the following 2 custom field names and values:

    Custom Field Name ----- Custom Field Value

    description_taxonomy ----- category

    description_terms ----- books (OR any category name you have)

    • 3 : Add a Shortcode Tag to your LoopBuddy layout which has the following shortcode in it:

    [taxdescription]

    That will show the category description anywhere you put the shortcode.


    Show Custom Page Title for Same LoopBuddy Layout on Different Pages via Shortcode

    One awesome example of using Shortcodes with LoopBuddy is to show a custom page title, along with any LoopBuddy Layout content, where different pages use the same LoopBuddy Layout. Our awesome LoopBuddy customer Bruce provided the following info to help you all with that:

    • 1 : Put the following anywhere (usually near the end of the file) in your theme's custom functions.php file:
    function return_title() {
    return '<h1>'.get_the_title().'</h1>';
    }
    add_shortcode('pg_title', 'return_title');
    • 2 : Add [pg_title] anywhere in the LoopBuddy Layout where you want to show the custom page title.

    That will show the custom page title just for that specific post/page where the Layout is being shown.

    Related links: LoopBuddy user Bruce asking about and then sharing the code to show custom Page Titles on each Page where the same LoopBuddy Layout is used

    Add Previous and Next Post Links on Single Post Pages

    If you want to show a Previous or Next Post link on a single post page, you can do so using a shortcode. Add the following code at the end of your functions.php file (but before the closing ?> , if any).

    Code

    function my_previous_next_post() {
        // retrieve the value for next post link
        $next_string = "Next post &rarr;";
        ob_start(); 
        next_post_link("%link", $next_string);
        $next_link = ob_get_clean(); 
        
        // retrieve the value for previous post link
        $previous_string = "&larr; Previous post";
        ob_start(); 
        previous_post_link("%link", $previous_string);
        $previous_link = ob_get_clean(); 
        
        // build output
        $return = PHP_EOL . '<div id="next-previous" class="navigation clearfix">' . PHP_EOL;
    
        // display previous link if any
        if ($previous_link) {
            $return .= '<div class="nav-previous alignleft">'. PHP_EOL;
            $return .= $previous_link. PHP_EOL;
            $return .= '</div>'. PHP_EOL;
        }
    
        // display next link if any
        if ($next_link) {
            $return .= '<div class="nav-next alignright">'. PHP_EOL;
            $return .=  $next_link . PHP_EOL;
            $return .= '</div>'. PHP_EOL;
        }
    
        $return .= '</div>';
    
        return $return;
    }
    add_shortcode('previous-next-post-links', 'my_previous_next_post');

    Usage

    Insert the shortcode [previous-next-post-links] in your LoopBuddy layout where you want the Previous and Next post links to appear.

    Customize the Appearance

    • you may have to add some styling, using css you can target the classes used in this example
    • you can change the appearance of the links by changing the following lines in the code:
    $next_string = "Next post &rarr;";

    and
    $previous_string = "&larr; Previous post";

    Add Previous and Next Title Links on Single Post Pages

    If you want to show a Previous or Next Post Title link on a single post page, you can do so using a shortcode. Add the following code at the end of your functions.php file (but before the closing ?> , if any).

    Code

    function my_previous_next_post() {
    // retrieve the value for next post link
    $next_post = get_adjacent_post(false, '', false) ;
    $next_string = get_the_title($next_post->ID);
    ob_start();
    next_post_link("%link", $next_string);
    $next_link = ob_get_clean();
    
    // retrieve the value for previous post link
    $previous_post = get_adjacent_post(true, '', true) ;
    $previous_string = get_the_title($previous_post->ID);
    ob_start();
    previous_post_link("%link", $previous_string);
    $previous_link = ob_get_clean();
    
    // build output
    $return = PHP_EOL . '<div id="next-previous" class="navigation clearfix">' . PHP_EOL;
    // display previous link if any
    if ($previous_link) {
             $return .= '<div class="nav-previous alignleft">'. PHP_EOL;
             $return .= $previous_link. PHP_EOL;
             $return .= '</div>'. PHP_EOL;
    }
    // display next link if any
    if ($next_link) {
             $return .= '<div class="nav-next alignright">'. PHP_EOL;
             $return .= $next_link . PHP_EOL;
             $return .= '</div>'. PHP_EOL;
    }
    $return .= '</div>';
    return $return;
    }
    add_shortcode('previous-next-post-links', 'my_previous_next_post');

    Usage

    Insert the shortcode [previous-next-post-links] in your LoopBuddy layout where you want the post title Previous and Next post links to appear.

    Add the Post Author Avatar


    If you want to show the post author avatar on a page, add the following code at the end of your functions.php file (but before the closing ?> , if any).

    Code

    function my_insert_avatar() {
        global $post;
        return get_avatar($post->post_author, 64 );
    }
    add_shortcode('insert-author-avatar', 'my_insert_avatar');

    Usage

    Insert the shortcode [insert-author-avatar] in your LoopBuddy layout where you want the Avatar to appear.

    Customize the Appearance

    In this example, the avatar is displayed as a 64px by 64px image. Change this value if you want the avatar in another format.

    Additional documentation

    get_avatar function reference from WordPress Codex

    Add Navigation from the WP-PageNavi Plugin

    LoopBuddy includes support for the wp_pagenavi plugin . Loopbuddy will insert the navigation at the bottom of the page, if you have installed and activated the plugin, and if you have ticked "Enable Paging" in the Query editor. However, if you want more control over when and where the wp_pagenavi navigation will appear, you can do so using a shortcode. Note that if you want to use a shortcode to insert the navigation, you should untick the "Enable Paging" setting, otherwise, it will show up twice. Add the following code at the end of your functions.php file (but before the closing ?> , if any).

    Code

    function my_insert_pagenavi() {
        return wp_pagenavi();
    }
    add_shortcode('insert-page-navi', 'my_insert_pagenavi');

    Usage

    Insert the shortcode [insert-page-navi] in your LoopBuddy layout where you want the wp-pagenavi pagination to appear. Generally, this would be in either (or both) the "Pre-Loop Text / HTML" or the "Post-Loop Text / HTML", since we usually don't want the pagination to appear in every post in a list of posts.

    Add a Link to Post Comments in the Post List (Archive Page)


    If you want to add a link to the post comments to your (archive) layout, add the following code at the end of your functions.php file (but before the closing ?> , if any).

    Code

    function my_insert_comments_link() {
        return '<a href="' . get_comments_link() . '">Comments to this post</a>';
    }
    add_shortcode('insert-comments-link', 'my_insert_comments_link');

    Usage

    Insert the shortcode [insert-comments-link] in your LoopBuddy where you want the link to appear.

    Customize the Appearance

    In this example, the link will show as clickable text "Comments to this post". You can change this to any text, or an image.

    Additional documentation

    get_comment_link function reference from WordPress Codex

    Add Post Comments to a Single Post or to Posts in the Post List (Archive Page)

    If you want to add comments, and a comment form to a post, add the following code at the end of your functions.php file (but before the closing ?> , if any).

    Code

    function my_insert_comments() {
    
        ob_start();
        global $withcomments;
        $withcomments = true; 
        comments_template();
        $my_comments = ob_get_contents();
        ob_end_clean();
     
        return $my_comments;
        
    }
    add_shortcode('insert-comments', 'my_insert_comments');

    Usage

    Insert the shortcode [insert-comments] in your LoopBuddy layout where you want the comments to appear.

    Additional documentation

    comments_template function reference from WordPress Codex

    Add Comment Form to a Single Post or to Posts in the Post List (Archive Page)

    If you want to show just a comment form with your post on a single post page or archive page, add the following code at the end of your functions.php file (but before the closing ?> , if any).

    Code

    function my_insert_comment_form() {
    
        ob_start();
        comment_form();
        $my_comment_form = ob_get_contents();
        ob_end_clean();
     
        return $my_comment_form;
           
    }
    add_shortcode('insert-comment-form', 'my_insert_comment_form');

    Usage

    Insert the shortcode [insert-comment-form] in your LoopBuddy layout where you want the comment form to appear.

    Additional documentation

    comment_form function reference from WordPress Codex

    Add Content from a Custom Field to a Layout

    If you want to add the content of a custom field to your layout, add the following code at the end of your functions.php file (but before the closing ?> , if any).

    Code

    function get_my_custom_field() {
        global $post;
        
        // example 1: return an image in image tags
        return '<img src="' . get_post_meta($post->ID, "my_custom_field_image", true) . '" class="custom-image" />';
    
        // example 2: return just the contents of the custom field
        return get_post_meta($post->ID, "my_custom_field_name", true);
    
        // example 3: return the result of a shortcode
        return do_shortcode( get_post_meta($post->ID , "my-shortcode" , true) );
    }
    add_shortcode('insert-custom-field', 'get_my_custom_field');

    Usage

    The example shows three ways to return the content. Only one should be used.

    • The first example assumes that the contents of the custom field my_custom_field_image is an image URL, so it will return the image url in html <img> tags, so that the image will be displayed as an image, and not the url of that image.
    • The second example retrieves the value of the custom field my_custom_field_name and will return that value. Insert the shortcode [insert-custom-field] in your LoopBuddy layout where you want the comment form to appear.
    • You can even add a shortcode to the custom field, example 3 shows how to return the results of a shortcode my_shortcode and insert that in your layout. The custom field contents should include the shortcode brackets, so [my_shortcode] .

    Additional documentation

    get_post_meta function reference from WordPress Codex

    Add Number of Post Views

    This function relies on the wp-postviews plugin. Download, install, activate and configure the plugin. Then, to insert the number of postviews in your layout, add the following code at the end of your functions.php file (but before the closing ?> , if any).

    Code

    function my_insert_post_views() {
    
        ob_start();
        if( function_exists('the_views') ) { 
            the_views(); 
        }
        $post_views = ob_get_contents();
        ob_end_clean();
     
        return $post_views;
    
    }
    add_shortcode('insert-post-views', 'my_insert_post_views');

    Usage

    Insert the shortcode [insert-post-views] in your LoopBuddy layout where you want the number of post views to show up.

    Add Number of Comments

    This function will insert the number of comments as a linked entry. Add the following code at the end of your functions.php file (but before the closing ?> , if any).

    Code

    function my_insert_comments_count() {
        
        $num_comments = get_comments_number();
    
        if ( comments_open() ) {
            if ( $num_comments == 0 ) {
                $comments = __('No Comments');
            } elseif ( $num_comments > 1 ) {
                $comments = $num_comments . __(' Comments');
            } else {
                $comments = __('1 Comment');
            }
    
            $return_comments = '<a href="' . get_comments_link() .'">'. $comments.'</a>';
        } else {
            $return_comments =  __('Comments disabled for this post.');
        }
     
        return $return_comments;
        
    }
    add_shortcode('insert-comments-count', 'my_insert_comments_count');

    Usage

    Insert the shortcode [insert-comments-count] in your LoopBuddy layout where you want the number of post views to show up.

    Add Archive Page Headers

    If you want to show the header that appears on Archive and Search Results pages ("Archive for etc."), you can do so using a shortcode. The following code is taken from any theme's archive.php page, you can add it at the end of your functions.php file (but before the closing ?> , if any).

    Code

    function my_lb_archives_header() {
        
        if ( is_category() ) { // Category Archive
            $title = sprintf( __( 'Archive for %s', 'it-l10n-Builder' ), single_cat_title( '', false ) );
        }
        else if ( is_search() ) { // Tag Archive
            $title = sprintf( __( 'You searched for %s', 'it-l10n-Builder' ), get_search_query());
        }
        else if ( is_tag() ) { // Tag Archive
            $title = sprintf( __( 'Archive for %s', 'it-l10n-Builder' ), single_tag_title( '', false ) );
        }
        else if ( is_tax() ) { // Taxonomyg Archive
            $title = sprintf( __( 'Archive for %s', 'it-l10n-Builder' ), builder_get_tax_term_title() );
        }
        else if ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() && function_exists( 'post_type_archive_title' ) ) { // Post Type Archive
                $title = post_type_archive_title( '', false );
        }
        else if ( is_author() ) { // Author Archive
            $title = sprintf( __( 'Author Archive for %s', 'it-l10n-Builder' ), get_the_author() );
        }
        else if ( is_year() ) { // Year-Specific Archive
            $title = sprintf( __( 'Archive for %s', 'it-l10n-Builder' ), get_the_time( 'Y' ) );
        }
        else if ( is_month() ) { // Month-Specific Archive
            $title = sprintf( __( 'Archive for %s', 'it-l10n-Builder' ), get_the_time( 'F Y' ) );
        }
        else if ( is_day() ) { // Day-Specific Archive
            $title = sprintf( __( 'Archive for %s', 'it-l10n-Builder' ), get_the_date() );
        }
        else if ( is_time() ) { // Time-Specific Archive
            $title = __( 'Time Archive', 'it-l10n-Builder' );
        }
        else { // Default catchall just in case
            $title = __( 'Archive', 'it-l10n-Builder' );
        }
    
        // wrap result in h4 tags
        $output = '<h4 class="loop-title">' . $title .  '</h4>';
        
        return $output;
    }
    add_shortcode( 'archives-header', 'my_lb_archives_header' );

    Usage

    Insert the shortcode [archives-header] in the "Pre-Loop Text / HTML" field in your LoopBuddy layout editor for the specific layouts you created.

Powered by Zendesk