简体   繁体   English

插件内的Wordpress简码

[英]Wordpress Shortcode inside of plugin

Here is a snippet from the shortcodes.php file in one of the plugins I'm writing. 这是我正在编写的其中一个插件中shortcodes.php文件的摘录。

if($home_loop->have_posts()) {
        switch($display) {      

        case "static":

                while ($home_loop->have_posts()): $home_loop->the_post();

                        if(has_post_thumbnail() ) { 
                            $content .= '<div class="parallax">';
                            $content .= get_the_post_thumbnail($post->ID, 'wrapper-wide', array('class' => "img-responsive")); 
                            $content .= '</div>';
                            $content .= '<div class="container">';
                            $content .= get_the_content();
                            $content .= '</div>';

                            $content .= '<style>';
                            $content .= '.parallax{//css style here}';

                            $content .= '</style>;



                        } else {
                            //Something
                        }

                endwhile;

                break;

This works perfect. 这完美。 If I put a featured image thumbnail in my home post it will be displayed in my parallax div. 如果我在首页中张贴了精选图片缩略图,它将显示在我的视差div中。 The question I have is instead of placing the thumbnail inside of the div, I want to have it background-image: url(). 我的问题是,我不想将缩略图放在div内,而希望它具有background-image:url()。 I found this code below 我在下面找到了这段代码

<?php
$background = get_field( 'background_image' );

if($background):
?>
<style>
.main-wrapper {
 background-image: url(<?php echo $background ?>);
}
</style>
<?php endif; ?>

How would I incorporate and run something like this inside of the $content? 我如何在$ content内合并并运行类似的内容? The reason I'm trying to get the image as a background-image:url() is so I can add specific css styling like background-position, size, repeat, attachment. 我尝试将图像获取为background-image:url()的原因是为了可以添加特定的CSS样式,例如background-position,size,repeat,attach。

Why not just add the image as a background-image right inside your code? 为什么不直接在代码内部将图像添加为背景图像呢? [wp_get_attachment_image_src][1] will return an array of the url, width and height of any attachment. [wp_get_attachment_image_src] [1]将返回任何附件的url,宽度和高度的数组。 Pass it the ID of the featured image, and gain the flexibility to use ANY size you want, not just the actual featured image size. 将特征图像的ID传递给它,并可以灵活使用所需的任何尺寸,而不仅仅是实际的特征图像尺寸。 Then place it as the background using inline CSS. 然后使用内联CSS将其作为背景。 It can be overwritten in your CSS sheet if needed. 如果需要,可以在CSS工作表中将其覆盖。

($home_loop->have_posts()) {
        switch($display) {      

        case "static":

                while ($home_loop->have_posts()): $home_loop->the_post();

                        if(has_post_thumbnail() ) {
                            $post_thumnail_info = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'wrapper-wide' );
                            $content .= '<div class="parallax" style="background-image: url( \'' . $post_thumbnail_info[0] . '\' );">';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM