WordPress.org

Ready to get started?Download WordPress

Forums

Widget Logic
Use $widget_ID ? (7 posts)

  1. squidz
    Member
    Posted 2 years ago #

    I am trying to target a specific widget to append a "all news" link below it's list of recent news posts, titles only.

    $widget_id is mentioned in the Other Notes for Widget Logic and it seems to be the perfect solution. However, there is no example regarding what to DO with the $widget_id variable within a function.

    so far I have:

    function all_news_link($content='', $widget_id'){
    ?><div>TEST</div><?php
    return $content;
    }
    add_filter('widget_content','all_news_link');

    I simply want to modify this so that the div with "TEST" in it will only show up on this specific widget with the ID="query-posts-3"

    http://wordpress.org/extend/plugins/widget-logic/

  2. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    it's a filter function - air gets the HTML to output ($content) and (in this case) the widget ID. all you have to do is modify the $content and return it. a bit like the code at the end of http://wordpress.org/extend/plugins/widget-logic/other_notes/

    something like

    return $content."<div>TEST</div>";

    will give you the right idea

  3. squidz
    Member
    Posted 2 years ago #

    alanft -

    My "Test" content shows up just fine. BUT it shows in ALL widgets. So, what I don't understand is how the function knows what the $widget_id variable is.

    Hows does $widget_id get a value (presumably the widget ID "query-posts-3") assigned to it?

    I've played with this for days now with only the ability to append text to every widget I'm using. That is why I'm here asking for assistance. I do not know how to get the plugin/function to ONLY apply the text "TEST" to the one widget I'm targeting.

  4. wrsowner
    Member
    Posted 2 years ago #

    The $widget_id is a string value that represents the CSS style ID for the LI that contains the widget (i.e., <li id="text-3">widget stuff</li>
    ... in this case, $widget_id = "text-3"). It's not a value to change, but a value that changes based on the particular widget being dealt with by WP at a given moment. A good example to see it in action is to set up 2-3 widgets in a test site, and place the following in your functions.php file:

    add_filter('widget_content', 'my_widget_function', 90, 2);
    function my_widget_function( $content='', $widget_id='') {
        return $content . '<p>This widget\'s style ID = ' . $widget_id . '</p>';
    }

    So, as you will see, each widget will contain whatever content is already in it AND at the end of each widget's content will be "This widget's style ID = <current widget ID>".

    HTH.

  5. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    @squidz, you have to change your function. first off get rid of the text that outputs the TEST text every time it's called

    ?><div>TEST</div><?php

    and instead get it to RETURN the text you want, e.g.

    return $content."ID=".$widget_id;

    which will instead put the widget_id at the end of the widget content. Then you can alter it to have conditional changes to what it returns.

  6. squidz
    Member
    Posted 2 years ago #

    We worked it out and have it under control now Alan. But it took a bunch of trial and error.

    Widget Logic isone of the most useful plugins around and this particular feature can quite powerful too. To allow people that aren't full-on developers to exploit it, including a clear and straightforward example of how to use the feature on Widget Logic's "Other Notes" page would be a wonderful thing.

  7. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    i've added a tiny bit extra text in the documentation (not yet updated on wordpress.org) that should give people the nudge needed.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic