最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Create shortcode in Child Theme?

programmeradmin0浏览0评论

I have created a child theme, and I am trying to add a shortcode to its functions.php file, by doing this:

// functions.php of child theme

<?php 

include(WP_CONTENT_DIR . '/graduates_functions.php');

?>

// graduates_functions.php

<?php

// [bartag foo="foo-value"]
function sayhi_func( $atts ) {

    extract( shortcode_atts( array(
        'graduate_type' => 'seo',
        'bar' => 'something else',
    ), $atts ) );
    echo 'test';
}

add_shortcode( 'sayhi', 'sayhi_func' );

?>

But when doing [sayhi] from a page I don't see the echo, I see [sayhi]. It works if I add the same in the functions.php of the theme itself, not the child.

Any thoughts?

I have created a child theme, and I am trying to add a shortcode to its functions.php file, by doing this:

// functions.php of child theme

<?php 

include(WP_CONTENT_DIR . '/graduates_functions.php');

?>

// graduates_functions.php

<?php

// [bartag foo="foo-value"]
function sayhi_func( $atts ) {

    extract( shortcode_atts( array(
        'graduate_type' => 'seo',
        'bar' => 'something else',
    ), $atts ) );
    echo 'test';
}

add_shortcode( 'sayhi', 'sayhi_func' );

?>

But when doing [sayhi] from a page I don't see the echo, I see [sayhi]. It works if I add the same in the functions.php of the theme itself, not the child.

Any thoughts?

Share Improve this question asked Apr 8, 2014 at 22:00 Hommer SmithHommer Smith 1271 silver badge7 bronze badges 4
  • Its worth a second look, but it sounds like you do not have the child theme enabled. – Douglas.Sesar Commented Apr 8, 2014 at 22:24
  • 1 where the graduates_functions.php is saved? – gmazzap Commented Apr 9, 2014 at 1:14
  • ... and what is in graduates_functions.php? Is that part of your theme or the parent? – s_ha_dum Commented Apr 9, 2014 at 1:41
  • I have the same problem – Ooker Commented Jun 27, 2020 at 7:56
Add a comment  | 

1 Answer 1

Reset to default 1

Shortcodes don't 'echo', they need to 'return' something. So I guess it will work if you put your functions content between ob_start and ob_get_clean so you return everything inbetween:

function yourfunction(){ ob_start(); /* insert here the code of your function */ $output = ob_get_clean(); return $output; }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论