WordPress.org

Ready to get started?Download WordPress

Forums

Q and A FAQ and Knowledge Base for WordPress
Hard code instead of using shortcodes (8 posts)

  1. Tom
    Member
    Posted 3 years ago #

    Hi,

    Firstly, thanks for this plugin it seems to be nice & lightweight and exactly what I was looking for.

    I'm using it on an eCommerce site I've been working on, but I now was to add a FAQ category to product pages within a category.

    Eg. If a product is in the "toys" category, it will display the "toys" FAQ's... if a product is in the "Kitchenware" category it will show the "kitchenware" FAQ's...

    As it stands at the moment I'd have to insert:
    [qa cat="toys"] ...to every product in the toys category, manually.

    Shortcodes are good for many things but for this particular site I have over 3,000 products to add, so anything to save me time would be a huge bonus.

    I'm thinking that if I can edit my product_listing.php (post template) with some php code/ if conditionals, I can achieve the needed - with your help of course :)

  2. Dalton
    Member
    Plugin Author

    Posted 3 years ago #

    I think what you're looking for is the do_shortcode function. Something like this:

    <?php echo do_shortcode([qa cat="dogs"]);?>

    You can use conditionals in your template and you can also replace the contents of the shortcode with variables; between those two you should have everything you need to automate this for thousands of products!

    Cheers,
    Dalton

  3. Tom
    Member
    Posted 3 years ago #

    Thanks Dalton, that seems to work, but I'm struggling to understand how I use the conditionals for various categories (I have 8 different categories).

    Can you please give me an example of how to show a different shortcode for say, 3 categories?

    thanks

  4. Tom
    Member
    Posted 3 years ago #

    OK here is what I have done... can anyone tell me if this is OK or if there is a better way of doing it?

    [Code moderated as per the Forum Rules. Please use the pastebin]

  5. Dalton
    Member
    Plugin Author

    Posted 3 years ago #

    If these are going on product pages and not on category index pages then I think you'll want to use the in_category() conditional for all of them.

    And the correct format if you've got multiple categories is

    in_category( array( 1,2,3 ) )

    Dalton

  6. Tom
    Member
    Posted 3 years ago #

    Thanks Dalton but it seems that code isn't right in some way.. can you please re-post it inside code tags for me?

  7. Tom
    Member
    Posted 3 years ago #

    Think I've got it...

    <!-- Lets show some FAQs based on the category a product is in --> 
    
    <?php if (in_category( array( 1,2,3 ) ) ) { ?>			<!-- primary category IDs go here -->
    <strong>Primary frequently asked questions</strong>
    <p><?php echo do_shortcode('[qa cat="faq-primary"]'); ?></p>
    
    <?php } elseif (in_category( array( 1,2,3 ) ) ) { ?>					<!-- secondary category IDs go here -->
    <strong>Secondary frequently asked questions</strong>
    <p><?php echo do_shortcode('[qa cat="faq-secondary"]'); ?></p>
    
    <?php } else { ?> 										<!-- display this (nothing) if not in any of above categories -->
    <p></p>
    <?php } ?>
    <!-- FAQ #end-->
  8. digitalrenewal
    Member
    Posted 2 years ago #

    I would like to do the exact same thing, however my shortcode has no variables, I would just like to echo the content (what the user put between shortcode tags.

    Ive tried
    <?php echo do_shortcode('[sc]' . $content . '[/sc]'); ?>
    but it doesnt work

    anyone know how to do this?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags