Tiffany Israel
Tiffany Israel

Reputation: 470

Can't get 'get_the_title' to work in a function

I've used this in other functions, but it doesn't seem to work in this particular one...

  <?php
      $page = get_the_title();
      $blogusers = get_users('orderby=display_name');
      foreach ($blogusers as $user) {
        $cpt_count = wpse31443_author_has_custom_post_type( $user->ID, $page );

        if (!empty($cpt_count)) {
          echo '<li>' . $user->display_name . '' . $cpt_count1 . '</li>';
        }
      }
    ?>

If I change $page = get_the_title(); to $page = 'title';then it works, so it's something with get_the_title(); but I'm not sure what because it has worked in other functions.

Upvotes: 0

Views: 6150

Answers (2)

Cody O&#39;Dell
Cody O&#39;Dell

Reputation: 181

Try This:

<?php
  global $post;

  $page = $post->post_title;
  $blogusers = get_users('orderby=display_name');
  foreach ($blogusers as $user) {
    $cpt_count = wpse31443_author_has_custom_post_type( $user->ID, $page );

    if (!empty($cpt_count)) {
      echo '<li>' . $user->display_name . '' . $cpt_count1 . '</li>';
    }
  }
?>

Upvotes: 2

varun1505
varun1505

Reputation: 810

The most common reason why "get_the_title()" wont work is if it is not in "the loop". Make sure that you call the function from within the loop only. If called from elsewhere, you will need to pass the page/post id to the function.

You'll get more information here: http://codex.wordpress.org/Function_Reference/get_the_title

Upvotes: 2

Related Questions