John Davidson

php - Wordpress how to get a single post link

0 comments
Message:


I want to go to single.php file when this button is clicked.


<div class="recepies-search">

<div class="card m-1" style="width: 18rem;">
<img class="card-img-top" src="<?get_the_post_thumbnail()?>" >
<div class="card-body">
<h5 class="card-title"><?php the_title();?></h5>
<button type="button" class="btn btn-warning" onclick="<?php get_permalink()?>">click</button>

</div>
</div>
</div>

but it doesn't do anything.



My Answer:
To get a single post link in WordPress and go to the single.php file when a button is clicked, you can use the following code:

1. In your WordPress theme, create a button in your template file (e.g., index.php, page.php, etc.) with a link to the single post:

html
Read More


2. Add the following code to your single.php file to display the single post content:

php
while (have_posts()) : the_post();
the_title('

', '

');
the_content();
endwhile;
?>


3. Add some CSS to style the button:

css
.button {
display: inline-block;
padding: 10px 20px;
background-color: #007bff;
color: #fff;
text-decoration: none;
border-radius: 5px;
}


Now, when the "Read More" button is clicked on the index.php or any other template file, it will take the user to the single post page (single.php) where the full post content will be displayed.

Rate this post

3 of 5 based on 3606 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us