WordPress.org

Forums

PHP in Text Widget help (9 posts)

  1. dnorth
    Member
    Posted 6 years ago #

    Hi,

    I am trying to dynamically pick an image to display in the sidebar based on the page id. It works for the most part. But....

    I have install and tried to use three differnet plugins the enable me to have a text widget that executes PHP code. They all seem to work but none of them will execute this PHP switch statement correctly.

    <img src="/wp-content/uploads/images/sidebar_img_<?php
    switch (the_ID()) {
    Case 11:
    case 14:
    case 19:
    case 21:
    case 24:
    case 49: echo the_ID();break;
    default: echo "1" };
    ?>.jpg">

    It does not do the break. If I take out the default it works, if I don't it alwasy does the default even if it matches one of the other cases. So, I get the page ID and "1".

    This seems simple, but I can't find the issue.

    Help.

    David

  2. You should wrap code in backticks for one.

    Also, you can't do PHP code in a text widget.

    Try this plugin: http://wordpress.org/extend/plugins/php-code-widget/

  3. dnorth
    Member
    Posted 6 years ago #

    I have tried that on and others.
    -Exec PHP - Weber
    -Executable PHP Widget - Otto
    -Samsarin PHP Widget - Pettitt

    All executle PHP in a widget on the side bar. All which work with the exceptions noted above.

    The problem is not getting some PHP to execute in the widget, the problem is to get the switch statement and break to work.

    David

  4. Can you re-paste your code and use backticks?

    This is clearly not complete but my GUESS is that it can't take the_ID() as a param for switch so try...

    <img src="/wp-content/uploads/images/sidebar_img_<?php
    $id = the_ID();
    switch ($id):
        case 11:
            echo "11";
            break;
        case 19:
            echo "19";
            break;
        case 49:
            echo "$id";
            break;
        default:
            echo "1";
    endswitch;
    ?>.jpg">
  5. dnorth
    Member
    Posted 6 years ago #

    Thanks,

    I had tried this, but I tried it again. On the ones tha have a value that matches like 11, it gets "111" becuase it still add the "1" from the default.

    So still doesn't work.

  6. dnorth
    Member
    Posted 6 years ago #

    Here is the code I use originally. Alhough I have tried lots of variations.

    <img src="/wp-content/uploads/images/sidebar_img_<?php
    
    switch (the_ID()) {
    Case 11:
    case 14:
    case 19:
    case 21:
    case 24:
    case 49: echo the_ID();break;
    default: echo "1" }
    ?>.jpg">

    Thanks for any help.

  7. dnorth
    Member
    Posted 6 years ago #

    Can anyone help with this?

  8. dnorth
    Member
    Posted 6 years ago #

    I tried changing to an if statement and it also does not work.

    <img src="/wp-content/uploads/images/sidebar_img_<?php $id = the_ID();
    if ($id==1 or $id==14 or  $id==19 or $id==11 or $id==21 or $id==24 or  $id==49) echo the_ID();
    else echo "1";
    ?>.jpg">

    If the id=11 then iget "111'

    It is a mystery to me.

    Anyone have any ideas on this?

  9. dnorth
    Member
    Posted 6 years ago #

    So the problem was the the the_ID() need to be call within the loop and outside the look it alwasy genreated data when it was called regardless of the php switch or if.

    Had to make a global varible in the main templact to get the id.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags