Title: Where do I add code?
Last modified: December 15, 2017

---

# Where do I add code?

 *  [leunam12](https://wordpress.org/support/users/leunam12/)
 * (@leunam12)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/where-do-i-add-code/)
 * Hi, I am new to the forums and new to WordPress, after googling for many days
   I and not being able to find a satisfactory answer I ahve decided to post here.
   Basically my question is where do I put code in wordpress? I have been searching
   for answers to many questions I have and many times the answer that I find is:“
   use this code” and the code, but I have never seen an explanation of where to
   put that code and how. I have read and watched tutorials about wordpress but 
   they don’t deal with this issue.
 * Example: I have been looking for a way to display all the subcategories of a 
   certain category on a page. I found this answer: use this code…
 *     ```
       $args = array('child_of' => 17);
       $categories = get_categories( $args );
       foreach($categories as $category) { 
           echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
           echo '<p> Description:'. $category->description . '</p>';
           echo '<p> Post Count: '. $category->count . '</p>';  
       }
       ```
   
 * Great!! But where do I place that code? I put it on the page and it didn’t work.
   I created a custom page template and put it there and it didn’t work; I added
   php tags at the beginning and the end and it didn’t work. Pretty frustrating.
   I know, I know, i am a newbie and I have to be patient but I need help.
 * Thanks.
    -  This topic was modified 8 years, 5 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).

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

 *  Moderator [t-p](https://wordpress.org/support/users/t-p/)
 * (@t-p)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/where-do-i-add-code/#post-9787290)
 * See if this plugin can work for you:
    [https://wordpress.org/plugins/sub-categories-widget/](https://wordpress.org/plugins/sub-categories-widget/)
 * Or, take a look at the source code to get some idea.
 *  [sstoft](https://wordpress.org/support/users/sstoft/)
 * (@sstoft)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/where-do-i-add-code/#post-9787415)
 * I’m a newbie too, so this is only a clue, not an answer:
    put your code in: /
   wp-content/themes/yourTheme/functions.php And where is that? It’s in your WordPress
   folder — find it with FTP
 * (But I think it may be safer to make you own my-functions.php,
    however I forget
   how to get your them to read that.)
 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/where-do-i-add-code/#post-9787644)
 * I agree that it can be difficult to know what goes where on occasions.
 * You should be doing this customisation in a child theme, details here:
    [http://codex.wordpress.org/Child_Themes](http://codex.wordpress.org/Child_Themes)
 * I see two different places you could place your code.
    A) In a custom page template,
   how the named templates get used is described here: [https://developer.wordpress.org/themes/basics/template-hierarchy/](https://developer.wordpress.org/themes/basics/template-hierarchy/)
   First off include a big obvious tag that proves to you that your template is 
   being accessed, something like: `echo "<h2>Page Template TEST</h2>";`
 * B) Using a shortcode. Details here:
    [http://codex.wordpress.org/Shortcode_API](http://codex.wordpress.org/Shortcode_API)
   Basically in your functions.php add code like this:
 *     ```
       function subcat17_func( $atts ){
        echo '<h2>In Function subcat17</h2>';
        $args = array('child_of' => 17);
        $categories = get_categories( $args );
        foreach($categories as $category) { 
           echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
           echo '<p> Description:'. $category->description . '</p>';
           echo '<p> Post Count: '. $category->count . '</p>';  
       }
       add_shortcode( 'subcat17', 'subcat17_func' );
       ```
   
 * Then in a page or post you can activate you code with `[subcat17]`, again start
   with a proof of functionality.
 *  Thread Starter [leunam12](https://wordpress.org/support/users/leunam12/)
 * (@leunam12)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/where-do-i-add-code/#post-9789200)
 * Ok, I already created a child theme and a custom page template based on the original
   one. I wil be testing more in a couple of days. Thanks
 *  Thread Starter [leunam12](https://wordpress.org/support/users/leunam12/)
 * (@leunam12)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/where-do-i-add-code/#post-9789252)
 * Ok, I tried the shortcode and it works. At the beginning it was giving me a lot
   of errors and I was pulling my hair until I got completely bald, then I discovered
   that a } was missing on the function. After I typed that } at the end it started
   working. I will keep testing. Thanks for your answers.
 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/where-do-i-add-code/#post-9789434)
 * OOPS, sorry about that.
    I think it happened because I created the example code
   by copy/pasting two different snippets.
 *  Thread Starter [leunam12](https://wordpress.org/support/users/leunam12/)
 * (@leunam12)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/where-do-i-add-code/#post-9792585)
 * Yeah, don’t worry about that. I have written code before and I know how it is,
   that little semi-colon that you forget gives you a lot of headaches. The strange
   part is that all the colors were right on VIM when I pasted it; usually if there
   is something missing the colors get all messed up.
 * Thank you again.
    So… what if I don’t want to create a shortcode? should I just
   write the function in functions.php and then call it somehow from the page template?
   Or should I always try to solve it with shortcode?
 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/where-do-i-add-code/#post-9792900)
 * Your only option if you don’t want to use shortcodes is to put your code into
   the body part of a custom page template.

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

The topic ‘Where do I add code?’ is closed to new replies.

## Tags

 * [code](https://wordpress.org/support/topic-tag/code/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 4 participants
 * Last reply from: [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * Last activity: [8 years, 5 months ago](https://wordpress.org/support/topic/where-do-i-add-code/#post-9792900)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
