Skip Navigation

[Resolved] Adding a toolset shortcode to a category content template from a theme

This support ticket is created 4 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 10 replies, has 2 voices.

Last updated by chrisC-25 4 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1503813

Hi

I want to add a logo to posts on category archive page. I can make a selectable field with Toolset, but when I go to add the shortcode in the existing template it does not recognize the shortcode.

Here is the code

<?php

// =============================================================================
// VIEWS/GLOBAL/_CONTENT-THE-EXCERPT.PHP
// -----------------------------------------------------------------------------
// Display of the_excerpt() for various entries.
// =============================================================================

?>

<?php do_action( 'x_before_the_excerpt_begin' ); ?>
<div>[types field='logo' output='raw'][/types]</div>
<div class="entry-content excerpt">

<?php do_action( 'x_after_the_excerpt_begin' ); ?>

  <?php the_excerpt(); ?>

<?php do_action( 'x_before_the_excerpt_end' ); ?>

</div>

<?php do_action( 'x_after_the_excerpt_end' ); ?>

Here is the page:
hidden link

#1503861

Hi, PHP doesn't recognize inline shortcodes like this. You have two options:
1. Use the types_render_field PHP API to display a formatted custom field value. We have documentation for this API available here: https://toolset.com/documentation/customizing-sites-using-php/functions/
Click the +More button to display code examples for each type of field.

2. Use the WP function do_shortcode to execute the shortcode in PHP. https://developer.wordpress.org/reference/functions/do_shortcode/

Option 1 is preferred for displaying custom field values. If you need assistance, please let me know if it's a post field or a term field, and let me know the slug. I'll give you some additional examples.

#1504013

Thanks Christian, I may go another route since I only need that logo/image to show for a certain category. I tried adding this code but did not work, any insight?

<?php

// =============================================================================
// VIEWS/GLOBAL/_CONTENT-THE-EXCERPT.PHP
// -----------------------------------------------------------------------------
// Display of the_excerpt() for various entries.
// =============================================================================

?>

<?php do_action( 'x_before_the_excerpt_begin' ); ?>

<div>
	<?php
    if ( is_category( 'news-pulse' ) ) {
        echo '<img src="/wp-content/uploads/2020/02/PMG-Logoblack-ntsq-120.png" width="60">';
    }

?>
</div>


<div class="entry-content excerpt">

<?php do_action( 'x_after_the_excerpt_begin' ); ?>

  <?php the_excerpt(); ?>

<?php do_action( 'x_before_the_excerpt_end' ); ?>

</div>

<?php do_action( 'x_after_the_excerpt_end' ); ?>
#1504021

The WordPress function is_category is designed to work with the Category taxonomy. Is news-pulse the slug of a category or some other taxonomy?

For custom taxonomies use is_tax:
https://developer.wordpress.org/reference/functions/is_tax/

#1504025

Yes it is the slug of a category and I just want to pop the logo in on those category posts only so you see the logo on posts at the archive here:
hidden link

That first post is checked as a news-pulse item.

#1504031

The code you have in place is_category is only true in the actual news-pulse archive:
hidden link

If you want to add the logo to a post in some other term archive when the post also has the news-pulse category, that's not what is_category is meant to accomplish.

#1504039

Ahhh okay, sorry my php is not great. Also, news-pulse is a child of news-music-publishing.

How can I accomplish this? I also tried this:

<?php

// =============================================================================
// VIEWS/GLOBAL/_CONTENT-THE-EXCERPT.PHP
// -----------------------------------------------------------------------------
// Display of the_excerpt() for various entries.
// =============================================================================

?>

<?php do_action( 'x_before_the_excerpt_begin' ); ?>

<div>
<?php
$categories = get_the_category();
foreach($categories as $category) 
{
    if($category->cat_name == "news-pulse")
    {
        echo '<img src="/wp-content/uploads/2020/02/PMG-Logoblack-ntsq-120.png" width="60">';
    }
    
}
?>
</div>


<div class="entry-content excerpt">

<?php do_action( 'x_after_the_excerpt_begin' ); ?>

  <?php the_excerpt(); ?>

<?php do_action( 'x_before_the_excerpt_end' ); ?>

</div>

<?php do_action( 'x_after_the_excerpt_end' ); ?>




#1504041

Maybe use the has_term function? Not sure, this custom code isn't really related to Toolset.
https://developer.wordpress.org/reference/functions/has_term/

#1504063

Understood, thanks for some direction.

#1504085

Just a follow up - here is what worked:

<?php

// =============================================================================
// VIEWS/GLOBAL/_CONTENT-THE-EXCERPT.PHP
// -----------------------------------------------------------------------------
// Display of the_excerpt() for various entries.
// =============================================================================

?>

<?php do_action( 'x_before_the_excerpt_begin' ); ?>

<div>
	<?php
    if ( has_category('news-pulse' ) ) {
        echo '<img src="/wp-content/uploads/2020/02/PMG-Logoblack-ntsq-120.png" width="60">';
    }

?>
</div>


<div class="entry-content excerpt">

<?php do_action( 'x_after_the_excerpt_begin' ); ?>

  <?php the_excerpt(); ?>

<?php do_action( 'x_before_the_excerpt_end' ); ?>

</div>

<?php do_action( 'x_after_the_excerpt_end' ); ?>
#1504087

My issue is resolved now. Thank you!