How would I add a shortcode at the bottom of every category & tag page in the functions.php?

I want to add Social Warfare shortcode after the category/tag description. My PHP is rusty and don’t remember how to write the code or what WP hook to use to make global changes to archive pages.

I manually added it to this tag page so that you can see what I’m trying to accomplish:

Hello @gmcniel,

There is no action/filter available to add some content below the tags of the post.

However, you can do it in a different way by overriding the _content-post-footer.php in your child theme:

<?php

// =============================================================================
// VIEWS/INTEGRITY/_CONTENT-POST-FOOTER.PHP
// -----------------------------------------------------------------------------
// Standard <footer> output for various posts.
// =============================================================================

?>

<?php if ( has_tag() ) : ?>
  <footer class="entry-footer cf">
    <?php echo get_the_tag_list(); ?>
  </footer>
  <!-- Add the do_shortcode function here-->
<?php endif; ?>

Place that file in x-child/framework/views/integrity.

Hope this helps.

I think I didn’t make it clear what I’m trying to accomplish. I’m referring to the archive pages such as category and tag pages themselves. Not adding content below the tags of a posts.

I want to put the [social_warfare] shortcode at the end of a category or tag description as seen here:

Hello @gmcniel,

Thank you for the clarification. You will need to create a custom function PHP code and hook it to the x_after_view_global__index view. Your code can be like this:

// Add a custom shortcode in the tag or category archive
// =============================================================================
function custom_shortcode() { 

  if( is_archive() && (is_tag() || is_category())  ) : ?>
 
    <div class="x-container custom-shortcode" style="clear: both;">
      <?php echo do_shortcode('[social_warfare]'); ?>
    </div>
 
  <?php endif; 
}
add_action('x_after_view_global__index','custom_shortcode');
// =============================================================================

Please note that the code provided above only serves as a guide and to help you in getting started with your modifications. Implementing and maintaining the code will be out of our support scope and for further maintenance for new possible versions of the theme that may cause the customization to break, you will need to hire a developer.

To learn more about the different actions hooks and filter that you can use for your modifications, please check this documentation:

We would love to know if this has worked for you. And thank you for your understanding.

That is pretty close. But it puts the social warfare share bar below the posts. I want to have it up in the description area instead.

So, instead of this (where it’s at the bottom above the footer):

More like this:

It’s worth pointing out that we already have code in our functions.php to Show Category Description. Is it possible to combine? Here’s are the relevant lines from our functions.php file:

/* Show Category Description */
function add_category_description() {
    if(is_archive()) {  ?>
    <div class="cat-desc">
        <div class="x-container max width"</div>
            <?php echo  category_description(); ?>
        </div>
    </div>
    <?php
    }
}
add_action( 'x_after_view_integrity__landmark-header', 'add_category_description',99999 );

// Add a custom shortcode in the tag or category archive
// =============================================================================
function custom_shortcode() { 

  if( is_archive() && (is_tag() || is_category())  ) : ?>
 
    <div class="x-container custom-shortcode" style="clear: both;">
      <?php echo do_shortcode('[social_warfare]'); ?>
    </div>
 
  <?php endif; 
}
add_action('x_after_view_global__index','custom_shortcode');
// =============================================================================

I pulled out that piece and put it in the Show Category Description part and removed the other function. It seems to work okay that way:

I have no idea if the share counts are accurate, but that’s a Social Warfare thing. And I’m happy to see share numbers show up at all for these category & tag pages.

Let me know if what I’ve got is good to go as is, or if it needs modification:

/* Show Category Description */
function add_category_description() {
    if(is_archive()) {  ?>
    <div class="cat-desc">
        <div class="x-container max width"</div>
            <?php echo  category_description(); ?>
			<?php echo do_shortcode('[social_warfare]'); ?>
        </div>
    </div>
    <?php
    }
}
add_action( 'x_after_view_integrity__landmark-header', 'add_category_description',99999 );

Oh I see the problem. It’s showing the share counts of the most recent post. And when I hit the Facebook share button for example, it grabs the URL from the most recent post in the loop.

So, I/we will need to find another way to get that shortcode up there, without messing up the URL being shared. I want for people to be able to share category & tag pages.

Hi @gmcniel,

Please kindly consider that your request is a customization one and outside of our support scope. We do not know what is the problem with the sharing plugin and URLs that you have mentioned and unfortunately, we can not be of help regarding that.

But if you want to have things above the view instead of after the view, you can change the code my colleague mentioned into:

// Add a custom shortcode in the tag or category archive
// =============================================================================
function custom_shortcode() { 

 if( is_archive() && (is_tag() || is_category())  ) : ?>

   <div class="x-container custom-shortcode" style="clear: both;">
     <?php echo do_shortcode('[social_warfare]'); ?>
   </div>

 <?php endif; 
}
add_action('x_before_view_global__index','custom_shortcode');
// =============================================================================

Thank you for your understanding.

Thank you. I’ll take up the share bar glitch with Social Warfare. Thanks for helping me get the code placed properly.

We are delighted to assist you with this.

Cheers!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.