admin管理员组

文章数量:1301758

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 question

I 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 question

I 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
Add a comment  | 

2 Answers 2

Reset to default 1

It 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