add_shortcode('sa_post_cats', 'sa_show_cats');
function sa_show_cats() {
$cats = wp_get_post_categories( get_the_ID(), ['fields'=>'all',]);
$html = array();
foreach ( $cats as $cat ) {
$url = site_url("/category/{$cat->slug}/");
$html[] = "<a class=\"sa-cat-links\" href=\"$url\">{$cat->name}</a>";
}
return implode( $html, ', ');
}
Thats the code I’m working with using the short code [sa_post_cats]. Is my code correct? I checked it many times…..
The snippets plugin author advised that I:
“Try switching line 3 with this:
$cats = wp_get_post_categories( get_the_ID(), array( ‘fields’ => ‘all’ ) );”
But that didn’t solve the error 500 either. I guess I may need to hire a freelancer to take a closer look to get this to work. I’ll be sure to comment back my findings.
That exact code works perfectly on my site. (without the snippets plugin though) You should check your error logs to see if there is any clue towards the cause. Switching to array() was a good idea, but we established earlier that the [ ] syntax works. No way the author could have known that. (FYI, the [ ] syntax was introduced for more recent versions of PHP and causes a syntax error on older versions)
To eliminate possible conflicts, rename the plugins and themes folders in /wp-content/. When WP cannot find the theme and plugins in the expected locations, it will deactivate them. Create new themes and plugins folders for WP to use. Copy over the twentyseventeen theme folder and its contents to the new themes folder to give WP something to use as a theme. It’ll use it since there is nothing else in the new themes folder. Also copy over the snippets plugin folder with its contents to the new plugins folder since we need it to get you snippets working. The idea is to get WP to as close as possible to its default state.
If after all of that, you still have the error, the problem is within the snippets plugin itself. Assuming the error goes away, delete the themes and plugins folder you just created (move their contents back if you moved instead of copied). Restore the original names of themes and plugins folders. Twentyseventeen will remain your current theme and all plugins but snippets will remain deactivated.
Log into the site now that it’s working. Restore your normal theme, then your plugins, one at a time, testing after each. When the error recurs, the last activated module is at fault. Move the offending folder out of its normal place and into any backup folder.
If the problem remains with only snippets active, and removing my code resolves the error, then leave it out of snippets and implement my code as a plugin like I described in this post. I’ve updated that code so the [sa_post_cats] shortcode works.
bcworks,
The plugin author got it to work! Here is what we used:
add_shortcode( 'sa_post_cats', function () {
$cats = wp_get_post_categories( get_the_ID(), array( 'fields' => 'all' ) );
$html = array();
foreach ( $cats as $cat ) {
$html[] = sprintf(
'<a class="sa-cat-links" href="%s">%s</a>',
get_term_link( $cat, 'category' ),
$cat->name
);
}
return implode( $html, ', ' );
} );
Hope this helps someone else. Thank you for all your assistance with this!
Great news! Aye, get_term_link() provides a more robust link in some cases than my manual construction. Well done by the plugin author. It’s a special author indeed to go the extra distance to help us out with this.