Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Here is my case, I'm making some experiments with post types, basically I created a post type called Slider where the featured image is will display in the slide.

Now, when user type something like domain.com/slider/slide1, or try to reach the slider post, I want to display a not found page instead of the single page, or the index in case I dont have the post single page.

Is it possible to achieve?

Solved!

Thanks to Saif Bechan, I could solve my problem,

Basically I used this function

 add_action('template_redirect', 'my_template_redirect');
 function my_template_redirect() {
 global $wp_query, $post;
 if(is_singular('Slider')) 
 {
      $wp_query->set_404();
   }
 }

Where Slider is the Post Type name,

share|improve this question

3 Answers 3

up vote 1 down vote accepted

I have a script that wil; display a good 404 page for certain pages. Now I do not yet know what custom post types are, as I am just starting, but I think you can edit the script to fit your needs.

add_action('template_redirect', 'my_template_redirect');
function my_template_redirect()
{
    global $wp_query, $post;

    if (is_author() || is_attachment() || is_day() || is_page())
    {
        $wp_query->set_404();
    }

    if (is_feed())
    {
        $author     = get_query_var('author_name');
        $attachment = get_query_var('attachment');
        $attachment = (empty($attachment)) ? get_query_var('attachment_id') : $attachment;
        $day        = get_query_var('day');
        $page   = get_query_var('page');

        if (!empty($author) || !empty($attachment) || !empty($day) || !empty($page))
        {
            $wp_query->set_404();
            $wp_query->is_feed = false;
        }
    }
}

Hope you can do something with this this code. I use this in my functions.php to block access to the different parts of the website. They are also blocked for feeds.

share|improve this answer
    
I used your idea to solve the problem, using the is_singular function instead, that returns "true" if is_single(), is_page() or is_attachment(). codex.wordpress.org/Function_Reference/is_singular thanks a ton –  andresmijares25 Nov 19 '11 at 5:36
    
Glad I could. I thought this script would come of some use, as it also deals with 404 pages. Ill remember the function you created also for custom post types, maybe I will need it in the future. –  Saif Bechan Nov 19 '11 at 6:46

You could just copy your index.php file and name it 404.php. Then if a page or attachment page wasn't found it would use the 404.php page which would just be a copy of the index.php file.

share|improve this answer
    
uh! so simple that I couldn't think about it! however I would like another more technical solution! but probably at the end of the day I will go for it! Muchas Gracias :)! –  andresmijares25 Nov 19 '11 at 3:27

If I understand you correctly create a page template named 404, then use this plugin: http://wordpress.org/extend/plugins/custom-post-template/ which will allow you to choose it as the template for that page.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.