• 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.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator t-p

    (@t-p)

    See if this plugin can work for you:
    https://ww.wp.xz.cn/plugins/sub-categories-widget/

    Or, take a look at the source code to get some idea.

    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.)

    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.ww.wp.xz.cn/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.ww.wp.xz.cn/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.ww.wp.xz.cn/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

    (@leunam12)

    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

    (@leunam12)

    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.

    OOPS, sorry about that.
    I think it happened because I created the example code by copy/pasting two different snippets.

    Thread Starter leunam12

    (@leunam12)

    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?

    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.