我有一个页面显示博客文章[latest_posts.php],另一个页面显示单个博客文章[blog.php]。我打算在latest_posts.php中链接图像标题,以便它重定向到博客.php在那里它将显示单击的特定帖子。
latest_posts.php ---->
<div class="main blog">
<!-- Header -->
<h2 class="underline">
<span>What'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'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'); ?>
提前谢谢。
在没有看到您的问题的代码的情况下,我估计逻辑是这样的。
在你的latest_posts.php你会做这样的事情(假设你有一个像$post这样的变量,其中包含ID和标题,或者类似的模式):
<a href="blog.php?id=<?php echo $post["id"]; ?>"><?php echo $post["title"]; ?></a>
然后在博客上.php您需要从URL中获取ID并以某种方式查找它。
<?php
$id = $_GET["id"];
$post = lookup_post_somehow($id);
if($post) {
// render post
} else {
// 404, blog post not found..
}
?>
你需要在谷歌上搜索教程。 :) 周围有一些很棒的东西。
您的博客有望在数据库中。 这些数据库记录应各自具有唯一的 ID。
在您latest_blogs页面上添加 ?id=(只是一个示例)以在单击链接时传递 idover。
现在,在您的博客页面上,php 变量 $_GET['id'] 将包含博客 ID。
希望这有所帮助。