admin管理员组文章数量:1426506
I hope you can help.
I have two pieces of code that I do not know how to pair. I want my widget to only appear on posts and furthermore only if one specific category is picked. Whenever I try to put the condition in front of the public function it informs of a php error on the line of the condtion.
Here is my conditional code:
if (is_single() && in_category('pladeanmeldelser'))
Here is my display widget code:
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$selskab = get_field( "selskab" );
$related = get_field( "related" );
$udgivet = get_field( "udgivet" );
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title']; } ?>
<?php global $post;
if ( has_post_thumbnail( $post->ID ) )
echo get_the_post_thumbnail( $post->ID, 'medium' );
?>
<div class="textwidget">
<?php if(isset($selskab)) { echo "<p><strong>Pladeselskab:</strong> $selskab </p>"; } ?>
<?php if(isset($related)) { echo "<p><strong>Musikalske slægtninge:</strong> $related </p>"; } ?>
<?php if(isset($udgivet)) { echo "<p><strong>Udgivelsesår:</strong> $udgivet </p>"; } ?>
</div>
<?php echo $args['after_widget'];
}
}
I hope you can help.
I have two pieces of code that I do not know how to pair. I want my widget to only appear on posts and furthermore only if one specific category is picked. Whenever I try to put the condition in front of the public function it informs of a php error on the line of the condtion.
Here is my conditional code:
if (is_single() && in_category('pladeanmeldelser'))
Here is my display widget code:
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$selskab = get_field( "selskab" );
$related = get_field( "related" );
$udgivet = get_field( "udgivet" );
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title']; } ?>
<?php global $post;
if ( has_post_thumbnail( $post->ID ) )
echo get_the_post_thumbnail( $post->ID, 'medium' );
?>
<div class="textwidget">
<?php if(isset($selskab)) { echo "<p><strong>Pladeselskab:</strong> $selskab </p>"; } ?>
<?php if(isset($related)) { echo "<p><strong>Musikalske slægtninge:</strong> $related </p>"; } ?>
<?php if(isset($udgivet)) { echo "<p><strong>Udgivelsesår:</strong> $udgivet </p>"; } ?>
</div>
<?php echo $args['after_widget'];
}
}
Share
Improve this question
asked May 25, 2019 at 12:21
AsbjoedtAsbjoedt
53 bronze badges
1 Answer
Reset to default 1You should put the conditional at the start of the widget()
function:
public function widget( $args, $instance ) {
// Not single post or not in the pladeanmeldelser category.
if ( ! is_single() || ! in_category('pladeanmeldelser') ) {
return;
}
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
// ... other code.
}
And that should work.
本文标签: conditional contentHow to make widget appear conditionally
版权声明:本文标题:conditional content - How to make widget appear conditionally 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745465137a2659500.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论