Shortcode to show list of project-related categories/tags on Project Page itself
-
Hello
I'm working on the "info section" of my project page template. Now I was wondering if there is already a built-in solution to show a list of categories or tags related to the project itself.
Ideally i'm thinking of some shortcode with which all subcategories of a group, which are related to the project itself are shown/listed.
Example:
Main Category: Clients
Sub-Categories: Client1, Client2, Client3....-> The shortcode [project_page_subcats_client] will generate a list of all "Client" Sub-Categories, which are selected for this project.
I installed the "Shortcoder" plugin, but didn't manage to get done yet. My issue is I don't really know where to start looking. Some Java coding probably? Would be great if somebody can give me some first pointers!
The project-page I'm talking about: https://schneiderbrett.com/hotel-hasle-linie/
Kind regards,
Amadeus -
Hello again
I tryed different things and here my solution so far:
Basically the best info I found was after searching the web for something like "display tags on post page wordpress".
What I found was that some changes have to be made in the "function.php" file which can be found in WP->Appearance->Theme File Editor.
The changes where some code (probably java?) Here an example, which worked for me (after some modifications). What I did here was generate a list of "sub-categories" of a certain "main-category" to be shown on a project page itself.
function getWorkCatList() { global $post; $args = array('parent' => 1); $categories = get_categories( $args ); $categoryOutput = []; if (!empty($categories)) { array_push($categoryOutput, '<ul class="_Default">'); foreach($categories as $category) { array_push($categoryOutput, '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </br> '); } array_push($categoryOutput, '</ul>'); } return implode('', $categoryOutput); } add_shortcode('work_cats_pg', 'getWorkCatList');
Explaining:
$args = array('parent' => 1); <== Here manually input the id of the "main-category"array_push($categoryOutput, '<ul class="_Default">'); <== Here manually indicate which Text Format to use
array_push($categoryOutput, '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . <== Here generating a clickable link to the sub-category page itself.
add_shortcode('work_cats_pg', 'getWorkCatList'); <== Finally defining the Shortcode. The list of project-related categorys can be inserted on any page by using following shortcode: [work_cats_pg]
The aim of all this? Less repetitive work with every new project which is posted. And projects basically show up anywhere where the user clicks a link. ;P
If anybody sees a better solution to all this feel free to post it. I just arrived here through trial and error - probably there are more efficient solutions out there.
One last question: Are the changes made in the "functions.php" file permanent? Hope to not crash my site with the next update.
Best regards,
Amadeus -
Hi there
A little problem came up, which I can't seem to solve.
Thanks to the code I'll post below I can get all the active sub-categories filtered by main category. What is missing is that the sub-categories are filtered according to current post. (Not only show globaly active sub-categories but show all active sub-categories related to current post).
function getProjSkillList() { global $post; $args = array('parent' => 193); $categories = get_categories( $args ); $categoryOutput = []; if (!empty($categories)) { array_push($categoryOutput, '<ul class="_Default">'); foreach($categories as $category) { array_push($categoryOutput, '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </br> '); } array_push($categoryOutput, '</ul>'); } return implode('', $categoryOutput); } add_shortcode('project_skills_pg', 'getProjSkillList');
By changing the number "193" I can control by which main category the sub-categories should be filtered.
The shortcode [project_skills_pg] can be placed on page to show list of sub-categories.
What changes to make, so that only sub-categories related to the current post are showing?
Link to a page: https://schneiderbrett.com/fr/hotel-hasle-linie/
Happy about any kind of hint/feedback!
Best,
Amadeus -
And last question resolved again. Here the code:
function createPostListSubCatWork() { global $post; $categories = wp_get_object_terms( $post->ID, 'category', [ 'parent' => 1, 'number' => 99 ] ); $categoryOutput = []; if (!empty($categories)) { array_push($categoryOutput, '<ul class="_Default">'); foreach($categories as $category) { array_push($categoryOutput, '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </br> '); } array_push($categoryOutput, '</ul>'); } return implode('', $categoryOutput); } add_shortcode('work_cats_pg', 'createPostListSubCatWork');
- List categories for the current post: [work_cats_pg]
- The categories should be children of category 1.
- No more than 99 positions should be displayed.
Source which kinde resolved it for me: https://wordpress.stackexchange.com/questions/330128/display-a-list-of-subcategories-from-specific-category-a-post-belongs-to
Best regards,
Amadezs
Before you post:
- When using a WordPress Cache plugin, disable it or clear your cache.
- Update Lay Theme and all Lay Theme Addons
- Disable all Plugins
- Go to Lay Options → Custom CSS & HTML, click "Turn Off All Custom Code", click "Save Changes"
This often solves issues you might run into
When you post:
- Post a link to where the problem is
- Does the problem happen on Chrome, Firefox, Safari or iPhone or Android?
- If the problem is difficult to explain, post screenshots / link to a video to explain it