Forums

[resolved] Something like the_category('&') but for post IDs ... (5 posts)

  1. benfranklin
    Member
    Posted 2 years ago #

    I need to feed into my code a list of the categories for a post in separated by ampersands (&) .... this is because I have create a loop on my single php so I can show related posts but only for the categories this single.php-based post is in. So, the loop requires the post id if I am to limit it to just outputing posts from that those categories ... any ideas? I am using the function below to list the categories by name separated by a comma ... all I need is the same thing for post ids but separated by &

    <?php the_category(','); ?>

    Any help would be greatly appreciated.

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    I need to feed into my code a list of the categories for a post in separated by ampersands (&)

    Did you try:

    <?php the_category('&'); ?>
  3. benfranklin
    Member
    Posted 2 years ago #

    Yes, but this function outputs category names and I need it to output their numerical category id

  4. MichaelH
    Volunteer
    Posted 2 years ago #

    <?php
    $metas='';
    foreach(get_the_category() as $cat) {
      $metas .= $cat->cat_ID . '& ';
    }
    echo substr($metas,0,-1);
    ?>
  5. benfranklin
    Member
    Posted 2 years ago #

    You the man now dawg!

Topic Closed

This topic has been closed to new replies.

About this Topic