WordPress.org

Ready to get started?Download WordPress

Forums

Yet Another Related Posts Plugin (YARPP)
[resolved] Executing shortcodes in YARPP excerpts (7 posts)

  1. ckbedwell
    Member
    Posted 1 year ago #

    Hello,

    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?

    http://wordpress.org/extend/plugins/yet-another-related-posts-plugin/

  2. mitcho (Michael Yoshitaka Erlewine)
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  3. ckbedwell
    Member
    Posted 1 year ago #

    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; ?>
    </div>
    
    <?php else: ?>
    <p id=no-display>Hello world</p>
    <?php endif; ?>

    Any solution would be much appreciated! :D

  4. mitcho (Michael Yoshitaka Erlewine)
    Member
    Plugin Author

    Posted 1 year ago #

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

  5. ckbedwell
    Member
    Posted 1 year ago #

    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!

  6. mitcho (Michael Yoshitaka Erlewine)
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  7. ckbedwell
    Member
    Posted 1 year ago #

    Perfect; it does work!

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic