Created
March 7, 2018 22:01
-
-
Save elseloop/2a9cbd2595401db0a6fbff7429ce909b to your computer and use it in GitHub Desktop.
WordPress function to print all instances of a passed shortcode, using built-in WP search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function turn_find_shortcode($atts, $content=null) { | |
| ob_start(); | |
| extract( shortcode_atts( array( | |
| 'find' => '', | |
| ), $atts ) ); | |
| $string = $atts['find']; | |
| $args = array( | |
| 's' => $string, | |
| ); | |
| $the_query = new WP_Query( $args ); | |
| if ( $the_query->have_posts() ) { | |
| echo '<ul>'; | |
| while ( $the_query->have_posts() ) { | |
| $the_query->the_post(); ?> | |
| <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> | |
| <?php | |
| } | |
| echo '</ul>'; | |
| } else { | |
| echo "Sorry no posts found"; | |
| } | |
| wp_reset_postdata(); | |
| return ob_get_clean(); | |
| } | |
| add_shortcode('shortcodefinder', 'turn_find_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment