Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:17:08+00:00 2026-06-10T23:17:08+00:00

Why if I call a custom function from within functions.php it does not work?

  • 0

Why if I call a custom function from within functions.php it does not work?

My custom function:

function get_cat_slug($ID) {
    $post_id = (int) $ID;
    $category = &get_category($post_id);
    echo $category->slug;
}

Loop trough all posts, like that:

$args = array(
 'numberposts' => -1
);  
$posts = get_posts($args);
foreach ($posts as $post){
   // and withing this lopp I would like to get a category slug
   // so I am calling my custom function, also included in functions.php
   get_cat_slug($post->ID);

}

But, get_cat_slug($post->ID) always return null. Why? What am I missing?
Any suggestions much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:17:10+00:00Added an answer on June 10, 2026 at 11:17 pm

    There should definitely not be an ampersand before get_category, which actually needs a Category ID and not a post ID.)

    get_the_category returns an array of categories (because posts can have multiple), and you also do not need to specify (int). If you only want to echo the slug of the first cat (assuming single categorization,) try:

    function get_cat_slug($post_id) {
      $cat_id = get_the_category($post_id);
      $category = get_category($cat_id[0]);
      echo $category->slug;
    }
    

    Following wordpress function styles, though, if you’re naming your function with get_..., it should return $category->slug rather than echo it, meaning you’d have to echo get_cat_slug($post->ID); in your template loop.

    Your loop depends on a WP_Query, which belongs in a template file and not functions.php. Which template file depends on what purpose your loop is serving and where you would like to display it. index.php is the logical choice for a primary post loop, though archive.php, single.php, or any pre-existing theme-specific template might make sense, as would any custom, non-standard template you create.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to call from C# a function in a custom DLL written in
Is it possible to call a function within an asp.net web application from outside
I would like to call a custom created function in access from VB NET.
How do I call a custom component's public function from the main app. For
I need to call a user defined javascript function from within my costom jquery
I would like to loop through an NSString and call a custom function on
I need to call a method and pass an object from my custom UITableViewClass
I'd like to call a custom function that is defined in a Python module
I'm making calls to C functions from R using R's .Call interface. Some of
I have a function in a functions.php file that defines certain variables: add_action( 'the_post',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.