-1

我有一个显示博客文章 [latest_posts.php] 的页面和另一个显示单个博客文章 [blog.php] 的页面。我打算在 latest_posts.php 中链接图像标题,以便它重定向到 blog.php,它会显示被点击的特定帖子。

latest_posts.php ---->

    <div class="main blog">

        <!-- Header -->
        <h2 class="underline">
            <span>What&#039;s new</span>
            <span></span>
        </h2>
        <!-- /Header -->

        <!-- Posts list --> 
        <ul class="post-list post-list-1">


                <?php

                /* Fetches Date/Time, Post Content and title */

                include 'dbconnect.php';

                $sql = "SELECT * FROM wp_posts";
                $res = mysql_query($sql);


                while ( $row = mysql_fetch_array($res) ) {
                ?>  


            <!-- Post #1 -->
            <li class="clear-fix">

                <!-- Date -->
                <div class="post-list-date">
                    <div class="post-date-box">


                        <?php 
                            //Timestamp broken down to show accordingly
                            $timestamp = $row['post_date'];
                            $datetime = new DateTime($timestamp);
                            $date = $datetime->format("d");
                            $month = $datetime->format("M");
                    ?> 
                            <h3>    <?php echo $date; ?>    </h3>
                            <span>  <?php echo $month; ?>   </span>

                    </div>
                </div>
                <!-- /Date -->

                <!-- Image + comments count -->
                <div class="post-list-image">
                    <!-- Image -->
                    <div class="image image-overlay-url image-fancybox-url">
                        <a href="post.php" class="preloader-image">
                            <?php 
                                                echo '<img src="', $row['image'], '" alt="' , $row['post_title'] , '\'s Blog Image" />'; 
                            ?>
                        </a>
                    </div>
                    <!-- /Image -->
                </div>
                <!-- /Image + comments count -->

                <!-- Content -->
                <div class="post-list-content">
                    <div>
                        <!-- Header -->
                        <h4> <a href="post.php? . $row['ID'] . "> <?php echo $row['post_title']; ?> </a> </h4>



                        <!-- /Header -->

                        <!-- Excerpt -->
                        <p>
                            <?php echo $row ['post_content'];  }?>
                        </p>
                        <!-- /Excerpt -->

                    </div>

                </div>
                <!-- /Content -->

            </li>
            <!-- /Post #1 -->

        </ul> 
        <!-- /Posts list -->


        <a href="blog.php" class="button-browse">Browse All Posts</a>

    </div>

    <?php require_once('include/twitter_user_timeline.php'); ?>

