我有一个在wordpress网站上显示新闻的项目。我成功创建了一个显示新闻的主页。存档页面,显示页面上的所有新闻。
现在我想将链接重定向到点击新闻的单页
这个ma主页,存档页面和单页respectivley。
<?php
get_header()
?>
<h3><a href="<?php echo site_url().'/news'?>">News</a></h3>
<?php
global $post;
$args = array(
'post_type' => 'news',
'numberposts' => '2',
);
$news = wp_get_recent_posts($args,false);
echo "<div class='row'>";
foreach( $news as $row)
{
$newsid=$row->ID;
$newstitle=$row->post_title;
$image = get_post_meta($newsid,'_thumbnail_id',true);
// var_dump($image);
// die;
echo "<div class= 'col-lg-4'><table style='width:200px;'><tr><td><img src='". wp_get_attachment_image_src($image)[0]."'style='width:100px;'></td></tr>";
$postname=$id->post_name;
// // //$newstitle=get_the_title($id);
// // $post_content = get_the_title($id);
// // $newsbrief = $post_content->post_content;
echo "<div class= 'col-lg-4'><table style='width:250px;'><tr><td style ='font-weight:bold'><a href = '".site_url()."/" .$postname."'>" .$newstitle. "</a></td></tr>";
$author=get_post_meta( $newsid, 'Author',true);
echo "<tr><td>".$author."</td>";
$newsbrief=$row->post_content;
echo "<tr><td>".$newsbrief."</td></tr></table></div>";
}
echo "</div>";
?>
<?php
get_footer();
?>
Archive Page
<?php
get_header()
?>
<?php
global $post;
$args = array(
'post_type' => 'news',
'post_status' => 'publish',
);
$news = wp_get_recent_posts($args,false);
echo "<div class='row'>";
foreach( $news as $row)
{
$newsid=$row->ID;
$newstitle=$row->post_title;
$image = get_post_meta($newsid,'_thumbnail_id',true);
echo "<div class= 'col-lg-4'><table style='width:200px;'><tr><td><img src='". wp_get_attachment_image_src($image)[0]."'style='width:100px;'></td></tr>";
echo "<div class= 'col-lg-4'><table style='width:200px;'><tr><td>" .$newstitle. "</td></tr>";
$author=get_post_meta( $newsid, 'Author',true);
echo "<tr><td>".$author."</td>";
$newsbrief=$row->post_content;
echo "<tr><td>".$newsbrief."</td></tr></table></div>";
}
?>
<?php
get_footer();
?>
single page
<?php
get_header();
?>
<?php
global $post;
$id= $post->ID;
$newstitle=$post->post_title;
$author=get_post_meta( $id, 'Author',true);
$desc =get_post_meta( $id,'Description', true);
echo "<h3>".$newstitle."</h3>";
echo "<h4".$author."</h4>";
echo $desc;
?>
<?php
get_footer();
?>
答案 0 :(得分:2)
@Jackson,
将以下代码替换为@ Magnus的建议:
<a href = '".site_url()."/" .$postname."'>" .$newstitle. "</a>
到
<a href='" .get_permalink( $newsid ). "'>" .$newstitle. "<a>