简体   繁体   English

如何从首页新闻页面重定向到单个新闻页面

[英]how to redirect to single news page from home news page

I am having a project to display news on a wordpress site. 我有一个项目在wordpress网站上显示新闻。 I have sucessfully created a home page which displays the news. 我已经成功创建了一个显示新闻的主页。 an archive page which displays all news on a page. 在页面上显示所有新闻的存档页面。

now i want to redirect the link to single page where the clicked news is diplayed 现在我想将链接重定向到显示了单击的新闻的单个页面

This ma home page , archive page and single page 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();
 ?>

我的工作

@Jackson, @杰克逊

Replace the below code with @Magnus's suggestion: 将以下代码替换为@Magnus的建议:

<a href = '".site_url()."/" .$postname."'>" .$newstitle. "</a>

to

<a href='" .get_permalink( $newsid ). "'>" .$newstitle. "<a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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