dpowt82802
2016-02-12 18:01
浏览 30
已采纳

如果($ image)和is_page_template

I'd like to take an existing function and alter it's output depending on the page template used (WordPress). The below looks to see if an image is present, then outputs it with the text first then the image after, so the image is on the right hand side visually. This behaves the same on all templates.

I've done some research and tried a few things but can't seem to crack it. What I need is something that says if the page template is 'template_left.php' output the cta-image before the content. Likewise, if the page template is 'template_right.php' output the cta-image after the contnent.

All help and advice to edit the below gratefully received.

<?php
function call_to_action_shortcode($attrs, $content = null) {
  $image = reset(rwmb_meta( "avenir_cta_image", 'type=image&size=large' ));

  if ($image) {
    $styles = "<style>
      .cta-image {
        background-image: url('{$image['url']}');
      }
    </style>";
    $output = '<div class="page-cta row">' .
      '<div class="content">' .
        $content .
      '</div>' .
      '<div class="cta-image pull-right"></div>' .
    '</div>';

    return $styles . $output;
  }

}
add_shortcode('call-to-action', 'call_to_action_shortcode');

图片转代码服务由CSDN问答提供 功能建议

我想取一个现有的函数并根据使用的页面模板(WordPress)改变它的输出。 下面查看图像是否存在,然后先输出文本,然后输出图像,这样图像就在右侧可视化。 这在所有模板上表现相同。

我做了一些研究并尝试了一些事情,但似乎无法破解它。 我需要的是,如果页面模板是'template_left.php',则在内容之前输出cta-image。 同样,如果页面模板是'template_right.php',则在对象之后输出cta-image。

编辑下面的所有帮助和建议感激不尽。

 &lt;?php 
function call_to_action_shortcode($ attrs,$ content = null){
 $ image = reset(rwmb_meta(“avenir_cta_image”,'type = image&amp; size  = large')); 
 
 if($ image){
 $ styles =“&lt; style&gt; 
 .cta-image {
 background-image:url('{$ image ['url']  }'); 
} 
&lt; / style&gt;“; 
 $ output ='&lt; div class =”page-cta row“&gt;'  。
'&lt; div class =“content”&gt;'  。
 $ content。
'&lt; / div&gt;'  。
'&lt; div class =“cta-image pull-right”&gt;&lt; / div&gt;'  。
'&lt; / div&gt;'; 
 
返回$ styles。  $ output; 
} 
 
} 
add_shortcode('call-to-action','call_to_action_shortcode'); 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyouzhi7218 2016-02-12 18:38
    已采纳

    I think the WordPress function you are looking for is get_page_template_slug($post_id)

    Here is an example of how to use it in your situation:

    function call_to_action_shortcode($attrs, $content = null) {
      $image = reset(rwmb_meta( "avenir_cta_image", 'type=image&size=large' ));
      if ($image) {
        $template_name = get_page_template_slug(get_the_ID());
           if('template_left.php' === $template_name) {
                // your code for this template goes here
           } else {
                // code for other templates here
           }
        }
      }
    
      add_shortcode('call-to-action', 'call_to_action_shortcode');
    

    Edit:

    According to the documentation for get_page_template_slug, when used in the loop, the post_id defaults to the current post, so you probably can omit the parameter. Shortcodes are usually called while in the WordPress loop. This function will return empty if there is no page template, or false if it's not a page (so false for posts and other custom post types). Hope this helps!

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题