如果(is_page(** PAGE ID **))不起作用
-
-
您仔细检查了您是否在ID 346页面上,对吗?You did double-check that your are on the page with ID 346, right?
- 1
- 2014-03-19
- kraftner
-
如果这是另一个内容类型,请尝试使用"if(get_the_ID()==346)".If this is another contenttype, try using `if ( get_the_ID() == 346 )`.
- 3
- 2014-03-19
- fischi
-
是的kraftner.我更改了节拍并开始使用对我有用的[WP内容实验和事件跟踪](http://wordpress.org/plugins/wp-content-experiments-event-tracking/).Yes kraftner. I changed tact and started using [WP Content Experiments & Event Tracking](http://wordpress.org/plugins/wp-content-experiments-event-tracking/), which works for me.
- 0
- 2014-03-19
- Steve
-
8 个回答
- 投票数
-
- 2017-01-06
您可以将其用于
<?php global $post; if( $post->ID == 346) { ?> <!-- do your stuff here --> <?php } ?>
您可以在标头中的任何位置或任何其他位置使用此功能.
you can use this for
<?php global $post; if( $post->ID == 346) { ?> <!-- do your stuff here --> <?php } ?>
you can use this anywhere either in header or anywhere else.
-
如果我想在<!-做东西->中添加PHP函数怎么办?我是否只使用` ID==346){ <!-在这里做你的东西-> }?>`What if I want to add a PHP function in ? Do I just use `ID == 346) { } ?>`
- 0
- 2018-11-16
- Telarian
-
是的,您可以在<!-中调用函数->Yes you can call your function in
- 0
- 2018-11-17
- Waqas Shakeel
-
嗯不为我工作.我想我要发布一个帖子.Hmm. Not working for me. I suppose I'll make a post.
- 0
- 2018-11-19
- Telarian
-
- 2014-03-19
一个更简单的解决方案是将
title
或slug
作为参数传递给is_page()
.如果您在另一台服务器上复制该页面,则不会有任何问题.<?php if (is_page( 'Page Title' ) ): # Do your stuff endif; ?>
A simpler solution will be to pass the
title
or theslug
as argument inis_page()
. You won't have issues if you duplicate that page on another server.<?php if (is_page( 'Page Title' ) ): # Do your stuff endif; ?>
-
使用the是最好的解决方案Using the slug is the best solution
- 1
- 2018-08-10
- Rob
-
如果管理员将来决定更改帖子的内容,是否会打破这种情况?If the admin decides to change the slug of the post in the future, would that break this condition?
- 0
- 2020-04-15
- Viktor Borítás
-
@ViktorBorítás是的.如果通常在开发过程中使用WordPress导入/导出功能,则不能保证所有服务器上的页面ID都相同.如果您每次都部署整个数据库,那么您将获得相同的页面ID.另外,您可以使用Pagetitle或Slug.@ViktorBorítás Yes it will. If you usually use the WordPress Import/Export features during development you're not guaranteed to have the same page ID on all your servers. If you deploy the whole database each time, then you'll get the same page ID. Else you can use Page title or slug.
- 1
- 2020-04-21
- RRikesh
-
@RRikesh是正确的,但是从长远来看,我认为引用页面ID仍然是最安全的策略(尤其是如果WP的高级本机内部重定向已被Devs覆盖),则在可能的段名/标题/名称下破坏尽可能少的内容更改.这很容易发生.;)我猜/希望在大多数情况下,Dev通常会镜像整个数据库,因此页面ID保持不变.@RRikesh right, however in my opinion referring to page ID is still the safest strategy on the long run (especially if WP's fancy native internal redirection got overwritten by Devs), to break as few things as possible at a possible slug/Title/name change. That can happen just too easily. ;) I guess/hope in most cases Devs usually mirror the whole DB, so page ID-s stay the same.
- 1
- 2020-04-28
- Viktor Borítás
-
- 2018-08-04
诸如
init
之类的钩子根本无法工作.您必须至少钩在
parse_query
上.一切都将起作用:
is_page(198); # ID (int) is_page('198'); # ID (string) is_page('Some Title'); # Title, case-sensitive is_page('some-title'); # Slug
但是它必须至少钩在
parse_query
或之后的任何其他钩子中.您可以在此处查看WordPress挂钩顺序: https://codex.wordpress.org/Plugin_API/Action_ReferenceHooks such as
init
will not work at all.You have to hook at least on
parse_query
.Everything bellow will work:
is_page(198); # ID (int) is_page('198'); # ID (string) is_page('Some Title'); # Title, case-sensitive is_page('some-title'); # Slug
But it must be hooked at least in
parse_query
or any other hook after it. You can see WordPress hook order here: https://codex.wordpress.org/Plugin_API/Action_Reference -
-
- 2019-10-05
首先,您必须了解 页面 和 帖子 之间的区别.完成此操作后,您可以选择使用 is_page 还是 is_single .
如果要处理WordPress页面,请在下面以这种方式编写.请注意,此示例使用数组以防万一,如果您想在许多页面中实现它:
<?php if (is_page( array( 1, 529, 'or post title' ) ) ) : ?> <!-- Do nothing --> <?php else : ?> <!-- Insert your code here --> <?php endif; ?>
但是,如果您希望它也对您的帖子生效,那么也可以添加以下行:
<?php if (is_single( array( 1, 529, 'or post title' ) ) ) : ?> <!-- Do nothing --> <?php else : ?> <!-- Insert your code here --> <?php endif; ?>
First you have to know the difference between a page and post. Once you have done that then you can choose whether to use is_page or is_single.
If you are dealing with WordPress pages, then write in this way below. Note, this example is using array just in case if you want to implement it in many pages:
<?php if (is_page( array( 1, 529, 'or post title' ) ) ) : ?> <!-- Do nothing --> <?php else : ?> <!-- Insert your code here --> <?php endif; ?>
But if you need it to take effect also on your posts, then add this lines too:
<?php if (is_single( array( 1, 529, 'or post title' ) ) ) : ?> <!-- Do nothing --> <?php else : ?> <!-- Insert your code here --> <?php endif; ?>
-
- 2016-08-29
请尝试从ID号&amp;中删除
''
(单引号)会起作用:is_page(34)
Please try to remove
''
(single quotes) from ID number & it will work:is_page(34)
-
这个答案需要更多解释This answer needs some more explanation
- 2
- 2016-08-29
- cjbj
-
- 2020-01-28
对于单个帖子使用
if ( is_single( '1346' ) )
对于单页使用
if ( is_page( '1346' ) )
'1346'
是您的帖子或页面ID. -
- 2020-01-28
function test_run(){ if (is_page( 'Page Title' ) ): //you can use is_page(int post id/slug/title) # Do your stuff endif; } add_action('parse_query', 'test_run');
完成@Lucas Bustamante的答案
function test_run(){ if (is_page( 'Page Title' ) ): //you can use is_page(int post id/slug/title) # Do your stuff endif; } add_action('parse_query', 'test_run');
completing @Lucas Bustamante 's answer
我正在关注本教程有关将Google Content Experiments代码添加到
header.php
.我在
header.php
中添加了以下代码:这没有在前端产生内容实验代码.我尝试过:
这也不起作用.
您能看到为什么此代码不起作用吗?谢谢.