Title: Adding via PHP
Last modified: August 21, 2016

---

# Adding via PHP

 *  Resolved [Amber Hinds](https://wordpress.org/support/users/alh0319/)
 * (@alh0319)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/adding-via-php/)
 * Hi Bill,
 * I was wondering if the short code would be recognized if it was added via a function
   added to the functions file and if I might be able to use a category function
   reference to have the short code pull the category it should display posts from
   based upon the category archive that it is being displayed on. I’m hoping to 
   have a list of posts displayed on the top on every category archive (below the
   taxonomy description but before the loop) and I would love if I could have it
   automatically do this for each category with the addition of one singular function.
   Any thoughts?
 * [https://wordpress.org/plugins/display-posts-shortcode/](https://wordpress.org/plugins/display-posts-shortcode/)

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Plugin Author [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * (@billerickson)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/adding-via-php/#post-4653464)
 * In your theme file, you can wrap things in the function do_shortcode() to make
   it run shortcodes.
 * Examples:
 *     ```
       <?php
   
       echo do_shortcode( '[display_posts'] );
   
       $my_output = '[display_posts]';
       echo apply_filters( 'do_shortcode', $my_output );
       ```
   
 *  Thread Starter [Amber Hinds](https://wordpress.org/support/users/alh0319/)
 * (@alh0319)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/adding-via-php/#post-4653598)
 * Hi Bill,
 * Thanks for the response. I am coming back to this finally. I’m trying to work
   out how I can automate it so that the post list will automatically be displayed
   at the top of each category archive listing the posts in that category. (Rather
   than me having to manually add it to the top of each category.) Here is what 
   I have:
 *     ```
       function post_list_on_category_archive() {
       if ( is_category() )
           	echo '<div class="before-category">';
       	echo do_shortcode( '[display_posts category="$category_id"]' );
   
       	$my_output = '[display_posts category="$category_id"]';
       	echo apply_filters( 'do_shortcode', $my_output );
       	echo '</div>';
   
       };
   
       add_action('genesis_before_loop', 'post_list_on_category_archive');
       ```
   
 * Does that look like it would work? Can the plugin understand how to get the category
   that is currently being displayed?
 * Thank you so much!!
 *  Plugin Author [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * (@billerickson)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/adding-via-php/#post-4653599)
 * A few issues I see:
 * 1. You haven’t defined $category_id. You’ll want $category_id = get_query_var(‘
   cat’ ) in there.
    2. You’re displaying the shortcode twice. This might have been
   caused by confusion from my last post. I was showing you two different examples.
   One is using the do_shortcode() function, and the other uses the do_shortcode
   filter. You only need one of those, not both.
 *  [maggew](https://wordpress.org/support/users/maggew/)
 * (@maggew)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/adding-via-php/#post-4653600)
 * If your trying to convert shortcode into php add this snippet into a theme file
   like `footer.php` (or whatever):
 *     ```
       <?php echo do_shortcode('[display-posts wrapper="div"]'); ?>
       ```
   
 * source: [http://codex.wordpress.org/Function_Reference/do_shortcode](http://codex.wordpress.org/Function_Reference/do_shortcode)
 *  Thread Starter [Amber Hinds](https://wordpress.org/support/users/alh0319/)
 * (@alh0319)
 * [12 years ago](https://wordpress.org/support/topic/adding-via-php/#post-4653601)
 * Thank you for the response, Bill, and thank you for the time. I know support 
   is not really your thing.
 * I am working though this and the only problem I seem to have is related to the
   current category generation. I am trying to use this code:
 *     ```
       //* Add List Above Category Archives
       function post_list_on_category_archive() {
       if ( is_category() ) {
           	echo '<div class="before-category">';
       	echo do_shortcode( '[display-posts posts_per_page="-1" order="ASC" orderby="title" columns="2" category="$category_id"]' );
       	$category_id = get_query_var( 'cat' );
       	echo '</div>';
       }
   
       };
   
       add_action('genesis_before_content', 'post_list_on_category_archive');
       ```
   
 * If I remove the “category=…” the short code will display as expected. But for
   some reason adding in category=”$category_id” returns nothing where the shortcode
   should be.
 * Thoughts?
 *  Plugin Author [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * (@billerickson)
 * [12 years ago](https://wordpress.org/support/topic/adding-via-php/#post-4653602)
 * Try this:
 *     ```
       //* Add List Above Category Archives
       function post_list_on_category_archive() {
       if ( is_category() ) {
           	echo '<div class="before-category">';
       	$category_id = get_query_var( 'cat' );
       	echo do_shortcode( '[display-posts posts_per_page="-1" order="ASC" orderby="title" columns="2" category="' . $category_id . '"]' );
       	echo '</div>';
       }
   
       };
   
       add_action('genesis_before_content', 'post_list_on_category_archive');
       ```
   
 *  Thread Starter [Amber Hinds](https://wordpress.org/support/users/alh0319/)
 * (@alh0319)
 * [12 years ago](https://wordpress.org/support/topic/adding-via-php/#post-4653603)
 * Still nothing. It is very odd that this one thing is causing it to disappear.
 * The website, by the way, is [http://enjoyrhinebeck.com/](http://enjoyrhinebeck.com/)
   if having that information tells you anything. Thanks.
 *  Plugin Author [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * (@billerickson)
 * [12 years ago](https://wordpress.org/support/topic/adding-via-php/#post-4653604)
 * I’m sorry, but there must be an issue with your theme or plugins you are running.
   That code works for me.
 *  Thread Starter [Amber Hinds](https://wordpress.org/support/users/alh0319/)
 * (@alh0319)
 * [12 years ago](https://wordpress.org/support/topic/adding-via-php/#post-4653605)
 * OK, thanks. I will keep trying to troubleshoot it. I’ll let you know if I can
   figure something out.
 *  Thread Starter [Amber Hinds](https://wordpress.org/support/users/alh0319/)
 * (@alh0319)
 * [12 years ago](https://wordpress.org/support/topic/adding-via-php/#post-4653606)
 * I finally figured out the issue, and boy do I feel slow. It wasn’t working because
   the your shortcode can’t pull categories based upon the ID, only the slug.
 *     ```
       //* Add List Above Category Archives
       function post_list_on_category_archive() {
       if ( is_category() ) {
           	echo '<div class="before-category">';
       	$cat = get_query_var('cat');
         	$yourcat = get_category ($cat);
       	echo do_shortcode( '[display-posts posts_per_page="-1" order="ASC" orderby="title" columns="2" category="' . $yourcat->slug . '"]' );
       	echo '</div>';
       }
   
       };
   
       add_action('genesis_before_loop', 'post_list_on_category_archive', 40);
       ```
   
 * With an adjustment to call the slug instead of teh category archive, it works
   perfectly. Thanks for all the help!!

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Adding via PHP’ is closed to new replies.

 * ![](https://ps.w.org/display-posts-shortcode/assets/icon-256x256.jpg?rev=2940963)
 * [Display Posts - Easy lists, grids, navigation, and more](https://wordpress.org/plugins/display-posts-shortcode/)
 * [Support Threads](https://wordpress.org/support/plugin/display-posts-shortcode/)
 * [Active Topics](https://wordpress.org/support/plugin/display-posts-shortcode/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/display-posts-shortcode/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/display-posts-shortcode/reviews/)

 * 10 replies
 * 3 participants
 * Last reply from: [Amber Hinds](https://wordpress.org/support/users/alh0319/)
 * Last activity: [12 years ago](https://wordpress.org/support/topic/adding-via-php/#post-4653606)
 * Status: resolved