WordPress.org

Ready to get started?Download WordPress

Forums

how to get the category of the parent? (3 posts)

  1. photocurio
    Member
    Posted 3 years ago #

    I am using the following php on my site:
    <?php if (is_category('My Category')) { echo '<a href="http://linkhere.com">'; } ?>
    This is to add a link if something is in a particular category.

    I want to be able to use this on the image.php template, which doesn't have a category.

    Is there a way to re-phrase it to look at the category of the post that the image is attached to? i'd assume this is the category of the parent.

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

    maybe using this
    http://codex.wordpress.org/Function_Reference/in_category
    in coordination with this
    http://codex.wordpress.org/Function_Reference/get_post
    might work.

    <?php
    $post_id = get_post($post->ID);
    $parent_post_id = $post_id->post_parent;
    
    if (in_category( 'My Category', $parent_post_id )) { echo '<a href="http://linkhere.com">link<?a>'; }
    ?>
  3. photocurio
    Member
    Posted 3 years ago #

    Wow. that works! I've been stumped on that little problem for days.

    site is here, btw: http://mumford-photo.com/

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.