我想添加
[one_third][responsive]
<a href="http://weburl.com/lifestyle">
<img class="aligncenter size-full wp-image-176" alt="courses" src="http://weburl.com/img/buttons/courses.png" width="310" height="109" /></a>
[/responsive][/one_third]
[one_third][responsive]
<a href="http://weburl.com/answers/landing-pages/">
<img class="aligncenter size-full wp-image-178" alt="tools" src="http://weburl.com/img/buttons/tools.png" width="310" height="109" /></a>
[/responsive][/one_third]
[one_third_last][responsive]
<a href="http://weburl.com/entrepreneurship">
<img class="aligncenter size-full wp-image-179" alt="videos" src="http://weburl.com/img/buttons/videos.png" width="310" height="109" /></a>
[/responsive][/one_third_last]
在所有页面的底部,我的创业页面的父页面。
他们是不是可以将其添加到“创业之父”页面中,而不必在每个页面上粘贴到底部?
我在下面使用了这个代码,但它也显示在EnterpreUrship页面中,我希望它只显示在创业的父页面中,但我可以通过css将其隐藏在创业页面中(显示:无;)
仅供参考,我的页面ID是267,概括一下,我想在267(创业)的父页面底部插入一个代码。
如果有人能改进这段代码,那将对我帮助很大。
谢谢
<?php
$entrepreneurshipObject = get_page_by_title( \'Entrepreneurship\' );
$entrepreneurshipID = $entrepreneurshipObject->ID;
$shortcode = \'<div class="buttonsEntrepreneurship"><div class="one_third"><span class="responsive"> <a href="http://collegeisnottheanswer.com/lifestyle"> <img class="aligncenter size-full wp-image-176" alt="courses" src="http://collegeisnottheanswer.com/img/buttons/courses.png" width="310" height="109"></a> </span></div> <div class="one_third"><span class="responsive"> <a href="http://collegeisnottheanswer.com/answers/landing-pages/"> <img class="aligncenter size-full wp-image-178" alt="tools" src="http://collegeisnottheanswer.com/img/buttons/tools.png" width="310" height="109"></a> </span></div> <div class="one_third last"><span class="responsive"> <a href="http://collegeisnottheanswer.com/entrepreneurship"> <img class="aligncenter size-full wp-image-179" alt="videos" src="http://collegeisnottheanswer.com/img/buttons/videos.png" width="310" height="109"></a> </span></div></div>\';
if($post->ID == $entrepreneurshipID || $post->post_parent == $entrepreneurshipID){
echo do_shortcode($shortcode);
}
?>
最合适的回答,由SO网友:Howdy_McGee 整理而成
由于您知道页面ID,我们可以直接查看页面ID267
- 因此,让我们跳过对ID的搜索,跳转到比较:
您提到您不想在创业页面上使用它,所以让我们只查找父级ID为的页面267
if($post->post_parent == 267)
看起来您已经删除了短代码,所以让我们让它在视觉上更具吸引力,并清理代码:
<?php if($post->post_parent == 267) : ?>
<div class="buttonsEntrepreneurship">
<div class="one_third">
<span class="responsive">
<a href="http://collegeisnottheanswer.com/lifestyle">
<img class="aligncenter size-full wp-image-176" alt="courses" src="http://collegeisnottheanswer.com/img/buttons/courses.png" width="310" height="109">
</a>
</span>
</div>
<div class="one_third">
<span class="responsive">
<a href="http://collegeisnottheanswer.com/answers/landing-pages/">
<img class="aligncenter size-full wp-image-178" alt="tools" src="http://collegeisnottheanswer.com/img/buttons/tools.png" width="310" height="109">
</a>
</span>
</div>
<div class="one_third last">
<span class="responsive">
<a href="http://collegeisnottheanswer.com/entrepreneurship">
<img class="aligncenter size-full wp-image-179" alt="videos" src="http://collegeisnottheanswer.com/img/buttons/videos.png" width="310" height="109">
</a>
</span>
</div>
</div>
<?php endif; ?>
SO网友:s_ha_dum
这应该很简单,只需打开一个过滤器the_content
. 我不太清楚“我的创业页面的所有页面父页面”是什么意思,但下面将把字符串添加到所有“页面”帖子类型页面的底部,这些页面的父页面等于0,即父页面。您可以调整if
获得所需准确行为的条件。
function append_to_page_wpse($content) {
global $post;
if (
\'page\' == $post->post_type
&& 0 === $post->post_parent
) {
$content .= \'[one_third][responsive] <a href="http://weburl.com/lifestyle"> <img class="aligncenter size-full wp-image-176" alt="courses" src="http://weburl.com/img/buttons/courses.png" width="310" height="109" /></a> [/responsive][/one_third] [one_third][responsive] <a href="http://weburl.com/answers/landing-pages/"> <img class="aligncenter size-full wp-image-178" alt="tools" src="http://weburl.com/img/buttons/tools.png" width="310" height="109" /></a> [/responsive][/one_third] [one_third_last][responsive] <a href="http://weburl.com/entrepreneurship"> <img class="aligncenter size-full wp-image-179" alt="videos" src="http://weburl.com/img/buttons/videos.png" width="310" height="109" /></a> [/responsive][/one_third_last]\';
}
return $content;
}
add_filter(\'the_content\',\'append_to_page_wpse\');
这应该足够早地运行,以便短代码正常处理,而无需任何特殊操作。