WordPress.org

Ready to get started?Download WordPress

Forums

Help needed to get posts Category Name (4 posts)

  1. anotherbreed
    Member
    Posted 2 years ago #

    I need some help (I'm a big PHP noobie) to show an image like this:

    <img src="www.mysite.com/IMAGE.jpg" />

    I would like "IMAGE" to be replaced by the actual category name of the specific post. I tried the following snippets, found on Codex and on this forum, with no luck (they all result in "www.mysite.com/.jpg"):

    <?php echo $cat->category_nicename; ?>
    <?php echo $post->cat_name; ?>
    <?php echo $category->cat_name; ?>
    <?php get_the_category( $id ) ?>
    <?php echo $post->cat_name;?>
    <?php echo $category->cat_name;?>

    Only this one worked, but showed me only one post and broke the site layout:

    <?php foreach((get_the_category()) as $category) { echo $category->cat_name; } ?>

    Any other suggestions? Many thanks

  2. anotherbreed
    Member
    Posted 2 years ago #

    Anyone?

  3. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    try:

    <img src="www.mysite.com/<?php foreach(get_the_category() as $category) { echo $category->slug; break; } ?>.jpg" />

  4. anotherbreed
    Member
    Posted 2 years ago #

    Like the previous it worked, but showed me only one post and broke the site layout... thank you Alchymyth, I suspect it's fault of the 'foreach' thing.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.