[Resolved] GP Hooks conditional not working

Home Forums Support [Resolved] GP Hooks conditional not working

Home Forums Support GP Hooks conditional not working

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #371846
    Keith

    Hi tom

    I have tried put this code in Before Header Content (and also in After Header Content)

    <?php if ( is_page( 3874, 3890, 3892, 3893 ) ) { ?>
    ONE
    <?php } else { ?>
    TWO
    <?php } ?>

    But it ALWAYS goes into else part of code and prints out TWO

    What is wrong with it? It seems straight forward enough.

    I have been trying to change Tagline for just 4 pages on site with this code which consequently also does not work

    <?php if ( is_page( 3874, 3890, 3892, 3893 ) ) {
    update_option( ‘blogdescription’, ‘ONE’);
    ?>
    ONE

    <?php } else {
    update_option( ‘blogdescription’, ‘TWO’);
    ?>
    TWO
    <?php } ?>

    Is there abetter way to do this than Hooks?

    Incidentally the pages are 4 sub-pages and are CPTs (‘Service’) – does this matter
    (Some of url slugs have hyphens but it’s the same with IDs and urls and page names anyway)

    I have ticked php checkbox and cleared browser caches (tried with Chrome & Firefox)
    Cheers

    #372006
    Leo
    Staff
    Customer Support

    Hi there,

    If you need multiple pages then you need to use an array.

    Checkout the example here: https://codex.wordpress.org/Conditional_Tags#A_PAGE_Page

    #372450
    Keith

    Hi Leo

    Thanks for that – I actually sent an earlier version of my code to you… I have had the array syntax in and it still ALWAYS goes into the ELSE part of the statement?…

    corrected code-

    <?php if ( is_page( array( 3874, 3890, 3892, 3893 ) ) ) {
    update_option( ‘blogdescription’, ‘ONE’);
    ?>
    ONE

    <?php } else {
    update_option( ‘blogdescription’, ‘TWO’);
    ?>
    TWO
    <?php } ?>

    going to different pages always gives me TWO?

    Cheers

    #372547
    Lyle

    Hi Keith –

    This works and was noted in this forum in the post indicated 🙂

    Conditional taglines
    
    https://generatepress.com/forums/topic/how-do-i-display-slightly-different-taglines-on-different-pages/
    
    <?php if ( is_page( 'page-slug-1' ) ) { ?>
          tagline #1
    <?php } elseif ( is_page( 'page-slug-2' ) ) { ?>
          tagline #2
    <?php } elseif ( is_home() ) { ?>
          tagline for blog page
    <?php } else { ?>
          tagline for all other pages
    <?php } ?>
    
    

    Cheers!
    Lyle

    #372572
    Keith

    Hi Lyle

    Thanks for that.

    However that is my issue – I originally took that code and amended for my site (as above) and it just does NOT seem to work – so I am currently out of ideas as to why not… I suspect some other config or something really obvious that I just cannot see! 🙂

    Incidentally it’s the same just for a single page whether I use page id, name or url.

    Ideas welcome

    #372702
    Tom
    Lead Developer
    Lead Developer

    Just to confirm, you have “Execute PHP” checked, correct?

    As for using update_option – that should be done in a function outside GP Hooks, and should be hooked into an action like after_setup_theme or wp.

    #372711
    Keith

    Hi Tom

    Yes, I have ‘Execute PHP’ checked and ‘Disable Hook’ UNchecked.

    I did not know if this was the correct place to use this code – probably why it’s not working if it’s in the wrong place!

    I basically just want to change the tagline for 4 of my pages and leave it as per the global setting for the rest.
    Its purely a requirement for display only and if you know how I can do it using a different way/function then I would like to know! 🙂

    When I first go to a page after Enabling the Hook it does change to the Else code (ie “TWO”) – hence why I felt it was working – at least in part.

    If you could elaborate on how exactly I should do what you suggest I would appreciate it as I am new to Hooks and GP

    Thanks in advance

    #372714
    Tom
    Lead Developer
    Lead Developer

    Try this function:

    add_filter( 'option_blogdescription', 'tu_update_tagline' );
    function tu_update_tagline( $option ) {
        if ( is_page( 10 ) ) {
            return 'TWO';
        }
    
        return $option;
    }

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    #372753
    Keith

    Hi Tom

    Thanks for quick reply!

    Just so I understand –
    Can ‘option_blogdescription’ be anything or is it a name equivalent to the ‘update_option( ‘blogdescription’, ‘TWO’)’ that I was trying to use?

    Also I want to actually change the display on screen – not just return a value.

    I still don’t quite understand as I have added your code into a New Code Snippet and it did not seem to work?
    I wondered if the $option parameter passed in was supposed to be $options like what was returned?

    Anyway still did not work

    So I added my original function back in – not sure if it’s correct or not and I still cant get it to work

    Here is my final code in the Code Snippets Plugin (set to run front and back end at moment and I am testing on front end)

    add_filter( ‘option_blogdescription’, ‘tu_update_tagline’ );
    function tu_update_tagline( $options ) {
    if ( is_page( 3874 ) ) {
    update_option( ‘blogdescription’, ‘For All Your Business Needs’);
    return ‘For All Your Business Needs’;
    } else {
    update_option( ‘blogdescription’, ‘For All The Family’);
    return ‘For All The Family’;
    }

    return $options;
    }

    I have done for just one page till I get it working. Also not sure if I need a return value or JUST run the function?

    Cheers

    #372757
    Tom
    Lead Developer
    Lead Developer

    The option_ filter allows us to filter any option output in the DB. Super powerful.

    Using update_option() is overkill for something like this, as it actually updates the DB value on each page load.

    Your exact code would be this:

    add_filter( 'option_blogdescription', 'tu_update_tagline' );
    function tu_update_tagline( $option ) {
        if ( is_page( 3874 ) ) {
            return 'TWO';
        }
    
        return $option;
    }
    #372775
    Keith

    Hi Tom
    Thanks but I still don’t fully understand you – that is same code as I had originally ??

    I have tried that again and it still does not work?…

    The tagline for the page does NOT change on page 3874 (checked id in view source code)

    I understand about not actually needing to store value in the DB but did not know how to just change the value at the last minute before it’s displayed on the page – which presumably what this filter is trying to do (and so I don’t need an ELSE part for all the other pages?)

    So I am still stuck I’m afraid

    #372934
    Tom
    Lead Developer
    Lead Developer

    Make sure the IDs are right.

    Here’s an example, I have a page: mysite.com/sample-page

    Here’s my function:

    add_filter( 'option_blogdescription', 'tu_update_tagline' );
    function tu_update_tagline( $option ) {
        if ( is_page( 'sample-page' ) ) {
            return 'TWO';
        }
    
        return $option;
    }

    And here’s the result: https://www.screencast.com/t/EBCcJ3gnuMu

    #373188
    Keith

    Hi Tom

    Thanks for response – your demo is exactly what I am trying to do. – Very simple (in theory!)

    I see your latest code has $option as both the passed in parameter and the return value which makes sense to me. (Whereas your earlier code that you have edited was $option and $options with a typo hence why that did not work).

    I have double checked id (again) and put your exact code with either the id or the name or the slug but STILL does not work. Is the problem something else than the code? (All I have changed in your code is ‘sample-page’ to the name of my page ‘business-events’)

    Incidentally it does not matter that my ‘page’ is a CPT of ‘service’, does it?

    Thanks for your help so far
    Cheers

    #373192
    Tom
    Lead Developer
    Lead Developer

    Ah, if it’s a CPT, try: is_singular( 'sample-page' ) instead.

    #373197
    Keith

    Hi

    Tried is_singular but still no joy (:-

    However I decided to try the is_page code on another test site and that DOES work-hooray.
    No CPTs on this one just simple pages.

    But still there’s an issue with this particular site thats stopping it working – or it needs different code because of CPT perhaps?

    Cheers

Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.