[Resolved] Adding Conditional code in Hooks

Home Forums Support [Resolved] Adding Conditional code in Hooks

Home Forums Support Adding Conditional code in Hooks

Viewing 15 posts - 1 through 15 (of 24 total)
  • Author
    Posts
  • #1257130
    GeneratePressUser

    Hello,

    Can you pls provide dummy conditional statement for:

    If post id = XX show {Following shortcode or html or anything which we want}

    Same pls provide for pages, post title, post slug etc if possible pls provide whole list of it, so that we can figure it out and create our code.

    Thanks!

    #1257151
    GeneratePressUser

    Hello,

    Ok I figured it out.

    <?php if ( is_single( ‘post-id’ ) ) : ?>
    Custom code here
    <?php endif; ?>

    Here is the list of all WP conditional tags:

    https://developer.wordpress.org/themes/references/list-of-conditional-tags/

    #1257502
    David
    Staff
    Customer Support

    Glad to hear you found your solution

    #1266752
    GeneratePressUser

    Hello David,

    Now I got a new problem:

    On using:

    <?php if ( is_single( ‘post-id’ ) ) : ?>
    [Custom shortcode here]
    <?php endif; ?>

    It works cool

    So we have 1 category with 35 posts so we have in created in elements > generate press hook, where we have such 35 codes at a place:

    <?php if ( is_single( ‘post-id’ ) ) : ?>
    [Custom shortcode here]
    <?php endif; ?>

    Like that 35 times total, 1 for each post.

    So it works fast and all is good, but when we add in each code more shortcodes like:

    <?php if ( is_single( ‘post-id’ ) ) : ?>
    [Custom shortcode 1]
    [Custom shortcode 2]
    <?php endif; ?>

    The page load time becomes 20 – 30 secs.

    Can you pls give us a better way to integrate it? like maybe so many php open and close causing issues?

    Also I tried implementing it like:

    <?php if ( is_single( ‘post-id-1’ ) ) : ?>
    [Custom shortcode 1]
    <?php endif; ?>

    <?php if ( is_single( ‘post-id-1’ ) ) : ?>
    [Custom shortcode 2]
    <?php endif; ?>

    So in short for 35 posts with each having 2 shortcode it makes it 35 x 2 = 70 such codes but I feel that works perfect and fast.

    So, not sure why using 2 shortcodes inside together don’t work.

    Is there a better way to implement which would reduce code size and help us keep same page speed?

    Let me know.

    Thanks!

    #1267129
    David
    Staff
    Customer Support

    Why not just use the GP Hook element to add your Shortcodes ?
    https://docs.generatepress.com/article/hooks-element-overview/

    You can set the Display Rules to Post Category to cover all of those posts.

    #1267373
    GeneratePressUser

    Hello David,

    That will be too many elements created there.

    We have 200+ codes for 200+ posts.

    So, making 200+ elements won’t be something good to manage, conditional is best, to manage and edit from the same place.

    So it’s like 200 posts divided in 5 post categories, so just 5 elements with display rule of just post category in each and that each category having their post conditional elements.

    Also pls we request u guys to add duplicate button for elements.

    So, pls let me know better way to add that code.

    Let me know 🙂

    #1267398
    David
    Staff
    Customer Support

    How may DIFFERENT shortcodes do you have ?

    #1267406
    GeneratePressUser

    Hello,

    I have 3 – 4 shortcodes approx for each post so each post category on average have 20 – 50 posts and as told earlier each category has its element hook.

    So, how to implement via PHP conditions to not ruin up speed.

    #1267559
    David
    Staff
    Customer Support

    So for each post you have 3 – 4 different shortcodes ? So for 20 posts that could 60 different shortcodes ? Is that correct.

    #1269312
    GeneratePressUser

    Hello David,

    Yes it could increase that’s just estimate so 3 – 4 for each post and have such 215 approx posts

    But can you tell why you are counting that? I just need PHP implementation better way or some different code.

    So, let me know 🙂

    #1269693
    Tom
    Lead Developer
    Lead Developer

    Hi there,


    [Custom shortcode 1]
    [Custom shortcode 2]

    The page load time becomes 20 – 30 secs.

    So having two shortcodes in the conditional increased the load time like that? Do the shortcodes execute once the page does load?

    #1269805
    GeneratePressUser

    Hello,

    Yes or else speed is 1 – 2 sec max.

    And weird thing is if written just 1 short code in condition then it works fast.

    So I tried writing both shortcodes seprate (not in same php condition) and it works fast.

    But this makes all conditions = number of shortcodes, in short making code double.

    Is there a better way to implement it?

    #1270273
    David
    Staff
    Customer Support

    I think if we had a better understanding of what the shortcodes do and their relationship to each post then we may be able to provide a better alternative.

    What i am trying to understand is how many actual unique shortcodes do you have ?

    Is it 200 posts x 4 different shortcodes = 800 shortcodes ?
    Or is is just 4 different shortcodes ?

    #1270370
    GeneratePressUser

    Hello,

    It’s different shortcodes, not same.

    #1270371
    David
    Staff
    Customer Support

    So potentially around 800 different shortcodes ?

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