Forums

Display (this) Based on Multiple Cat IDs from User Input (widget options) (2 posts)

  1. imscissors
    Member
    Posted 1 year ago #

    Hi,

    I've been searching high and low for how to do this for hours to no avail.

    I'm coding a plugin where my goal is to allow someone to place "whatever" on single posts based on category id's that they specify.

    Scenario
    ------------
    Place Google Adsense on post of these categories:

    In the widget options I provide a field to define category id's (comma separated):

    <!-- Multiple Category ID's -->
          <p>     
    
            <?php echo '<label for="' . $this->get_field_name('gaw_placebymcat') . '">' . __(' Multiple Categories by ID:') . '
            <input style="width:25%;" id="' . $this->get_field_id('gaw_placebymcat') . '" name="' . $this->get_field_name('gaw_placebymcat') . '"
            type="text" value="' . $gaw_placebymcat . '" /></label>'; ?>
    
          </p>

    So lets say their input is 6,11,17 - the person would want the widget (adsense ads) to display on all post in the categories 6,11, and 17.

    So I got the conditional tag in_category to correct return true or false based on the current posts categories by hard-coding the array as normal:

    if (in_category( array( '6', '11', '17' ) ) ) { echo 'display ads her because this is cat 6, 11 or 17';} else {echo 'we wont display because condition is false';}

    So I confirmed this as working. So it looks like I need an array like in the code above. So I looked how to create an array from a comma separated string and came up with this:

    $multicats = $gaw_placebymcat;
    //the variable is user defined cats sep'd by commas
    
    // split to array
    $multicatarray = explode(', ', $multicats);
    print_r($multicatarray);

    The result:
    Array ( [0] => 6,11 )

    But I don't think this what I want, and I have know idea how to incorporate that into the in_category condition. Again, I'm pretty new to PHP and a bit of a hack I guess that's how you learn :)

    Anyway, I'm so hard-headed I worked on this for 8 hours before asking here.

    Hope one of you can point in the right direction. Maybe I'm totally turned around here.

    So please, please help me!

    Thanks a bunch,
    Bryan

  2. imscissors
    Member
    Posted 1 year ago #

    Hello,

    After another 5 hours of searching I've still not found a solution here. My head is spinning, testing different things. I just need to know if I'm on the right track and maybe a little nudge.

    Can anyone help me out?

    Thanks,
    Bryan

Topic Closed

This topic has been closed to new replies.

About this Topic