我正在尝试制作我的第一个小部件/插件,插件运行良好,但是在激活之后,它会向我发送以下错误:
插件在激活过程中产生了两个意外的输出字符。如果您注意到“已发送的标题”消息、联合提要问题或其他问题,请尝试禁用或删除此插件。
然后,当我进入网站时,它会向我发送另一个错误:
“内容编码错误
您要查看的页无法显示,因为它使用无效或不受支持的压缩格式。“
在此之后,如果刷新页面,它将允许我正常进入,但是,每次我进入不同的页面时,都会重复相同的问题。
我不明白发生了什么事,所以我很感激你能帮我解决这个问题。
这是代码:
<?php
/*
Plugin Name: Mx Energy Partners - Post Relacionados
Plugin URI:
Description: Añade Post Relacionados al sitio Mx Energy Partners
Version: 1.0.0
Author: Arturo Valverde
Author URI:
Text Domain: mxtechnologypartners
*/
?>
<?php
if (!defined('ABSPATH')) die();
class Mxnrgprtnrs_related_posts extends WP_Widget
{
public function __construct()
{
$widget_ops = array(
'classname' => 'mxnrgprtnrs_posts_relacionados',
'description' => 'Añade Post Relacionados al sitio Mx Energy Partners',
);
parent::__construct(
'mxnrgprtnrs_posts_relacionados',
'MX Energy Partners - Posts Relacionados',
$widget_ops
);
}
public function widget($args, $instance)
{
if (post_type_exists('post')) : ?>
<div class="related-posts blog padding-5">
<h4>Artículos Relacionados:</h4>
<ul class="listado-blog">
<?php
$tags = get_the_tags();
if ($tags) :
$tag_ids = array();
foreach ($tags as $tag) {
$tag_ids[] = $tag->term_id;
}
$qryargs = array(
'post_type' => 'post',
'tag__in' => $tag_ids,
'post__not_in' => array(get_the_ID()),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4,
'post_status' => 'publish',
);
$my_query = new WP_Query($qryargs);
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
?>
<li class="blog-item">
<div class="img-post">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
</div>
<div class="contenido">
<div class="meta full-width">
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
<span>
Autor:
<a href="<?php echo get_author_posts_url(get_the_author_meta('ID')) ?>">
<?php echo get_the_author_meta('display_name'); ?>
</a>
</span>
<span>
<?php the_time('l d, F, Y'); ?>
</span>
</div>
</div>
</li>
<?php
endwhile;
endif;
else :
wp_reset_query();
endif;
wp_reset_query();
?>
</ul>
</div>
<?php endif; ?>
<?php
}
public function form($instance)
{
// outputs the options form on admin
}
public function update($new_instance, $old_instance)
{
// processes widget options to be saved
}
}
add_action('widgets_init', function () {
register_widget('mxnrgprtnrs_related_posts');
});
?>发布于 2022-03-19 18:56:34
删除插件信息和类声明之间的关闭和重新打开PHP标记。
?>
<?php该代码本质上是发出空白的,这是您的额外字符的来源。
https://stackoverflow.com/questions/71536289
复制相似问题