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
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.
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' ); ?>
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/
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.
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.
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' ); ?>
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/
Understood, thanks for some direction.
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' ); ?>
My issue is resolved now. Thank you!