Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1301758
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI have a line of code in my template I would like to add the css class "has-video" to if the post is within a certain category.
I am trying to do this inline with ' . ( if (in_category(42)) echo 'has-video') ? '
but I get a syntax error, unexpected 'if' (T_IF)
I'm not great with my PHP and know this is close but not great :/
Here is the whole echo code:
echo '<section id="cooked-recipe-list-' . $list_id_counter . '" class="cooked-clearfix cooked-recipe-' . $list_style . ' cooked-recipe-loader' . ( in_array( $list_style, $masonry_layouts ) ? ' cooked-masonry' : '' ) . ( isset($atts['columns']) && $atts['columns'] ? ' cooked-columns-' . $atts['columns'] : '' ) . ' ' . ( if (in_category(42)) echo 'has-video') ? '">';
Closed. This question is off-topic. It is not currently accepting answers.
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI have a line of code in my template I would like to add the css class "has-video" to if the post is within a certain category.
I am trying to do this inline with ' . ( if (in_category(42)) echo 'has-video') ? '
but I get a syntax error, unexpected 'if' (T_IF)
I'm not great with my PHP and know this is close but not great :/
Here is the whole echo code:
echo '<section id="cooked-recipe-list-' . $list_id_counter . '" class="cooked-clearfix cooked-recipe-' . $list_style . ' cooked-recipe-loader' . ( in_array( $list_style, $masonry_layouts ) ? ' cooked-masonry' : '' ) . ( isset($atts['columns']) && $atts['columns'] ? ' cooked-columns-' . $atts['columns'] : '' ) . ' ' . ( if (in_category(42)) echo 'has-video') ? '">';
Share
Improve this question
asked Mar 22, 2021 at 1:52
Marney FontanaMarney Fontana
31 bronze badge
2 Answers
Reset to default 1It may helpful to you...
$video = (in_category ( 42 )) ? 'has-video' : '';
echo '<section id="cooked-recipe-list-' . $list_id_counter . '" class="cooked-clearfix cooked-recipe-' . $list_style . ' cooked-recipe-loader' . ( in_array( $list_style, $masonry_layouts ) ? ' cooked-masonry' : '' ) . ( isset($atts['columns']) && $atts['columns'] ? ' cooked-columns-' . $atts['columns'] : '' ) . ' ' . $video. '">';
Try this and let me know..
The concrete problem here is that you are trying to nest echo
statements. That is not valid PHP.
But the underlying problem is something you can see too often in WordPress templates: overly complex strings at the cost of readability. Here is an alternative style to get your output that is much easier to read. And you can use that everywhere in your templates.
First set up your variables, then create the string. This way your code is easier to understand and therefore to change. Also use esc_attr()
when you are using variables from other sources in your code.
$id = esc_attr( 'cooked-recipe-list-' . $list_id_counter );
// Basic classes
$classes = [
'cooked-clearfix',
'cooked-recipe-' . $list_style,
'cooked-recipe-loader'
];
// Now extend the classes, step by step
if ( in_array( $list_style, $masonry_layouts ) )
$classes[] = 'cooked-masonry';
// empty() includes an "isset" check
if ( ! empty ( $atts['columns'] ) )
$classes[] = 'cooked-columns-' . $atts['columns'];
if ( in_category(42) )
$classes[] = 'has-video';
// convert classes to a string
$classes = implode( ' ', $classes );
// Make sure no nasty code is in there
$classes = esc_attr( $classes );
// And finally print a simple string
print "<section id='$id' class='$classes'>";
Yes, it's a bit longer. But you will never have to search for something when you want to make a change, and you have comments explaining everything to a later you.
本文标签: categoriesInline If statement to echo CSS
版权声明:本文标题:categories - Inline If statement to echo CSS 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741663057a2391151.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论