0

This incredible website and the genius developers here have helped me significantly in the course of my WP Dev over the years, firstly THANK YOU.

I am now faced with a problem that I have been trying to find a solution to for the past 3 days and is driving me insane, but might be easy for one of you genius's!

The problem...

I am using the excellent Microkids Related Posts plugin and I am trying to make a seemingly simple change to the code and the link that is outputted to show the Parent Category Slug of a post as a class, so that I can use it as a global stable unique identifier. The foreach loop is as follows...

foreach( $related_posts as $post_type => $post_type_related_posts ) {
    # This filters %posttype% from the title
    $title = MRP_get_title( __($options['title'], 'microkids-related-posts'), $post_type );
    if( count( $post_type_related_posts ) ) {
        $output .= "<div id=\"related-posts-$post_type\" class=\"related-posts-type\">\n";
        # Create the title with the selected HTML header
        $output .= "<".$options['header_element'].">".$title."</".$options['header_element'].">\n";
        $output .= "<ul>\n";
        # Add related posts
        foreach( $post_type_related_posts as $related_post ) {
            $output .= "<li>";
            $output .= "<a href=\"".get_permalink( $related_post->ID )."\">".$related_post->post_title."</a>";
            $output .= "</li>\n";
        }
        $output .= "</ul></div>\n";
    }
    # If there are no related posts for this post type
    else {
        if( !$options['hide_if_empty'] ) {
            $output .= "<div id=\"related-posts-$post_type\" class=\"related-posts-type\">\n";
            $output .= "<".$options['header_element'].">".$title."</".$options['header_element'].">\n";
            $output .= "<p>".$options['text_if_empty']."</p>\n";
            $output .= "</div>";
        }
        else {
            # Show nothing
            return "";
        }
    }
}
$output .= "</div>";
return $output;

In the inner foreach loop is the line...

$output .= "<a href=\"".get_permalink( $related_post->ID ."\">".$related_post->post_title."</a>";

I would like to add a class in the "a" containing the Category Slug so that I can reference that later.

  1. my-domain/photo-shoot-location/knights-3/
  2. my-domain/press/knights-3/
  3. my-domain/photo-meta-data/knights-3/
  4. my-domain/about/knights-3/ etc.

I would like for it to look as follows...

$output .= "<a class=\" THE-CATEGORY-SLUG \" href=\"".get_permalink( $related_post->ID )."\">".$related_post->post_title."</a>";

With THE-CATEGORY-SLUG to be "photo-shoot-location" or "press" etc.

Any help would be greatly appreciated.

Thank you so much in advance!!!!!

0

Ok guys found the solution myself (5 days later, oof) added below to help someone else in the future...

foreach((get_the_category( $related_post->ID )) as $category) {                             
    $output .= "<a class=\"$category->slug\"  href=\"".get_permalink( $related_post->ID )."\" title=\"" . $category->cat_name . "\">" . $category->cat_name . "</a>";
}

Basically the old link above looked like this...

$output .= "<a href=\"".get_permalink( $related_post->ID )."\">".$related_post->post_title."</a>";

I took the link and added it in a foreach loop of it's own. Then I prepared the foundations by calling the get_the_category for the related_post by it's ID. That then gave me access to everything that the $category hook in WP offer, so I then simply call $category->cat_name for the category name and in turn $category->slug for the category slug name.

How cool is WP, VERY COOL!!! Anyway hope this help somebody out there.

0

Extra bonus Easter Egg for those in need...

After the foreach loop by adding...

$output .= get_post_field('post_content', $related_post->ID);

You will also get the current post content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.