博客.php --->

    <?php require_once('include/header.php'); ?>

    <body class="blog">

        <?php require_once('include/navigation_bar_blog.php'); ?>

        <div class="blog">

            <div class="main">

                <!-- Header -->
                <h2 class="underline">
                    <span>What&#039;s new</span>
                    <span></span>
                </h2>
                <!-- /Header -->

                <!-- Layout 66x33 -->
                <div class="layout-p-66x33 clear-fix">

                    <!-- Left column -->
                    <!-- <div class="column-left"> -->

                        <!-- Posts list -->
                        <ul class="post-list post-list-2">

                        <?php

                        /* Fetches Date/Time, Post Content and title with Pagination */

                        include 'dbconnect.php';

                        //sets to default page
                        if(empty($_GET['pn'])){
                            $page=1;
                        } else {
                            $page = $_GET['pn'];

                        }
                        // Index of the page
                        $index = ($page-1)*3;

                        $sql = "SELECT * FROM `wp_posts` ORDER BY `post_date` DESC LIMIT " . $index . " ,3";
                        $res = mysql_query($sql);

                        //Loops through the values
                        while ( $row = mysql_fetch_array($res) ) {
                        ?>




                            <!-- Post #1 -->
                            <li class="clear-fix">
                                <!-- Date -->
                                <div class="post-list-date">
                                    <div class="post-date-box">

                                    <?php 
                                            //Timestamp broken down to show accordingly
                                            $timestamp = $row['post_date'];
                                            $datetime = new DateTime($timestamp);
                                            $date = $datetime->format("d");
                                            $month = $datetime->format("M");
                                    ?> 
                                            <h3>    <?php echo $date; ?>    </h3>
                                            <span>  <?php echo $month; ?>   </span>

                                    </div>
                                </div>
                                <!-- /Date -->

                                <!-- Image + comments count -->
                                <div class="post-list-image">

                                    <!-- Image -->
                                    <div class="image image-overlay-url image-fancybox-url">
                                        <a href="post.php" class="preloader-image">
                                        <?php echo '<img src="', $row['image'], '" alt="' , $row['post_title'] , '\'s Blog Image" />'; ?>
                                        </a>
                                    </div>
                                    <!-- /Image -->
                                </div>
                                <!-- /Image + comments count -->
                                <!-- Content -->
                                <div class="post-list-content">
                                    <div>

                                        <!-- Header -->
                                        <h4> <a href="post.php"> <?php echo $row['post_title']; ?> </a> </h4>
                                        <!-- /Header -->
                                        <!-- Excerpt -->
                                        <p> <?php echo $row ['post_content'];  ?> </p>
                                        <!-- /Excerpt -->
                                    </div>
                                </div>
                                <!-- /Content -->
                            </li>
                            <!-- /Post #1 -->



                        <?php } // close while loop ?>

                        </ul>
                        <!-- /Posts list -->



                        <div><!-- Pagination -->

                            <ul class="blog-pagination clear-fix">

                                <?php 

                                //Count the number of rows
                                $numberofrows = mysql_query("SELECT COUNT(ID) FROM `wp_posts`");
                                //Do ciel() to round the result according to number of posts
                                $postsperpage = 4;
                                $numOfPages = ceil($numberofrows / $postsperpage); 


                                for($i=1; $i < $numOfPages; $i++) { 

                                    //echos links for each page
                                    $paginationDisplay = '<li><a href="blog.php?pn=' . $i . '">' . $i . '</a></li>';
                                    echo $paginationDisplay;


                                }



                                ?>
                                <!--
                                <li><a href="#" class="selected">1</a></li>
                                <li><a href="#">2</a></li>
                                <li><a href="#">3</a></li>
                                <li><a href="#">4</a></li>
                                -->                     

                            </ul>
                        </div><!-- /Pagination -->

                    <!-- /div> -->
                    <!-- Left column -->


                </div>
                <!-- /Layout 66x33 -->

            </div>

        </div>

        <?php require_once('include/twitter_user_timeline.php'); ?>
        <?php require_once('include/footer_blog.php'); ?>

提前致谢。

4

2 回答 2

1

在没有看到您的问题的代码的情况下,我估计逻辑是这样的..

在你的 latest_posts.php 上,你会做这样的事情(假设你有一个像 $post 这样包含 ID 和标题的变量,或者类似的模式):

<a href="blog.php?id=<?php echo $post["id"]; ?>"><?php echo $post["title"]; ?></a>

然后在 blog.php 上,您需要从 URL 中获取 ID 并以某种方式查找它。

<?php
$id = $_GET["id"];

$post = lookup_post_somehow($id);

if($post) {
  // render post
} else {
  // 404, blog post not found..
}
?>
于 2013-11-04T06:57:58.453 回答
0

你需要做一些谷歌搜索教程。:) 周围有一些很棒的东西。

您的博客希望在数据库中。这些数据库记录都应该有一个唯一的 ID。

在您的 latest_blogs 页面上添加 ?id= (只是一个示例)以在单击链接时传递 idover。

现在,在您的博客页面上,php 变量 $_GET['id'] 将包含博客 ID。

希望那些对你有帮助。

于 2013-11-04T06:56:43.413 回答