0

I am trying to create my first wordpress theme. But I have run into som problems. When I click the link (the_permalink()) it leads back to the homepage. I get the right URL but the URL is showing the homepage, so I feel I am missing something.

So far I got a header.php, footer.php, sidebar.php, functions.php, style.css and index.php. The index file contains my loop.

I have also created a custom post type. And it is the custom post type I am looping in index.php.

What am I missing? Why does the permalink lead me to the homepage (loop page/index.php)? I have tried created single.php and single-customposttypeslug.php both empty so far. But that did'nt sovle my problem.

So what am I missing? I have been googling, searching SO and looked into the codex but I can't figure out what I am doing worng..

My permalinks looks like this websitename.com/index.php/property/test6/

The Loop:

<?php
/* START THE LOOP */
$loop = new WP_Query( array( 'post_type' => 'properties', 'posts_per_page' => 8 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); 
/* GET METADATA */
$city = get_post_meta( get_the_ID(), 'city_id', true );
$rentorsale = get_post_meta( get_the_ID(), 'rentorsale_id', true );
$price = get_post_meta( get_the_ID(), 'price_id', true );
$livingarea = get_post_meta( get_the_ID(), 'livingarea_id', true ); 
$bedrooms = get_post_meta( get_the_ID(), 'bedrooms_id', true ); 
$bathrooms = get_post_meta( get_the_ID(), 'bathrooms_id', true ); ?>
<a href="<?php the_permalink(); ?>">
    <div class="col-md-3"> <!-- START BS COL-MD-3 -->
        <div class="poststyle"> <!-- START POSTSTYLE -->
            <div class="postheader"> <!-- START POSTHEADER -->
                <h2><?php echo $city; ?></h2>
            </div> <!-- END POSTHEADER -->
            <div class="imgcontainer"> <!-- START IMGCONTAINER -->
                <?php the_post_thumbnail(); ?>
                <img src="<?php bloginfo('stylesheet_directory'); ?>/img/1.jpg" alt="property" title="Real Estate"/>
                <div class="rentorsale"> <!-- START RENTORSALE -->
                    <p><?php echo $rentorsale; ?></p>
                </div> <!-- END RENTORSALE -->
                <div class="price"> <!-- START PRICE -->
                    <p><?php echo $price; ?> <i class="fa fa-usd"></i></p>
                </div> <!-- END PRICE -->
            </div> <!-- END IMGCONTAINER -->
            <table class="table"> <!-- START BS TABLE -->
                <tr>
                    <td>Size</td>
                    <td><?php echo $livingarea; ?></td>
                </tr>
                <tr>
                    <td>Bedrooms</td>
                    <td><?php echo $bedrooms; ?></td>
                </tr>
                <tr>
                    <td>Bathrooms</td>
                    <td><?php echo $bathrooms; ?></td>
                </tr>
            </table> <!-- END BS TABLE -->
        </div> <!-- END POSTSTYLE -->
    </div> <!-- END BS COL-MD-3 -->
</a>
<?php endwhile; wp_reset_query();
?>

Register custom post type:

    <?php

add_action( 'init', 'properties_post_type' );

function properties_post_type() {

    $args = array(

        'description'         => __( 'Post type to manage properties', ' ' ), // string
        'public'              => true, // bool (default is FALSE)
        'publicly_queryable'  => true, // bool (defaults to 'public').
        'exclude_from_search' => false, // bool (defaults to 'public')
        'show_in_nav_menus'   => false, // bool (defaults to 'public')
        'show_ui'             => true, // bool (defaults to 'public')
        'show_in_menu'        => true, // bool (defaults to 'show_ui')
        'show_in_admin_bar'   => true, // bool (defaults to 'show_in_menu')
        'menu_position'       => 6, // int (defaults to 25 - below comments)
        'menu_icon'           => null, // string (defaults to use the post icon)
        'can_export'          => true, // bool (defaults to TRUE)
        'delete_with_user'    => true, // bool (defaults to TRUE if the post type supports 'author')
        'hierarchical'        => false, // bool (defaults to FALSE)
        'has_archive'         => 'false', // bool|string (defaults to FALSE)
        'query_var'           => 'properties', // bool|string (defaults to TRUE - post type name)
        'capability_type'     => 'post', // string|array (defaults to 'post')
        'map_meta_cap'        => true, // bool (defaults to FALSE)

        'rewrite' => array(

            'slug'       => 'property', // string (defaults to the post type name)
            'with_front' => false, // bool (defaults to TRUE)
            'pages'      => true, // bool (defaults to TRUE)
            'feeds'      => true, // bool (defaults to the 'has_archive' argument)
            'ep_mask'    => EP_PERMALINK, // const (defaults to EP_PERMALINK)
        ),


        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'author',
            'thumbnail',
            'trackbacks',
            'custom-fields',
            'revisions',
            'page-attributes',
            'post-formats',
        ),

        'labels' => array(
            'name'               => __( 'Properties',                   ' ' ),
            'singular_name'      => __( 'Property',                    ' ' ),
            'menu_name'          => __( 'Properties',                   ' ' ),
            'name_admin_bar'     => __( 'Properties',                   ' ' ),
            'add_new'            => __( 'Add New',                    ' ' ),
            'add_new_item'       => __( 'Add New Property',            ' ' ),
            'edit_item'          => __( 'Edit Property',               ' ' ),
            'new_item'           => __( 'New Property',                ' ' ),
            'view_item'          => __( 'View Property',               ' ' ),
            'search_items'       => __( 'Search Properties',            ' ' ),
            'not_found'          => __( 'No properties found',          ' ' ),
            'not_found_in_trash' => __( 'No properties found in trash', ' ' ),
            'all_items'          => __( 'All properties',               ' ' ),
            'archive_title'      => __( 'Properties',                   ' ' ),
        )
    );

    /* Register the post type. */
    register_post_type(
        'properties', 
        $args      // Arguments for post type.
    );
}

