【问题标题】:WordPress - Overwriting a template plugin ShortcodeWordPress - 覆盖模板插件简码
【发布时间】:2017-11-29 06:56:29
【问题描述】:

我是 php 和 wordpress 的新手。 当我从管理面板中的主题编辑图标框时,我有几个字段,例如“标题”和“内容”。标题在&lt;h4&gt; &lt;/h4&gt; 内,内容在<p> </p> 内 我需要在内容后添加另一个&lt;p style='iconbox_price'&gt;&lt;/p&gt;,以便我可以在管理面板中对其进行编辑。

这是图标框的代码。我怎样才能将它添加到它,所以它会工作。

<?php
function easyweb_webnus_iconbox( $attributes, $content = null ) {

extract(shortcode_atts(array(
    "type"=>'',
    'icon_title'=>'',
    'icon_link_url'=>'',
    'icon_link_text'=>'',
    "icon_name"=>'',
    "iconbox_content"=>'',
    "icon_size"=>'',
    "icon_color"=>'',
    "title_color"=>'',
    "content_color"=>'',
    "link_color"=>'',
    "icon_image"=>'',
    "featured"=>'',
    "border_left"=>'',
    "border_right"=>'',
), $attributes));
ob_start();

$type =  ( $type == 0 ) ? '' : $type ;

$iconbox_style = $type17_start_wrap = $type17_end_wrap = '';
if ( $type==17 ) {
    $iconbox_style = ( !empty($icon_color) ) ? ' style="color: ' . esc_attr($icon_color) . '"' : '' ;
    $type17_start_wrap = '<div class="icon-wrap" style="background-color:' . esc_attr($icon_color) . '">';
    $type17_end_wrap   = '</div>';
}

$iconbox22_class = '';
if ( $type == 22 ) {
    $iconbox22_class .= $featured ? ' ' . $featured : '';
    $iconbox22_class .= $border_left ? ' ' . $border_left : '';
    $iconbox22_class .= $border_right ? ' ' . $border_right : '';
}

echo '<article class="icon-box' . $type . $iconbox22_class . '" ' . $iconbox_style . '>';

    if(!empty($icon_name) && $icon_name != 'none') :
        if(!empty($icon_link_url))
            echo '' . $type17_start_wrap . '<a href="' . esc_url($icon_link_url) . '">'  . do_shortcode(  "[icon name='$icon_name' size='$icon_size' color='$icon_color']" ).'</a>' . $type17_end_wrap . '';
        else
            echo $type17_start_wrap . do_shortcode(  "[icon name='$icon_name' size='$icon_size' color='$icon_color']" ) . $type17_end_wrap;
    elseif(!empty($icon_image)) :
        if(is_numeric($icon_image)){
            $icon_image = wp_get_attachment_url( $icon_image );
        }
        if(!empty($icon_link_url))
            echo "<a href='$icon_link_url'>" . '<img src="'.$icon_image.'" alt="" />' . '</a>' ;
        else
            echo '<img src="'.$icon_image.'" alt="" />';
    endif;

    $title_style = !empty($title_color)?' style="color:'.$title_color.'"':'';
     echo '<h4'.$title_style.'>' . $icon_title . '</h4>';
     $content_style = !empty($content_color)?' style="color:'.$content_color.'"':'';
     echo '<p'.$content_style.'>'.$iconbox_content .'</p>' ;
     $link_style = !empty($link_color)?' style="color:'.$link_color.'"':'';
     echo (!empty($icon_link_url) &&  (!empty($icon_link_text)) )?"<a".$link_style." class=\"magicmore\" href=\"{$icon_link_url}\">{$icon_link_text}</a>":'';

echo '</article>';

$out = ob_get_contents();
ob_end_clean();
$out = str_replace('<p></p>','',$out);
    return $out;
 }
 add_shortcode('iconbox', 'easyweb_webnus_iconbox');

在 html 中,这是生成的代码:

<article class="icon-box14">
    <a href="/razrabotka-saitov/">
    <i class="sl-screen-desktop" style=" font-size:42px;"></i>
    </a>
    <h4 style="height: 22px;">Title</h4>
    <p style="height: 116px;">Content<br></p>
    <a class="magicmore" href="#">More</a>
</article>

【问题讨论】:

    标签: wordpress shortcode


    【解决方案1】:

    你看到那里的线:

    echo '<p'.$content_style.'>'.$iconbox_content .'</p>' ;
    

    您可以将自定义段落添加到该行:

    echo '<p'.$content_style.'>'.$iconbox_content .'</p>
    <p style='iconbox_price'></p>' ;
    

    【讨论】:

    • 这样网站就宕机了。即使我在你这样写的第一行之后添加另一行: echo '

      '.$iconbox_content .'

      echo '

      ';它不会在管理面板中提供一个字段来填写它。
    • 在 p 标签中将 ' 改为 "
    • 是的,我做到了。但它不会提供我可以从管理面板编辑它的任何字段或位置。我敢肯定至少应该有一些变量,可能还有一些代码让它工作
    • 感谢您的帮助。但我以非常不同的方式做到了。我马上把解决办法写下来。
    【解决方案2】:

    我在我的案例中找到了解决方案。也许它会帮助别人。我不得不编辑两个文件。就我而言,这是第一个文件所在的位置。

    wp-content/plugins/'theme'-shortcodes/shortcodes/iconbox.php
    

    我试图寻找变量$iconbox_content 的来源,并在上面的代码中从数组中提取它:

    extract(shortcode_atts(array(
            "type"=>'',
            'icon_title'=>'',
            'icon_link_url'=>'',
            'icon_link_text'=>'',
            "icon_name"=>'',
            "iconbox_content"=>'',
            "icon_size"=>'',
            "icon_color"=>'',
            "title_color"=>'',
            "content_color"=>'',
            "link_color"=>'',
            "icon_image"=>'',
            "featured"=>'',
            "border_left"=>'',
            "border_right"=>'',
        ), $attributes));
    

    所以我搜索了所有 wordpress 目录文件,因为我什至不知道在哪里可以找到它。 并在themes =&gt; 'my theme' =&gt; 'my theme' =&gt; visualcomposer =&gt; shortcodes目录中找到03-iconbox.php

    我有不同的数组,例如:

        array(
            "type"=>'textarea',
            "heading"=>esc_html__('Content', 'ew'),
            "param_name"=> "iconbox_content",
            "value"=>"",
            "description" => esc_html__( "IconBox Content Goes Here", 'ew') 
        ),
    

    所以我想,我所做的另一半已经很合乎逻辑了,但无论如何:

    我在这个文件中添加了一个数组:

        array(
            "type"=>'textarea',
            "heading"=>esc_html__('Price', 'ew'),
            "param_name"=> "iconbox_price",
            "value"=>"",
            "description" => esc_html__( "Price goes here", 'ew')   
        ),
    

    在第一个文件中: 提取"iconbox_price" =&gt;'',

    以下:echo '&lt;p class="iconbox_price"&gt;'.$iconbox_price .'&lt;/p&gt;' ;

    希望这对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 2014-02-11
      • 1970-01-01
      • 2013-02-08
      • 2013-02-26
      • 2018-04-07
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多