Post link shows ID instead of title on the URL


Joined
Jan 13, 2023
Messages
2
Reaction score
0
I'm modifying a PHP blog for a project and realized that a post URL shows ID instead of title of the post. How can I correct this?

Assuming I'm not advanced at PHP coding, is there a script I can use to make correction without tempering the database?
 
Ad

Advertisements

Joined
Mar 11, 2022
Messages
225
Reaction score
29
Hold on, i'll be there for yo........shi.....t. I fell and broke my crystal ball. Seriously??? Okay. You're not advanced at PHP (i wonder why you start with projects without any knowledege). Anyway. How's your current code look like? There is tons of ways to do things. No, there is no "is there a script I can use to make correction" out there without knowing what exactly is going on.
 
Ad

Advertisements

Joined
Jan 30, 2023
Messages
71
Reaction score
2
Yes, you can create a script to modify the URL to show the title of the post instead of the ID. Here's a basic idea of how to do it:

  1. Fetch the title of the post based on the ID
  2. Clean the title to remove any special characters and spaces
  3. Use the cleaned title as the part of the URL
Here's a sample code that does the above steps:

PHP:
<?php
// fetch the post title based on the ID
$post_id = $_GET['id'];
$post_title = fetch_post_title_by_id($post_id);

// clean the title
$post_title = strtolower(str_replace(' ', '-', $post_title));
$post_title = preg_replace('/[^A-Za-z0-9\-]/', '', $post_title);

// use the cleaned title as part of the URL
$post_url = "/post/$post_id-$post_title";

This is just a basic example and you may need to modify it to suit your needs. Additionally, you may need to update your routing to handle the new URLs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top