• Resolved DigitalFormula

    (@digitalformula)


    Hi all,

    I’m building a theme and have added some shortcodes using add_shortcode as per the WordPress codex. When I try to use them, the theme just renders the shortcode markup and doesn’t return what the shortcode is supposed to return.

    I disabled all plugins but that didn’t work. I then setup a brand new installation of WordPress 3.7.1 that has no plugins and no customization at all. I then added my shortcode function to functions.php and tested it – still no go. It just displays the shortcode markup.

    Here is the functions.php code:

    function permalink_test( $atts )
    {
      extract( shortcode_atts( array( 'id' => 1, 'text' => "" ), $atts ) );
      if( $text )
      {
        $url = get_permalink( $id );
        return( "<a href='$url'>$text</a>" );
      }
      else
      {
        return( get_permalink( $id ) );
      }
    }
    
    add_shortcode( 'permalink', 'permalink_test' );

    I got that code from the Digging Into WordPress book so it should be ok.

    Here is index.php from the basic test theme:

    <?php
    
    while( have_posts() ) :
    
      the_post();
    
      the_title();
    
      ?>
    
      [permalink id=1, text="hello"]
    
      <?
    
    endwhile;
    
    ?>

    I’m pretty sure that’s ok but am more than happy to be told it is wrong, e.g. if I’m using something outside the loop when it should be inside, etc.

    What could be causing the shortcodes to not work?

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • what are you using as shortcode in the content of a post/page?

    for example?
    [permalink text=test id=1]
    or
    [permalink text="some test" id="123"]

    how are you calling the content in the template? are you using the_content() ?

    just saw your added code;

    generally, the_content() will parse any shortcodes;

    or you can use this directly in a template:

    <?php echo do_shortcodes('[permalink id=1, text="hello"]'); ?>

    http://codex.wordpress.org/Function_Reference/do_shortcode

    Thread Starter DigitalFormula

    (@digitalformula)

    Great, thanks alchymyth. The do_shortcode() function does what I need but does raise a question.

    Do shortcodes only get parsed if they’re in a post or page’s content? Can I not use them inside a template without using do_shortcode()?

    Thanks again.

    Do shortcodes only get parsed if they’re in a post or page’s content? Can I not use them inside a template without using do_shortcode()?

    yes

    Thread Starter DigitalFormula

    (@digitalformula)

    Thank you. I’ll mark this topic as resolved.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Shortcodes not working in custom theme’ is closed to new replies.