the_content过滤器中的Wordpress多页变量

时间:2019-07-12 18:39:03

标签: wordpress

我试图在我的Wordpress functions.php文件中添加一个过滤器,但是我遇到了$multipage全局变量的问题。

我假设尚未在the_content中定义该变量,因为我正在尝试向the_content添加过滤器,而$multipage变量似乎为空。

function the_content_filter( $content ) {
    global $multipage;
    if ( $multipage ) {
        return 'multipage is true';
    } else {
        return 'multipage is false';
    }
}
add_filter( 'the_content', 'the_content_filter' );

总是返回“多页为假”。看来$ multipage是一个空字符串。多页和单页帖子都是这种情况。

如果无法使用全局$ multipage变量,如何确定帖子在the_content过滤器中是单页还是多页?

1 个答案:

答案 0 :(得分:1)

class Tour(models.Model): class Meta: verbose_name = "тур" verbose_name_plural = "туры" REGION_CHOICE = ( ('e', 'Европа'), ('b', 'Беларусь') ) COLOR_CHOICE = ( ('y', 'Жёлтый'), ('g', 'Зелёный'), ('b', 'Голубой'), ('r', 'Красный') ) color = models.CharField(max_length=1, default='b', choices=COLOR_CHOICE, verbose_name="Цвет страницы") region = models.CharField(max_length=1, default='b', choices=REGION_CHOICE, verbose_name="Регион") name = models.CharField(max_length=128, verbose_name="Внутреннее название") image = models.ImageField(upload_to="tours/static/img/", verbose_name="Основное изображение") duration = models.PositiveIntegerField(verbose_name="Длительность (в днях)") seat = models.PositiveIntegerField(verbose_name="Количество мест") cost = models.PositiveIntegerField(verbose_name="Стоимость тура в BYN") images = models.ManyToManyField('TourImage', verbose_name="Фотографии с туров") def __str__(self): return self.name class TourArrival(models.Model): class Meta: verbose_name = "заезд" verbose_name_plural = "заезды" ordering = ['date'] tour = models.ForeignKey(Tour, on_delete=models.CASCADE, verbose_name="Тур") date = models.DateField(default=timezone.now, verbose_name="Дата заезда") def __str__(self): return formats.date_format(self.date, 'DATE_FORMAT') class TourTranslation(models.Model): class Meta: verbose_name = "локализация для тура" verbose_name_plural = "локализации для туров" tour = models.ForeignKey(Tour, on_delete=models.CASCADE, verbose_name="Тур") language = models.CharField(max_length=7, verbose_name="Язык", choices=LANGUAGES) name = models.CharField(max_length=128, verbose_name="Название") sentence = models.CharField(max_length=128, verbose_name="Короткое предложение", help_text="Отображается под заголовком") description = RichTextField(max_length=65536, verbose_name="Описание тура") route = RichTextField(max_length=65536, verbose_name="Описание маршрута") def __str__(self): return self.name 不能那样工作。它不会add_filter淘汰您的内容。您需要echo的值。另外,return是布尔值,表示它仅返回TRUE或FALSE。

您可以尝试以下方法:

$multipage

如果function the_content_filter( $content ) { global $multipage; /* This will tell you if it's a multipage. It will display int(0) if it's not or int(1) if it is. Remove this line when you are done testing. */ var_dump( $multipage ); if ( $multipage ) { return 'multipage is true'; } else { return $content; } } add_filter( 'the_content', 'the_content_filter' ); 为true,它将显示multipage is true而不是内容,否则将显示原始内容。

这里是$multipage过滤器上的文档:https://developer.wordpress.org/reference/hooks/the_content/