Support » Plugin: Yet Another Related Posts Plugin (YARPP) » Executing shortcodes in YARPP excerpts

  • Resolved ckbedwell



    Firstly, I adore this plugin, it is beautifully crafted and this is the only issue I’ve had so far.

    I’m using the plugin as an excellent way to show related products with the eshop plugin (as it uses pages for its products) and I want to be able to display the Add-to-cart button for these products in the sidebar. The Add-to-Cart button is displayed using shortcodes.

    I have modified a custom template so it shows the product thumbnail, title and the excerpt, however in the excerpt for the related pages it shows the actual text for the shortcode, e.g. [eshop_addtocart id=404] and doesn’t execute it.

    I’ve added a filter to my functions.php and this makes the shortcode work for all my other excerpts.
    : `add_filter(‘the_excerpt’, ‘shortcode_unautop’);
    add_filter(‘the_excerpt’, ‘do_shortcode’);’`

    Does anybody know how I can execute the shortcodes in the excerpts displayed using the YARPP plugin?

Viewing 6 replies - 1 through 6 (of 6 total)
  • This sounds like a great use case! 🙂 Let me ask you: where are you adding these shortcodes? Is this in a custom YARPP template file, or do you mean you created a custom file in your theme?

    The text outputted by custom YARPP templates are indeed not run through the shortcode-translating code. If that’s what you’re doing, I could add a hook so that you can add the relevant filter.

    Thanks for the kind compliments and the reply!

    I’m adding the shortcodes in each Pages’ excerpt box. So I create a page, fill out the relevant information to make it a product, then add the shortcode in the custom excerpt box whilst creating the page.

    The shortcode executes when you are looking at archive pages (due to the filter I mentioned in my first post), but doesn’t execute for the YARPP plugin.

    This is my custom YARPP template:

    <?php ?>
    <?php if (have_posts()):?>
    <h3>Related products</h3>
    <div id=related-products-sidebar>
    	<?php while (have_posts()) : the_post(); ?>
    		<?php if (has_post_thumbnail()):?>
    		<div class=related-product><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail();
    					?><h4><?php echo get_the_title(); ?></h4></a><?php echo get_the_excerpt(); ?></div>
    		<?php endif; ?>
    	<?php endwhile; ?>
    <?php else: ?>
    <p id=no-display>Hello world</p>
    <?php endif; ?>

    Any solution would be much appreciated! 😀

    Sorry, I’m still confused… I don’t see any shortcode being produced in that YARPP template…

    Ah, that might be the problem — I’m still not overly comfortable with PHP, so may have been some miscommunication! I might just be missing some code in that YARPP template (if so, please tell me what to add…)

    I’ll link you to my half-built site so you can see what I mean:

    So, I fill out my shortcode in my excerpt here (screenshot).

    This is a category page, archiving all the Chaos Space Marines.

    It pulls on the excerpts for what to display, as you can see the shortcode executes and the add to cart button is there for each product.

    On this page, using the YARPP widget in the sidebar it pulls the excerpts up but the shortcode doesn’t execute.

    I hope that makes things clearer, your personal responses are much appreciated!

    Oh, I see. So it’s the excerpt which includes the shortcodes.

    I’m not sure if it will work, but try this: change the line with the excerpt in your YARPP template to be echo do_shortcode(get_the_excerpt()). In other words, wrap the output of the excerpt in do_shortcode, so it’ll force the shortcode processing there. Hope that helps.

    Perfect; it does work!

    Thank you very much for your assistance, much much appreciated!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Executing shortcodes in YARPP excerpts’ is closed to new replies.