functions - Using Switch Statement to Change Image According to Last Digit of Topic ID

admin2025-01-08  5

I am trying to build a function, which gives out different image-urls, according to the last digit of bbp_get_topic_id().
This is, what i've got so far:

function bg_image_topic_titlearea() {
  $letztezifferimgs = substr("'.bbp_get_topic_id().'", -1);
  switch ($letztezifferimgs) {
    case "0":
        echo ".jpg";
        break;
    case "1":
        echo ".jpg";
        break;
    case "2":
        echo ".jpg";
        break;
    case "3":
        echo ".jpg";
        break;
    case "4":
        echo ".jpg";
        break;
    case "5":
        echo ".jpg";
        break;
    case "6":
        echo ".jpg";
        break;
    case "7":
        echo ".jpg";
        break;
    case "8":
        echo ".jpg";
        break;
    case "9":
        echo ".jpg";
        break;
}
}  

Yet it does not react, if i use .bg_image_topic_titlearea(). to get the image url.
I am trying to get it done for some time now and i would be glad, if someone could help me.

Kind regards
Dominik

I am trying to build a function, which gives out different image-urls, according to the last digit of bbp_get_topic_id().
This is, what i've got so far:

function bg_image_topic_titlearea() {
  $letztezifferimgs = substr("'.bbp_get_topic_id().'", -1);
  switch ($letztezifferimgs) {
    case "0":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/10/Blockaden-lösen-2.jpg";
        break;
    case "1":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/09/Das-Leben-ist-ein-Spiel-2.jpg";
        break;
    case "2":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2017/02/Bedürfnisse-erkennen-3.jpg";
        break;
    case "3":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/08/Selbsterkenntnis-sich-selbst-finden2-1.jpg";
        break;
    case "4":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/11/Reinkarnation-Wiedergeburt-2.jpg";
        break;
    case "5":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/10/Intuition3.jpg";
        break;
    case "6":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/07/Manchmal-muss-man-loslassen-3.jpg";
        break;
    case "7":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2016/07/Manchmal-muss-man-loslassen-3.jpg";
        break;
    case "8":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2017/02/Pfad.jpg";
        break;
    case "9":
        echo "https://test.bewusstewelt.de/wp-content/uploads/2017/01/Zirbeldrüse-aktivieren-2.jpg";
        break;
}
}  

Yet it does not react, if i use .bg_image_topic_titlearea(). to get the image url.
I am trying to get it done for some time now and i would be glad, if someone could help me.

Kind regards
Dominik

Share Improve this question edited Mar 23, 2018 at 15:02 Michael Ecklund 6,81213 gold badges68 silver badges110 bronze badges asked Mar 29, 2017 at 16:24 Dominik BaumgartnerDominik Baumgartner 13 bronze badges 5
  • Please give an example of the full statement you're using to get the image url, as there might be a problem there, too. – CK MacLeod Commented Mar 29, 2017 at 16:49
  • Its a bundle of shortcodes, where i want to include the outcome of this switch statement. It works for sure, because, if i enter an image url directly, it works also. – Dominik Baumgartner Commented Mar 29, 2017 at 20:29
  • 1 Well even one typical statement would be useful. – CK MacLeod Commented Mar 29, 2017 at 20:33
  • I am not sure, if i get your question right. This is, where i want to include the outcome: function bbpress_topic_add_titlearea() { echo do_shortcode('[et_pb_section bb_built="1" admin_label="section" background_image=" '.bg_image_topic_titlearea().'...' ) } – Dominik Baumgartner Commented Mar 29, 2017 at 20:44
  • The fragmentary snippet you're provided is obviously incomplete. I'll just say not that incorporating a function as shortcode attribute strikes me as unusual. I think if you provided the ENTIRE shortcode function or set of functions, the problems would be easier to fix in detail. For now I'll provide a guess as to the problem below. – CK MacLeod Commented Mar 29, 2017 at 23:04
Add a comment  | 

2 Answers 2

Reset to default 0

I would try something like this:

function bg_image_topic_titlearea() {

    if ( ! function_exists( 'bbp_get_topic_id' ) ) {
        return false;
    }

    if ( ! $letztezifferimgs = bbp_get_topic_id() ) {
        return false;
    }

    $letztezifferimgs = substr( $letztezifferimgs, - 1 );

    // switch ...

    return true;

}

I would recommend checking to make sure the function call (bbp_get_topic_id();) to retrieve the Topic ID exists.

I would recommend checking to see if there's actually a value received from the function call to retrieve the Topic ID.

Not sure why you need to use substr();, but perform that once you know you have received a Topic ID.

THEN perform your switch.

I would also recommend placing a "default case" in your switch statement.

Example of a "default case":

default:

    // do something...

break;

Without seeing the full code in question, it's difficult to isolate its problems. So, please forgive me if I've misunderstood.

I'll just add to what I was suggesting in my comments above that a more typical shortcode implementation would be 1) to have the image-url function return, not echo, its output, and 2) to use a simple string as shortcode attribute, not a function returning a string. Typically, the function or its arguments would already have returned the string within the shortcode function.

Further on return v echo, the Codex states:

Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.

It looks, in short, like you're trying to return an echo, or maybe echo an echo. (I'll also add that, at least up until today when I made the same mistake a couple or three times, using an echo or equivalent is a mistake I make or have made frequently - sometimes you can get away with it, and sometimes it produces unexpected or simply bad results.)

As for the second part, incorporating a function as an attribute when placing the shortcode wherever it goes, I've never tried and I don't think I've ever run across it, though it's hard to be sure what you're doing or to test it, based on what's presented in the question. In any event .bg_image_topic_titlearea(). seems to suggest an intention to, again, return rather than echo a string within a longer statement - i.e., echo 'Here's the image url: ' . bg_image_topic_titlearea() . 'How do you like it?'; For that, you'd want a returned value, not an echoed statement.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267730a1218.html

最新回复(0)