WordPress.org

Ready to get started?Download WordPress

Forums

Defined function gives undefined function error.. custom function (1 post)

  1. damusix
    Member
    Posted 7 months ago #

    OK so I'm making a theme here at work and I'm sectioning off all functions that aren't add_action or the like. I'm using them through-out for different things. They are stored in a file called 'theme_functions.php'

    So far I have like 7 functions in there, they're all running perfectly fine. I added another and when I try to use in anywhere, it gives me

    Fatal error: Call to undefined function myFunction() in /Users/mycomputer/PHP/MY_SITE/wp-content/themes/MY_THEME/page-home.php on line 15

    I asked PHP if the filed existed and it tells me YES

    echo file_exists(plugin_dir_path(__FILE__)."_include/inc/theme_functions.php") ? "YES" : "NO";

    I am including like so:

    require_once plugin_dir_path(__FILE__)."_include/inc/theme_functions.php";

    I know it's not scope because other functions work.

    My code is clean and I made sure my syntax was on point... So I'm resorting to asking here because I have absolutely NO clue what is going on.

    CODE:

    function myFunction($termName, $postsPerPage=10){
      $category = get_term_by( 'name', $termName, 'category' )->term_id;
      $posts = array();
    
      $post_args = array(
        'posts_per_page' => $postsPerPage,
        'category' => $category,
        'orderby' => 'post_date',
        'order' => 'DESC' );
      $p = get_posts($post_args);
    
      foreach ($p as $i => $post) {
        $img = get_primary_image($post->ID, '300'); // this is another non-wp function within the same file that WORKS
        $title = $post->post_title;
        $link = get_permalink($post->ID);
    
        $excerpt = substr($post->post_excerpt, 0, 90)."...";
        $x = array(
          'img' => $img,
          'title' => $title,
          'excerpt' => $excerpt,
          'link' => $link
        );
        array_push($posts, $x);
      }
      return array_chunk($posts, 2);
      }

    If anyone can point me in the right direction, I will love you forever and ever (restrictions may apply). Thanks again!

Reply

You must log in to post.

About this Topic