Welcome to Praful Bhuskute BlackSpy Praful Bhuskute BlackSpy Praful Bhuskute BlackSpy Praful Bhuskute BlackSpy

Shortcode Element always displaying at the top of the page WordPress plugin Issue

This Solution of Shortcode always displayed at the top of the page for the WordPress plugin

Old Code

				
					<?php

add_shortcode('breadcrumbs', 'breadcrumbs_shortcode');

function breadcrumbs_shortcode()
{
    global $post;
    $post_slug = $post->post_name;

    $breadcrumbs_url = '/article';

    $term_obj_list  = get_the_terms( $post->ID, 'type' );

    $url_string = '
        <a style="color: var( --e-global-color-secondary );" href="/">
            Home
        </a>
    ';

     foreach($term_obj_list as $term) {

        $breadcrumbs_url .= '/'.$term->slug;

        $url_string .= '
            <a style="color: var( --e-global-color-secondary );" href="'.$breadcrumbs_url.'">
            <span class="breadcrumbs-separator">»</span> '.$term->name.'
            </a>
        ';
     }

     $url_string .= '
        <a style="color: var( --e-global-color-primary );">
            <span class="breadcrumbs-separator">»</span> '.$post->post_title.'
        </a>
    ';

     echo $url_string;
}

?>
				
			

Add this code after function

				
					ob_start();
				
			

Add this code before end function

				
					return ob_get_clean();

				
			

New updated code

				
					<?php

add_shortcode('breadcrumbs', 'breadcrumbs_shortcode');

function breadcrumbs_shortcode()
{
ob_start();

    global $post;
    $post_slug = $post->post_name;

    $breadcrumbs_url = '/article';

    $term_obj_list  = get_the_terms( $post->ID, 'type' );

    $url_string = '
        <a style="color: var( --e-global-color-secondary );" href="/">
            Home
        </a>
    ';

     foreach($term_obj_list as $term) {

        $breadcrumbs_url .= '/'.$term->slug;

        $url_string .= '
            <a style="color: var( --e-global-color-secondary );" href="'.$breadcrumbs_url.'">
            <span class="breadcrumbs-separator">»</span> '.$term->name.'
            </a>
        ';
     }

     $url_string .= '
        <a style="color: var( --e-global-color-primary );">
            <span class="breadcrumbs-separator">»</span> '.$post->post_title.'
        </a>
    ';

 
     echo $url_string;
return ob_get_clean();

}

?>
				
			

Drop your queries

Table of Contents

Have A Project In Mind? Let's Get To Work