简体   繁体   中英

Wordpress single.php not working when remove index.php from permalink

I'm very new to wordpress theme developing and just started to make a one with bootstrap 3.I'm running wordpress on my localhost on nginx webserver.I created index.php file and make posts listed on it with read more buttons and linked their titles to post's permalinks and created the single.php also.It also working fine but i feel there's something wrong with my codes.

My post's permalinks looks like this.Always showing index.php

http://www.blog.dev/index.php/%postname

so i change post's permalinks to custom and removed that "index.php". After that my signle.php doesn't working. I mean it gives me a error 404.

Here's my single.php

<!-- Including Header -->
<?php get_header(); ?>

<!-- Post Cover -->
<div id="postCover" class="container-fluid"></div>
<div id="postContainer" class="container">
    <div class="col-md-2"></div>
    <div class="col-md-8">
        <div class="panel panel-default">
            <div class="panel-heading">
                <?php the_title(); ?>
            </div>
            <div class="panel-body">
                <?php
                    if ( has_post_thumbnail() ) {
                        the_post_thumbnail('post-thumbnail', array( 'class' => "img-responsive"));
                    }
                ?>
                <div id="posDetailsContainer" class="container-fluid text-left">
                    <span>
                        <i class="fa fa-calendar"></i>&nbsp;
                        Posted on <?php the_time('M'); ?>&nbsp;&nbsp;<?php the_time('j'); ?>&nbsp;&nbsp;<?php the_time('Y'); ?>
                    </span>
                    <span>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <i class="fa fa-comments-o"></i>&nbsp;
                        <?php comments_number( 'No Comments', '1 Comment', '% Comments' ); ?>
                    </span>
                </div>

                <!-- Post Content Display -->
                <div id="postContent" class="container-fluid">
                    <?php
                        if ( have_posts() ) : while ( have_posts() ) : the_post();
                            the_content();
                            endwhile;
                        endif;
                    ?>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-2"></div>
</div>

How to fix this ?

Thanks ! Srivin Prabhash

Here is the whole single.php you might wanna use/start w

<?php
/**
 * The Template for displaying all single posts.
 */

get_header(); ?>

<div id="content" role="main">

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

        <div class="entry">
            <?php the_content(); ?>
        </div>

        <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    </div>
    <?php endwhile; endif; ?>

</div><!-- #content -->

I think you have done some mistake in index.php. Put this code in your index.php file.

    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
    <h5>Posted By :  <?php the_author(); ?> | Date : <?php echo the_date('d-m-y'); ?></h5>
    <p><?php  the_excerpt(); ?></p>
    <a href="<?php the_permalink();?>"> Read more </a>
    <?php endwhile; ?>
    <?php endif; ?>
  <?php wp_reset_query(); ?>    

Still if you face this problem remove your .htaccess file from root directory and change permalink setting.Tack backup before delete .htaccess file.

I hope this will work for you.

Add .htaccess file on root folder and paste this code inside the file

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

and then go to setting->permalink and select postname. it will works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM