WordPress – Widget Parameters

I’m creating my own plugin and widget to display my latest work on my portfolio website, except I’m having some issues with the final part – displaying my data.

I’ve created a plugin which allows me to insert new data to my database – this holds information about a new project I’ve finished (Title, subtitle, href and src).

Read More

I can echo out all of this data just fine.

The next thing I did was to create a widget which allows me to display all of these bits of data, by calling it as follows:

the_widget( 'soc_latestwork','title=$data->Title.&subtitle=$data->SubTitle&image_src=$data->ImageSrc&image_url=$data->ImageUrl', array('before_widget' => '', 'after_widget' => ''));

And this displays my widget just fine… Expect the issue have is that my data is stored inside an array, which I can access by doing this

$data->Title

etc.

Now, how on earth am I meant to put this data in my widget? Whatever I put inside of it just gets echo’d out as plain text.

Here is my widget code which shows this data:

function widget($args, $instance)
    {

        extract($args);

        echo $before_widget;

        ?>

<div class="col-lg-3 col-sm-3 latest-work-box" data-scrollreveal="enter left after 0.15s over 1s">

            <!-- Image box -->

            <div class="latest-work-box-wrap">

                <a href="<?php echo $instance['image_src'] ?>" >
                    <img src="<?php echo $instance['image_url'] ?>" />
                </a>

                <div class="latest-work-box-wrap-bottom">

                <h1> <?php echo $instance['title'] ?> </h1>

                <h2> <?php echo $instance['subtitle'] ?> </h2>

                </div>

            </div>

        </div>

        <?php

        echo $after_widget;

    }

Related posts

1 comment

  1. You’re currently passing in an array of strings such as "$data->Title" when you should be passing the data contained within $data->Title.

    Change your widget call to this:

    the_widget( 'soc_latestwork','title='.$data->Title.'&subtitle='.$data->SubTitle.'&image_src='.$data->ImageSrc.'&image_url='.$data->ImageUrl, array('before_widget' => '', 'after_widget' => ''));
    

Comments are closed.