我有一个页面显示博客帖子[latest_posts.php]和另一个显示单个博客帖子的页面[blog.php]。我打算在latest_posts.php中链接图像标题,以便它重定向到blog.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'); ?>
blog.php ---&gt;
<?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'); ?>
先谢谢。
答案 0 :(得分: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..
}
?>
答案 1 :(得分:0)
你需要做一些谷歌搜索教程。 :)周围有一些很棒的东西。
您的博客有望在数据库中存储。这些数据库记录应该都有唯一的ID。
在您的latest_blogs页面上添加?id =(仅作为示例)以在单击链接时传递idover。
现在,在您的博客页面上,php变量$ _GET ['id']将包含博客ID。
希望有所帮助。