How to display non-page / post content

I have a custom made Twitter stream on my site.

I want to have a permalink for each tweet which would go to a page which would display only the tweet – no other content – surrounded by my custom theme.

Ideally I would get WordPress to display a Page Template on a certain url (something like /tweet/<tweet_id>), but I don’t want to create a Page because it will appear in the main menu.

I’m happy to write a plugin but my Google-fu is failing help me to figure out how to display just that plugin’s content on a page.

2 Answers
2

You can do this by creating a “fake page” so to speak, where instead of creating the page in your dashboard area under Pages -> Add New then selecting a template from Page Attributes or by creating a template that matched the slug or id such as page-{slug}.php or page-{id}.php you instead use a combination of rewrite rules combined with hooking onto an action template_redirect to achieve the desired result.

You can see an exact example of this in my answer to this question.

I’ll copy in my snippets below but the full explanation should be read in my answer above.

Step 1:

add_action('init', 'fake_page_rewrite');

function fake_page_rewrite(){

    global $wp_rewrite;

    //set up our query variable %fake_page% which equates to index.php?fake_page= 
    add_rewrite_tag( '%fake_page%', '([^&]+)'); 

    //add rewrite rule that matches /blog/page/2, /blog/page/3, /blog/page/4, etc..
    add_rewrite_rule('^blog/page/?([0-9])?','index.php?fake_page=blog&paged=$matches[1]','top');  

    //add rewrite rule that matches /blog
    add_rewrite_rule('^blog/?','index.php?fake_page=blog','top');

    //add endpoint, in this case 'blog' to satisfy our rewrite rule /blog, /blog/page/ etc..
    add_rewrite_endpoint( 'blog', EP_PERMALINK | EP_PAGES );

    //flush rules to get this to work properly
    $wp_rewrite->flush_rules();

}

Step 2:

add_action('template_redirect', 'fake_page_redirect');

    function fake_page_redirect(){

        global $wp;

        //retrieve the query vars and store as variable $template 
        $template = $wp->query_vars;

        //pass the $template variable into the conditional statement and
        //check if the key 'fake_page' is one of the query_vars held in the $template array
        //and that 'blog' is equal to the value of the key which is set
        if ( array_key_exists( 'fake_page', $template ) && 'blog' == $template['fake_page'] ) {

            //if the key 'fake_page' exists and 'blog' matches the value of that key
            //then return the template specified below to handle presentation
            include( get_template_directory().'/your-template-name-here.php' );

            exit;

        }
    }

Note: Some tweaking might be necessary to associate individual tweet IDs but we’d need to see your code that relates to the tweets.

These snippets go into your functions.php file. Naturally you should change any of the parameters that should otherwise meet your desired end point. So where you see blog becomes whatever-you-want etc. The template name in the second snippet should correspond to your desired template.

Leave a Comment