I made a custom page template within my plugin, that page contains a do_action('foo');. The function hooked to foo loops through my custom taxonomy terms. But whenever I have added a new term it doesn’t show on the page untill I: Click to edit the page with the template and click save.

Hope everything is clear.

My template:

<?php 
/* 
 * Template Name: Foo
 */

get_header(); 
?>

<div class="wrap">
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
            <?php
            /*
             * Loop through webshops
             */
            do_action('foo');

            while ( have_posts() ) : the_post();

                get_template_part( 'template-parts/page/content', 'page' );

            endwhile; // End of the loop.
            ?>

        </main>
        <!-- #main -->
    </div>
    <!-- #primary -->
</div>
<!-- .wrap -->

<?php get_footer(); ?>

Function:

<?php

function foofunction() {
    $terms = get_terms( array(
                'taxonomy' => 'foobar',
                'hide_empty' => false,
            ) );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        $count = count( $terms );
        $i = 0;

        $term_list = '<p>';
        foreach ( $terms as $term ) {
            $i++;

            $t_ID = $term->term_id;
            $term_data = get_option("taxonomy_$t_ID");

            $term_list .= '<img style="height: 150px" src="'.$term_data['img'].'"><a href="' . esc_url( get_term_link( $term ) ) . '" >' . $term->name . '</a>';
            if ( $count != $i ) {
                $term_list .= ' <br /> ';
            }
            else {
                $term_list .= '</p>';
            }


        }
        echo $term_list;
    }
}

add_action('foo','foofunction');
?>

Read more here: Custom plugin with page template is not dynamic


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: