Adding short codes from a page’s content on header and hiding the same from page’s content

I have created a page and added the following shortcode from wp-orbit-slider

 [orbit-slider category="test"] 

I want the contents of

Read More
 [orbit-slider category="test"] 

be displayed on the header part, instead of on the content area part, which the wordpress usually does . I tried adding the the short code on header.php and it works, but the same content will be duplicated on the content area also. I need to avoid this. How can this be achieved ?

Related posts

Leave a Reply

2 comments

  1. This might work for you, trying to hook early to the_content filter to strip the shortcode tag from it:

    add_filter('the_content', 'ad_filter_the_content',1,1);
    function ad_filter_the_content($content) {
        // specify page id or array of page ids to include
        if (is_page(5)) {
            return str_replace('[orbit-slider category="test"]', '', $content);
        }
        return $content;
    }
    
  2. You can use the PHP call for a Shortcode. It works like this:

    echo do_shortcode('[orbit-slider category="test"]');
    

    But I think, from your question, that you may have different Shortcodes for each page. If this is the case, try adding a Custom Field for your Pages, containing the category for your Orbit-Slider.

    Of course, if you use the build in Taxonomy System (e.g. the standard Categories) you do not have to use a Custom Field, you can just use the Category. I use the first Caategory for the Categoryversion – be sure to handle the exceptions yourself here.

    // for the custom field version
    $orbitcategory = get_post_meta( get_the_ID(), 'my_orbit_slider', true );
    // for the Taxonomy version
    $orbitcategory = get_the_category( get_the_ID() );
    $orbitcategory = $orbitcategory[0]->name;
    
    
    
    $thisslider = '[orbit-slider category="' . $orbitcategory . '"]';
    
    echo do_shortcode( $thisslider );
    

    You should be fine like this.

    And remember to not put your shortcode into the Content again 🙂