• drinkingsouls

    (@drinkingsouls)


    Hey, I wasn’t really sure where to post this. I’m a bit embarrassed asking for help on this as I’m a second year Web Devloper and really should know the answer.
    Basically I have a PHP statement that displays my page navigation plugin. It says if the function exists then display it.
    <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    I’ve been trying to structure it as an If Else so that if the function does not exist then I could display a few text links. I can’t seem to get the structure right. I have used plenty of If Else stetements in the past but I’m unfarmilliar of how to use it when calling on functions such as this. Would anybody be able to help me structure this into an If Else statement?. I know it’s something simple, but it’s still beyond my knowledge.
    Any help would be greatly appreciated.
    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Michael

    (@alchymyth)

    this should explain enough:
    https://www.w3schools.com/php/php_if_else.asp

    if you get stuck with php functions for instance, use web search:
    in this case searching (googling) for ‘php if else’ would give the above link, and many more.

    i find that w3schools are general are good source of reliable and short information.

    Thread Starter drinkingsouls

    (@drinkingsouls)

    Hey, thanks but I’ve already tried looking up if else on the net. This is the code I’m using.

    <?php
    if (function_exists('wp_pagenavi')) {
        wp_pagenavi();
    } else {
        echo "Custom Text If Navigation Does Not Exist";
    }
    ?>

    I see nothing wrong with the code and on the homepage the navigation is displayed, but on anywhere else the custom text is not displayed, nothing is. I think the problem maybe that it’s looking to see if the function exists in general, not on that page. So on a content page it finds that the function does exist but can’t display it because it doesn’t exist for that page. Does that make sense?. The function exists, but it is not used on any page other than the homepage.
    I think this maybe what’s happening, or the code is just completely wrong?, but if it was badly formatted then my site wouldn’t display, nevermind the navigation.

    Michael

    (@alchymyth)

    your if/else logic is ok.

    pagenavi only exists on a page of posts (usually index.php) – to go from paginated post page to the next (if you have more posts than fit onto one page).
    it is otherwise done by ‘next_posts_link()’ and ‘previous_posts_link()’ or ‘posts_nav_link()’. wp_pagenavi checks for the plugin.

    on a ‘page’ page you won’t have pagenavi (the word might be misleading). (you might have ‘wp_link_pages’ in the page template, to navigate if the page is spread over separate pages by inserting <!--nextpage-->.)

    and on single.php you may find ‘next_post_link()’ and ‘previous_post_link()’.

    you will find the description all in the codex.

    Thread Starter drinkingsouls

    (@drinkingsouls)

    I see, I was thinking it may be calling the plugin rather than the codex. I still don’t understand why the code above doesn’t work though. It displays the page navi correctly on index.php where it is wanted, but the else statement doesn’t seem to work. Whenever I go onto any other page nothing is displayed at all. I would expect the text to be displayed because the page navi function does not exist for that page, but it does not echo back the custom text. Nothing is displayed after index.php. I don’t understand why this happens.
    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘PHP If Else Statement Structure.’ is closed to new replies.