슬러그가 대시로 구분 된 페이지의 콘텐츠를 가져 오는 데 문제가 있습니다. 예를 들어 제가 www.example.com/home
로가는 경우 예를 들어 www.example.com/about-us
에 가면 에러가 발생합니다. Trying to get property of non-object
오류가이 라인에 관한 것입니다;슬러그 가져 오기 및 대시로 내용 쓰기
<h1><?php echo $page->Title; ?></h1>
<?php echo $page->Content; ?>
나는 다음과 같은 데이터베이스에서 슬러그를 얻는 함수를 가지고 있습니다.
function getSlug($name = null) {
global $db;
if ($name) {
$query = $db->prepare('SELECT * FROM pages WHERE `Title` = ? LIMIT 1');
$query->execute(array($name));
return $query->fetchObject();
}
}
또한 다음과 같은 슬러그를 만드는 함수가 있습니다.
function createSlug($string){
$string = preg_replace('/[«»""!?,[email protected]£$%^&*{};:()]+/', '', $string);
$string = strtolower($string);
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
return $slug;
}
내 htaccess에는 규칙 RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?name=$1 [L]
이 있습니다.
콘텐츠를 얻으려면 다음 코드를 사용하십시오.
<?php
include 'header.php';
$slug = create_slug($_GET['name']);
$page = getSlug($slug);
?>
<div class="container">
<h1><?php echo $page->Title; ?></h1>
<?php echo $page->Content; ?>
<?php include 'footer.php'; ?>
정말 문제가 무엇인지 전혀 알지 못합니다. 어떤 도움이라도 대단히 감사 할 것입니다.
미리 감사드립니다. 친절한 답변
감사합니다 @ 노력에 대한
덕분에, 내 htaccess로는 다음과 같다; ' \t한다 RewriteEngine \t # RewriteBase의 ON/ \t는 #/URL되는 index.php 다른 URL을 다시 \t한다 RewriteRule^([A-ZA-Z0-9 - /] +) $의 index.php? 이름 = $ 1 [L] \t 작성한 ErrorDocument 404 index.php를 ' –