-1

This question already has an answer here:

I have the following. When I call it, in the page, the content within the page will always be AFTER the plugin rather than before. Please can some help?

<?php
function category_has_children($term_id)
{
    $children = get_term_children($term_id, "category");
    if (is_array($children)) {
        return $children;
    } else {
        return false;
    }
}

// Add Shortcode
function dataservices_category($atts)
{

    // Attributes
    extract(shortcode_atts(array(
        'id' => ''
    ), $atts));

    // Code
    if (isset($id)) {
        $categories = get_categories('child_of=' . $id);
        foreach ($categories as $category) {
            if ($category->parent != $id) {
                echo '<div style="margin-left:50px;">';
                echo '<h4>' . $category->name . '</h4>';
            } else {
                echo '<h3>' . $category->name . '</h3>';
            }
            if (category_has_children($category->term_id) == false):
                echo '<ul>';
                foreach (get_posts('cat=' . $category->term_id) as $post) {
                    setup_postdata($post);
                    echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a> </li>';
                }
                echo '</ul>';
            endif;
            if ($category->parent != $id) {
                echo '</div>';
            }
        }
    }

}
add_shortcode('dataservices', 'dataservices_category');
?>

marked as duplicate by Pieter Goosen, Michael, Howdy_McGee, TheDeadMedic, Robert hue Jun 22 '15 at 5:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • There are still plenty of duplicates on this site regarding this issue :-) – Pieter Goosen Jun 17 '15 at 11:32
  • Never ever use extract(). It was completely removed from core about two years ago for very specific reasons, and the use of it has been highly discouraged – Pieter Goosen Jun 17 '15 at 11:44
4

Return the HTML instead of the echo

function dataservices_category($atts)
{

// Attributes
extract(shortcode_atts(array(
    'id' => ''
), $atts));
$html = "";
// Code
if (isset($id)) {

    $categories = get_categories('child_of=' . $id);
    foreach ($categories as $category) {
        if ($category->parent != $id) {
            $html .='<div style="margin-left:50px;">';
            $html .='<h4>' . $category->name . '</h4>';
        } else {
            $html .='<h3>' . $category->name . '</h3>';
        }
        if (category_has_children($category->term_id) == false):
            $html .='<ul>';
            foreach (get_posts('cat=' . $category->term_id) as $post) {
                setup_postdata($post);
                $html .='<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a> </li>';
            }
            $html .='</ul>';
        endif;
        if ($category->parent != $id) {
            $html .='</div>';
        }
    }
}
return $html;
}
add_shortcode('dataservices', 'dataservices_category');
  • Never ever use extract(). It was completely removed from core about two years ago for very specific reasons, and the use of it has been highly discouraged – Pieter Goosen Jun 17 '15 at 11:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.