I am trying to create a shortcode that can be used to display the project attributes (Categories) of a portfolio item on the portfolio item page.

I am using salient and they only allow for the attributes in the sidebar.

I have successfully moved the portfolio categories around the page by moving this code snippet around the single-portfolio.php file. However, this is not giving me enough control as it posts the categories as a full width-item.

   <?php the_content(); ?>


                <?php
                        $project_attrs = get_the_terms( $post->ID, 'project-attributes' );
                         if (!empty($project_attrs)){ ?>
                            <ul class="project-attrs checks">
                                <?php
                                foreach($project_attrs as $attr){
                                    echo '<li>' . $attr->name . '</li>';
                                }
                                ?>
                            </ul>
                        <?php } ?>

I am now trying to create a shortcode that I can add that will post these categories.

Here is the shortcode that I am trying to create based on the previous code snippet...

<?php

function portfolio_categories() {
  $post = get_post( $id );
  $project_attrs = get_the_terms( $post->ID, 'project-attributes' );
  if (!empty($project_attrs)){ ?>
    <ul class="project-attrs checks">
      <?php
  foreach($project_attrs as $attr){
    echo '<li>' . $attr->name . '</li>';
  }
</ul>

}

add_shortcode( 'portfolio-categories', 'portfolio_categories');

?>

Obviously this is not working... I would like to learn how to properly write this code so any tips on how to set up my shortcode to pull the attributes would be much appreciated. I have looked through the shortcode API. At this point I am not sure how to mix the php required to grab the attributes and the html to output through the shortcode.

Thanks!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.