?>
  • Where is that loop located? Which file? – Tom J Nowell Aug 11 '15 at 17:20
  • As mention in the post. The loop is locatd in index.php – Alex Aug 11 '15 at 17:46
  • No wonder then, every page will be showing that loop, even if it isn't the homepage. Your pages are showing fine, they're just loading a template which is ignoring what WordPress says and always displaying the same thing – Tom J Nowell Aug 11 '15 at 19:09
  • You should also swap your wp_reset_query for wp_reset_postdata, but that won't fix your issue – Tom J Nowell Aug 11 '15 at 19:09
  • I have now added a home.php, home.php is used as the homepage now. But when I click the permalink index.php is still being used. – Alex Aug 11 '15 at 20:56
0

index.php is not the template for the homepage, it's the fallback template. Right now every page you load is falling back to the catch all index.php template, but your index.php doesn't have a main loop, and isn't set up as the fallback catch all template. Instead it's doing its own new query and displaying the results.

So no, you're not being redirected to the homepage, rather, you're displaying the homepage on every page. Move your homepage code into a homepage template, e.g. home.php or a page template called home. index.php should be the last resort fallback, with a generic main loop.

I recommend you take a look at the template hierarchy to see how WordPress decides which template to load, and what you can implement

  • Hi I will take a look at the template hierachy, I have single.php from before and have now also added page.php (both empty of code) but I still get the same problem. My permalinks look like this for example websitename.com/index.php/property/test6/ – Alex Aug 10 '15 at 23:02
  • You should set up pretty permalinks and your htaccess file or nginx configs, this doesn't look like a templating issue. Make sure everything works in the standard default themes first – Tom J Nowell Aug 10 '15 at 23:40
  • I have now tried to change to Twenty Fifteen and it working fine with that theme. So that is not the issue. I will update my post with my loop code and CPT code, maybe the issue is there. – Alex Aug 11 '15 at 0:02
  • @JohanGudmundsson I've updated my answer – Tom J Nowell Aug 11 '15 at 19:13
  • I see, I will update my templete structure and see if that helps! – Alex Aug 11 '15 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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