WordPress.org

Ready to get started?Download WordPress

Forums

Help to get parent post slug (3 posts)

  1. surfsup74
    Member
    Posted 1 year ago #

    I'm having trouble outputting the parent slug.

    Here's what I've got so far (however this currently outputs nothing within the $media_items parameters):

    <?php function the_parent_slug() {
      global $post;
      if($post->post_parent == 0) return '';
      $post_data = get_post($post->post_parent);
      return $post_data->post_name;
    } ?>
    
    <?php $media_items = get_attachments_by_media_tags('size=thumb&media_tags='.$the_parent_slug); ?>

    I've also tried putting 'the_parent_slug' within an echo to see if anything is output and I get nothing.

    <?php echo '' . $the_parent_slug . ''; ?>

    Probably something simple?

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently damaged/corrupted by the forum's parser.]

  2. codingpet
    Member
    Posted 1 year ago #

    Hi,

    I saw you write a function but did you run the function actually? e.g. $the_parent_slug = the_parent_slug(); And Then use the $the_parent_slug variable.

  3. surfsup74
    Member
    Posted 1 year ago #

    OK - I now have...

    <?php function the_parent_slug() {
    global $post;
    if($post->post_parent == 0) return '';
    $post_data = get_post($post->post_parent);
    return $post_data->post_name;
    } ?>
    <?php $the_parent_slug = the_parent_slug(); ?>
    <?php $media_items = get_attachments_by_media_tags('size=thumb&media_tags='.$the_parent_slug); ?>

    ...but still get nothing in the $media_items parameters. Where am I going wrong here?

Topic Closed

This topic has been closed to new replies.

About this Topic