Title shortcode how to work with new theme

huangapple go评论99阅读模式
英文:

Title shortcode how to work with new theme

问题

我在将主题更改为更新PHP到8版本后遇到了一个问题,有一个标题的短代码如下所示:

[title size="H3" bold="no" align="left" color="#226dae" style="" border_color="" margin_top="" margin_bottom=""]...[/title]

[title size="H5" bold="no" align="left" color="#226dae" style="" border_color="" margin_top="" margin_bottom=""]...[/title]

我应该怎么做才能正确显示标题?需要在function.php中添加代码吗?

我尝试在function.php中添加了一小段代码,但似乎没有针对我的短代码,标题也没有显示。

英文:

I have a problem after change my theme for update the PHP to 8 version, there is a shortcode for the title like that:

[title size="H3" bold="no" align="left" color="#226dae" style="" border_color="" margin_top="" margin_bottom=""]...[/title]

[title size="H5" bold="no" align="left" color="#226dae" style="" border_color="" margin_top="" margin_bottom=""]...[/title]

What I have to do for have the right view for the title? Add a code in function.php?

I tried small code on function.php but seems not target my shortcode and the title don't show.

答案1

得分: 1

似乎您曾使用一个主题,提供了自定义标题显示的短代码功能,但新主题中并没有这个功能。如果您想继续使用先前主题的短代码,您需要在新主题中重新创建这个功能。

您可以通过在您的主题的 functions.php 文件中添加一个短代码函数来实现这一点。以下是一个示例,展示了如何创建一个处理特定标题短代码的函数:

function custom_title_shortcode($atts, $content = null) {
    extract(shortcode_atts(
        array(
            'size' => 'h2',
            'bold' => 'no',
            'align' => 'left',
            'color' => '#000000',
            'style' => '',
            'border_color' => '',
            'margin_top' => '',
            'margin_bottom' => ''
        ), $atts)
    );

    $style_attributes = 'text-align:'.$align.'; color:'.$color.'; border-color:'.$border_color.'; margin-top:'.$margin_top.'; margin-bottom:'.$margin_bottom.';';

    if ($bold == 'yes') {
        $style_attributes .= ' font-weight:bold;';
    } else {
        $style_attributes .= ' font-weight:normal;';
    }

    $style_attributes .= ' '.$style; // 包括额外的样式

    return '<'.$size.' style="'.$style_attributes.'">'.$content.'</'.$size.'>';
}

add_shortcode('title', 'custom_title_shortcode');

这个函数创建了一个名为 [title] 的短代码,它接受不同的参数来自定义标题的外观,如大小、对齐方式、颜色、边框颜色、顶部边距和底部边距。在开放和闭合短代码标签之间的 $content 变量将用作标题的文本。

将此代码添加到您的 functions.php 文件后,您应该能够在您的文章和页面中使用 [title] 短代码。短代码参数可以以如下格式设置:[title parameter="value"]您的标题文本[/title]

但请注意,直接编辑主题的 functions.php 文件可能不是最佳实践,因为这些更改在主题更新时会丢失。更好的方法是创建一个子主题或使用自定义插件来添加您的短代码。

英文:

It seems like you were using a theme that provided shortcodes functionality for custom title display, which is not present in the new theme you have switched to. If you want to continue using the shortcode from the previous theme, you'll have to recreate this functionality in your new theme.

You can do this by adding a shortcode function in your theme's functions.php file. Below is an example of how you can create a function that handles your specific title shortcode:

function custom_title_shortcode($atts, $content = null) {
extract(shortcode_atts(
    array(
        &#39;size&#39; =&gt; &#39;h2&#39;,
        &#39;bold&#39; =&gt; &#39;no&#39;,
        &#39;align&#39; =&gt; &#39;left&#39;,
        &#39;color&#39; =&gt; &#39;#000000&#39;,
        &#39;style&#39; =&gt; &#39;&#39;,
        &#39;border_color&#39; =&gt; &#39;&#39;,
        &#39;margin_top&#39; =&gt; &#39;&#39;,
        &#39;margin_bottom&#39; =&gt; &#39;&#39;
    ), $atts)
);

$style_attributes = &#39;text-align:&#39;.$align.&#39;; color:&#39;.$color.&#39;; border-color:&#39;.$border_color.&#39;; margin-top:&#39;.$margin_top.&#39;; margin-bottom:&#39;.$margin_bottom.&#39;;&#39;;

if ($bold == &#39;yes&#39;) {
    $style_attributes .= &#39; font-weight:bold;&#39;;
} else {
    $style_attributes .= &#39; font-weight:normal;&#39;;
}

$style_attributes .= &#39; &#39;.$style; // Include additional style

return &#39;&lt;&#39;.$size.&#39;style=&quot;&#39;.$style_attributes.&#39;&quot;&gt;&#39;.$content.&#39;&lt;/&#39;.$size.&#39;&gt;&#39;; 
}

add_shortcode(&#39;title&#39;, &#39;custom_title_shortcode&#39;); `

This function creates a shortcode [title] which accepts different parameters to customize the appearance of the title, such as size, alignment, color, border color, top margin, and bottom margin. The $content variable between the opening and closing shortcode tags will be used as the text for the title.

After adding this to your functions.php file, you should be able to use your [title] shortcode in your posts and pages. The shortcode parameters can be set in the format [title parameter="value"]Your title text[/title].

However, please note that directly editing a theme's functions.php file might not be the best practice as these changes will be lost when the theme gets updated. A better approach would be to create a child theme or to use a custom plugin to add your shortcodes.

huangapple
  • 本文由 发表于 2023年7月10日 15:38:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定