wordpress代码
I am always wondering how to display different Widgets in WordPress depending on the page/post/category etc. I ever tried some plugins that is for specific purpose. However, they are not flexible enough for all my needs. For example, I want a widget that contains AdSense ad not display on certain posts that I selected and marked by the post meta, which is not easy to control with some plugins that only support limited integrated controlling options. I found a plugin “Widget Logic” that controls whether a widget will be displayed by evaluating a piece of PHP code. I soon find it is very powerful and like it very much.
我一直想知道如何根据页面/帖子/类别等在WordPress中显示不同的小部件。我曾经尝试过一些专门用于特定目的的插件。 但是,它们不够灵活,无法满足我的所有需求。 例如,我想要一个包含AdSense广告的窗口小部件不显示在我选择并由帖子元标记的某些帖子上,而使用某些仅支持有限的集成控制选项的插件很难控制它。 我找到了一个插件 “ Widget Logic”,该插件通过评估一段PHP代码来控制是否显示小部件。 我很快发现它非常强大并且非常喜欢它。
Widget Logic插件 (The Widget Logic plugin)
Please refer to the site of Widget Logic for how to install and use it. There is detailed documents there: widget logic plugin.
请参阅Widget Logic的站点以了解如何安装和使用它。 那里有详细的文档: 小部件逻辑插件 。
WordPress条件标签 (WordPress Conditional Tags)
WordPress provides some Conditional Tags which you may find very useful when deciding a piece of PHP code to control a widget: Conditional Tags.
WordPress提供了一些条件标签,当您决定一段PHP代码来控制小部件时,它们可能会非常有用: 条件标签 。
一个复杂的例子 (A Complex Example)
Now, let’s solve the problem I list at the beginning of the post and make it more complex: Only display the widget when a. It is a post (e.g., not display it on homepage, category, page, or tag pages), b. It is not displaying the old comment pages (since when readers are reading the comments, they will not see the widget at all which stay on the top of the web page), c. It has no “noad” post meta set (so I can make some posts not display the ad in the widget by setting a “noad” post meta to it). The Widget logic code to control this wiget is:
现在,让我们解决我在文章开头列出的问题并使它变得更复杂:仅在a时显示小部件。 它是帖子(例如,不在首页,类别,页面或标签页上显示),b。 它没有显示旧的注释页面(由于读者在阅读注释时,他们根本看不到停留在网页顶部的小部件),c。 它没有“ noad”帖子元集(因此,我可以通过将其设置为“ noad”帖子元来使某些帖子在小部件中不显示广告)。 控制此小工具的Widget逻辑代码为:
is_single() &&
!get_post_custom_values("noad", get_the_ID()) &&
!(get_comment_pages_count() > 1 &&
get_query_var('cpage') != ('newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1))
翻译自: https://www.systutorials.com/controlling-display-of-wordpress-widget-with-php-code/
wordpress代码