1

I'm new to WordPress and need to create a website including a special form, with which I can upload photos, set my position on an OpenStreetMap and choose from several dropdown menus.

Here you can find an example of how it should look like: enter image description here

I already did this:

  1. Created a page template with the form

    <form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">

  2. Made the form submitable for users who are logged in (is this a secure solution?)

        <?php if ( 0 == $current_user->ID ) {
                         wp_loginout(); 
                         echo " to submit a post";
                    } else {
                      echo '<fieldset class="submit">
                                <input type="submit" value="Los" tabindex="40" id="submit" name="submit" />
                            </fieldset>
                            <input type="hidden" name="action" value="new_post" />';
                            wp_nonce_field( "new-post" ); } ?>
    
  3. When the form is submitted this php code is executed:

    if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {    
        $new_post = array(
                'post_title'    =>  "roadkill entry",
                'post_content'  =>  "testcontent",
                'post_category' =>  array(0),
                'post_status'   =>  'publish',
                'post_type' =>  'roadkills' // this is my custom post   );
    
        $pid = wp_insert_post($new_post);
    
        $link = get_permalink( $pid );
        wp_redirect( $link );
    }
    

But I will end in an page not found. http://www.citizen-science.at/wordpress/roadkill/eintrag-hinzufugen/

When I create an entry via the admin GUI I am able to view the single post, but not the archive http://www.citizen-science.at/wordpress/roadkills/dafdf/

Oh yeah, here is how I am registering my post type:

<?php 
add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'roadkills',
        array(
            'labels' => array(
                'name' => __( 'Roadkills' ),
                'singular_name' => __( 'Roadkill' )
            ),
        'public' => true,
        'has_archive' => true,
        )
    );
}

What did I miss here? (I already got it working yesterday, but somehow it broke)

0

3 Answers 3

1

Within your theme folder create a page names archive-{YOUR_POST_TYPE}.php. In your case archive-roadkills.php. Test whether your archive page is catching this page or not?

If yes, then you can edit the page the way you like. Whether copying the code from existing archive.php or your own.

Reference: Template Hierarchy Codex

1

I suggest to give the following code a try:

<?php 
add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'roadkills',
        array(
            'labels' => array(
                'name' => __( 'Roadkills' ),
                'singular_name' => __( 'Roadkill' )
            ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'roadkills')
        )
    );
}

I suppose the missing 'rewrite' => array('slug' => 'roadkills') within your register post type function leads into the permalink error and can be solved by adding the line above.

2
  • 1
    Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Mar 5, 2014 at 12:03
  • I tried this, but it had no effect :( I could only solve this by creating another custom post type.
    – 4ndro1d
    Mar 6, 2014 at 19:19
-1

I could only solve the problem by creating another custom post type (with another name). Afterwards everything just went fine. I am sorry, but I don't have any explanation for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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