What are the ways to implement the opening of the post without the use of a redirect? The idea is as follows: Category -> Subcategory (if there is only one post in subcategory - open this post when clicked) -> Post.

Here is my code that works with JS and redirects to the desired page as it was intended.

<?php } }elseif (count($posts) == 1) { $i=1; foreach($posts as $cpost) { $number = $pr_id = sprintf("%02d", $i); $sub_newlink = get_permalink($cpost->ID); echo "<script type=\"text/javascript\">document.location.href = '".$sub_newlink."';</script>"; exit; } ?> 

How can this be done only with PHP and without redirection? What would the first subcategory inherit from the link of this post? Now the link of the 2 sub categories looks like this. How can it be screwed to the 1st?

  <a href="<?php echo get_permalink($cpost->ID) ?>" class="wk_link" title="<?php echo($cpost->post_title); ?>"> 

Now my code works, but it happens as follows: 1. The subcategory has a link to the subcategory trail and only then redirect to the post from this subcategory.

Implementation 2:

 elseif (count($posts) == 1) { $i=1; print_r($posts[0]->ID); 

    2 answers 2

    What would the first subcategory inherit from the link of this post?

    No Slug in VP - a unique thing.

    • Added "implementation 2" to my code. Now it works correctly and the post ID is displayed without a redirect. But how can I get the full content of the post? - Isabella Monza

    With the help of PHP, a redirect is done only in the HTTP header. But in order for it to work it is impossible to make any output like echo or print and so on before calling it.

    In other words: check the expression first, then, if necessary, do

     header('Location: http://www.example.com/'); 

    And only then do the output tags, scripts, and more.

    • one
      Without knowing the VP - do not need to give such advice. Especially since this is not the answer to the question. - SeVlad