Add Shortcode Inside Excerpt – WordPress Development Stack Exchange

Question

I have the following function:

function word_function(){
return 'PLACEHOLDER';
}

add_shortcode('word_place', 'word_function');

add_filter('get_the_excerpt', 'do_shortcode');

It places the word “PLACEHOLDER” anywhere I put the [word_place] shortcode.

I want to put the shortcode inside excerpts so that:

“Place Excerpt [word_place] Here”

Would look like:

“Place Excerpt PLACEHOLDER Here”

When the post/page is published.

At the moment it works for a blog page, however when it comes to category and tag pages it still looks like:

“Place Excerpt [word_place] Here”

I’ve had a similar issue with widgets and I’ve used filters to correct the problem.

I’ve found the following filters for excerpts:

add_filter('get_the_excerpt', 'do_shortcode');
add_filter( 'the_excerpt', 'shortcode_unautop');
add_filter( 'the_excerpt', 'do_shortcode');
echo do_shortcode(get_the_excerpt());
echo do_shortcode(the_excerpt());

archive.php

<?php
/**
 * Template for displaying archive layout for easy digital downloads
 *
 * @package OceanWP WordPress theme
 */

global $post;

do_action( 'ocean_before_archive_download_item' );

?>

<div <?php post_class(); ?>>

    <div class="edd_download_inner">

    <?php

    // Get elements.
    $elements = oceanwp_edd_archive_elements_positioning();

    // Loop through elements.
    foreach ( $elements as $element ) {

        // Image.
        if ( 'image' === $element ) {
            do_action( 'ocean_before_archive_download_image' );

            edd_get_template_part( 'shortcode', 'content-image' );

            do_action( 'ocean_after_archive_download_image' );
        }

        // Category.
        if ( 'category' === $element ) {

            do_action( 'ocean_before_archive_download_categories' );

            echo '<div class="edd_download_categories">';

            echo oceanwp_edd_terms_list( 'download_category' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

            echo '</div>';

            do_action( 'ocean_after_archive_download_categories' );
        }

        // Title.
        if ( 'title' === $element ) {

            do_action( 'ocean_before_archive_download_title' );

            edd_get_template_part( 'shortcode', 'content-title' );

            do_action( 'ocean_after_archive_download_title' );

        }

        // Price.
        if ( 'price' === $element ) {

            do_action( 'ocean_before_archive_download_price' );

            edd_get_template_part( 'shortcode', 'content-price' );

            do_action( 'ocean_before_archive_download_price' );

        }

        // Description.
        if ( 'description' === $element ) {

            do_action( 'ocean_before_archive_download_description' );

            edd_get_template_part( 'shortcode', 'content-excerpt' );

            do_action( 'ocean_after_archive_download_description' );

        }

        // Add to cart button.
        if ( 'button' === $element ) {

            do_action( 'ocean_before_archive_download_add_to_cart' );

            echo oceanwp_edd_add_to_cart_link(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

            do_action( 'ocean_after_archive_download_add_to_cart' );

        }
    }
    ?>

    </div>

</div>

<?php

do_action( 'ocean_after_archive_download_item' );

index.php

<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme and one of the
 * two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * For example, it puts together the home page when no home.php file exists.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package OceanWP WordPress theme
 */

get_header(); ?>

    <?php do_action( 'ocean_before_content_wrap' ); ?>

    <div id="content-wrap" class="container clr">

        <?php do_action( 'ocean_before_primary' ); ?>

        <div id="primary" class="content-area clr">

            <?php do_action( 'ocean_before_content' ); ?>

            <div id="content" class="site-content clr">

                <?php do_action( 'ocean_before_content_inner' ); ?>

                <?php
                // Check if posts exist.
                if ( have_posts() ) :

                    // Elementor `archive` location.
                    if ( ! function_exists( 'elementor_theme_do_location' ) || ! elementor_theme_do_location( 'archive' ) ) {

                        // Add Support For EDD Archive Pages.
                        if ( is_post_type_archive( 'download' ) || is_tax( array( 'download_category', 'download_tag' ) ) ) {

                            do_action( 'ocean_before_archive_download' );
                            ?>

                            <div class="oceanwp-row <?php echo esc_attr( oceanwp_edd_loop_classes() ); ?>">
                                <?php
                                // Archive Post Count for clearing float.
                                $oceanwp_count = 0;
                                while ( have_posts() ) :
                                    the_post();
                                    $oceanwp_count++;
                                    get_template_part( 'partials/edd/archive' );
                                    if ( oceanwp_edd_entry_columns() === $oceanwp_count ) {
                                        $oceanwp_count = 0;
                                    }
                                endwhile;
                                ?>
                            </div>

                            <?php
                            do_action( 'ocean_after_archive_download' );
                        } else {
                            ?>
                        <div id="blog-entries" class="<?php oceanwp_blog_wrap_classes(); ?>">

                            <?php
                            // Define counter for clearing floats.
                            $oceanwp_count = 0;
                            ?>

                            <?php
                            // Loop through posts.
                            while ( have_posts() ) :
                                the_post();
                                ?>

                                <?php
                                // Add to counter.
                                $oceanwp_count++;
                                ?>

                                <?php
                                // Get post entry content.
                                get_template_part( 'partials/entry/layout', get_post_type() );
                                ?>

                                <?php
                                // Reset counter to clear floats.
                                if ( oceanwp_blog_entry_columns() === $oceanwp_count ) {
                                    $oceanwp_count = 0;
                                }
                                ?>

                            <?php endwhile; ?>

                        </div><!-- #blog-entries -->

                            <?php
                            // Display post pagination.
                            oceanwp_blog_pagination();
                        }
                    }
                    ?>

                    <?php
                    // No posts found.
                    else :
                        ?>

                        <?php
                        // Display no post found notice.
                        get_template_part( 'partials/none' );
                        ?>

                    <?php endif; ?>

                <?php do_action( 'ocean_after_content_inner' ); ?>

            </div><!-- #content -->

            <?php do_action( 'ocean_after_content' ); ?>

        </div><!-- #primary -->

        <?php do_action( 'ocean_after_primary' ); ?>

    </div><!-- #content-wrap -->

    <?php do_action( 'ocean_after_content_wrap' ); ?>

<?php get_footer(); ?>

However, none of them work for me.

Any assistance is appreciated.

0
Discover 2 months 2021-06-11T22:29:20-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse