how to redirect to single news page from home news page

My Work

I am having a project to display news on a wordpress site. 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();
 ?>

Read more here: how to redirect to single news page from home news page

Leave a Reply

Your email address will not be published. Required fields are marked *