将文本添加到特定页面的底部

时间:2014-03-19 作者:ZaX

我想添加

[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);
            }
            ?>

2 个回复
最合适的回答,由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\');
这应该足够早地运行,以便短代码正常处理,而无需任何特殊操作。

结束

相关推荐

在多站点设置中,不同子站点的‘WP_CONTENT_URL’是否不同?

我在我的网络(WordPress Multisite)上托管了相当多的博客,并通过一个公共CDN提供其静态内容,只需在wp config中设置wp content directory URL即可。php类似:# http://codex.wordpress.org/Editing_wp-config.php#Moving_wp-content_folder define( \'WP_CONTENT_URL\', \'http://abcdefghijk.cloudfront.net/wp-cont