首页 > 解决方案 > 来自数据库 ID 的元 OG 图像

问题描述

我有一个网站,有一个页面列出多个新闻,在单个新闻的详细页面中,我需要捕获 id_news 以放置单个新闻中的元 og 图像和元 og 标题。

试了很多代码都没有结果,总是显示黑页

我在页面顶部有这段代码:

<?php
$caatid=intval($_GET['cid']);

$fetch_sql = "SELECT titulonews, completonews, postimage FROM noticiaspt where id_news='$catid'";
while($fetch_row=mysql_fetch_array($fetch_result))
    {
    $titulonews = $fetch_row['titulinews'];
    $postimage = $fetch_row['postimage'];
    $completonews = $fetch_row['completonews'];
    ?>
    <meta property="og:title" content="<?php echo $titulonews; ?>" />
    <meta property="og:image" content="http://linktomysite.pt/admbo/postimages/<?php echo $postimage; ?>" />
    <meta property="og:description" content="<?php echo $completonews; ?>" /> <?php } ?>

但是,在正文中,使用正确 ID 请求新闻的代码可以正常工作......

这是我尝试的其他代码:

<?php

    $catid=intval($_GET['cid']);

    $query=mysqli_query($con,"Select id_news,titulonews,completonews,postimage from noticiaspt where Is_Active=1 and id_news='$catid'");
    while($row=mysqli_fetch_array($query))  {
    ?>
    <meta property="og:title" content="<?php echo htmlentities($row['$titulonews']); ?>" />
    <meta property="og:image" content="http://linktomysite/admbo/postimages/<?php echo htmlentities($row['$postimage']); ?>" />
    <meta property="og:description" content="<?php echo htmlentities($row['$completonews']); ?>" /> <?php } ?>

我找不到问题。有什么帮助吗?

标签: php

解决方案


有很多要谈的点

  1. 您希望当前页面有一个通知,但您正在使用 while() 最终将为您提供数据库返回的最后一行
  2. 这可能会破坏您的代码<?php } ?>

推荐阅读