Forums

Enable shortcodes in custom post types? (7 posts)

  1. tmuka
    Member
    Posted 1 year ago #

    I've searched around and couldn't find any information on how to enable shortcode filters in my simple custom post type (below). I'm hoping somebody here smarter than me might point me in the right direction.

    /*
    * Add Banner Post Type
    */
    function register_sc_banner_post_type() {
    	$args = array(
    		'label' => __('Banners'),
    		'singular_label' => __('Banner'),
    		'public' => true,
    		'show_ui' => true,
    		'exclude_from_search' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'rewrite' => array('slug'=>'banner'),
    		'supports' => array('title', 'editor','custom-fields')
    		)
    	);
    	register_post_type( 'sc_banner' , $args );
    }
    add_action('init', 'register_sc_banner_post_type');

    The shortcode and custom post type are both defined in my theme's functions.php file. Is there a hook I need to apply the shortcode filters to my custom post somehow?

    Thanks for any help you might provide!

  2. dfunkydog
    Member
    Posted 1 year ago #

    Does the shortcode work for the normal posts?

  3. dfunkydog
    Member
    Posted 1 year ago #

    I tried your custom post types and my shortcodes work in banner posts. Have you hooked the shortcode to wordPress.

    add_shortcode($tag, $func)

  4. tmuka
    Member
    Posted 1 year ago #

    Thanks for the help, dfunkydog!

    Yes, my shortcode does work for normal posts. The shortcode is pretty simple, still i tried an even simple test shortcode that still doesn't work in my custom post type.
    // [sc_test]
    function sc_test(){
    return "TEST SUCCESS";
    }
    add_shortcode('sc_test', 'sc_test');

    my shortcode is declared above my custom post type in functions.php

    thanks for any advice you can provide, i appreciate it!

  5. tmuka
    Member
    Posted 1 year ago #

    Ok, turns out the issue was that the get_the_content() in my custom query_posts loop doesn't apply filters. (this is noted at the bottom of http://codex.wordpress.org/Function_Reference/get_the_content

    <?php
    echo apply_filters('the_content',get_the_content('Continue reading <span class="meta-nav">→</span>'));
    ?>

    Hope this helps somebody else!

  6. cmunns
    Member
    Posted 5 months ago #

    Right on the money. Thanks for following up with the answer here tmuka

  7. tmuka
    Member
    Posted 5 months ago #

    glad it helped!

Topic Closed

This topic has been closed to new replies.

About this Topic