【问题标题】:WordPress Blog Post "Read More" Link Not Taking Me To Actual PostWordPress 博客帖子“阅读更多”链接不带我进入实际帖子
【发布时间】:2017-09-10 07:40:35
【问题描述】:

这里的很多文章似乎都在处理“阅读更多”链接不显示的问题。我的显示很好,只是实际上并没有将我们带到显示完整博客文章的页面。您可以在此处查看它的显示方式(阅读更多>>):

现在当我点击“阅读更多>>”时,我明白了:

在我的代码中,在 home.php 中,我显示的博客文章如下:

<?php 
$paged = ( get_query_var('page') ) ? get_query_var('page') : '1';
$args = array('post_type' => 'post', 'posts_per_page' => 3, 'order' => 'ASC', 'orderby' => 'title', 'paged' => $paged);
$posts = new WP_Query($args);
if ($posts->have_posts()):  while ($posts->have_posts()) : $posts->the_post(); 
    get_template_part( 'content', get_post_format());
    echo "<div class='thepost'><p class='post-date'>".get_the_date()."</p>"; 
    echo "<h2 class='post-title'><a href=".get_permalink().">".get_the_title()."</a></h2>";
    //echo "<span class='post-image'>".the_post_thumbnail()."</span>";
    the_post_thumbnail('post-thumbnail', ['class' => 'post-image']);
    echo "<p class='post-paragraph'>".get_the_excerpt()."</p>"; 
    echo "<p><a href=".get_permalink()."><span class='read-more'>Read More >></span></a></p> </div>";

    endwhile;?>

那么我需要对此行进行哪些更改:echo "&lt;p&gt;&lt;a href=".get_permalink()."&gt;&lt;span class='read-more'&gt;Read More &gt;&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;";,以便它链接到用户点击的完整帖子?

我已经浏览了the codex,了解如何自定义阅读更多链接,但我只是找到了

“然后,用户可以继续阅读更多内容,因为您已通过摘要介绍吸引他们,方法是单击指向全文的链接。主题通常在标题中包含此链接,上述方法默认情况下会在您的预告片后面生成它。”

我在阅读法典时一定遗漏了一些东西。谢谢!

编辑:检查链接:

EDIT 2 检查损坏的页面:

编辑 3:完整的 CSS (style.css):https://pastebin.com/TaUnemz9

EDIT 4个人博客文章页面的完整截图:

【问题讨论】:

  • 为了清关,您的阅读更多链接没有将用户发送到相应的帖子?你能把网站链接贴在这里吗?
  • @RajKumarBhardwaj 我正在本地开发,所以我认为粘贴链接不会有帮助。但是,是的,这是我的问题。
  • 根据另一位开发人员的说法,我的 html 没有任何问题。结果页面刚刚损坏了 CSS。我在上面的编辑中粘贴了指向我的完整 CSS 的链接。
  • 清除浏览器缓存以加载新的 css。您应该能够从您发送到的 URL 判断链接是否正确。如果那是正确的,那只是css,似乎是这样的。
  • 检查文件 page.php 和 single.php 是否有后循环代码

标签: php wordpress posts custom-wordpress-pages


【解决方案1】:

首先,如果没有任何适当的链接,我无法告诉您确切的问题是什么。但是当我仔细查看您的屏幕截图时,我发现了

你会得到像http://localhost/fourth-blog-post这样的链接 当您单击阅读更多按钮时。

但应该是这样的http://localhost/blog/fourth-blog-post

因为它是您所有帖子的单一帖子,并且它来自 single.php 文件, 现在您可以做两件事,首先通过您的帖子出现的管理面板检查您的帖子的网址。第二个,如果它与我在上面链接中提到的第二个 url 匹配。那么你应该在 get_permalink() 的地方尝试使用 _permalink() 函数来获取更多信息。

https://codex.wordpress.org/Function_Reference/the_permalink

因为 get_permalink() 不应该在循环中使用。它应该是循环中的 the_permalink()。

希望对你有帮助:)

【讨论】:

  • 是的,我将代码从 home.php(博客页面)复制并粘贴到了 single.php,但我的问题仍然是链接。 “阅读更多”的链接 - 该页面的 URL 类似于 http://localhost/hello-world/。我不知道为什么它会去“hello-world”而不是http://localhost/blog/fourth-blog-post。请参阅上面的编辑以获取页面的完整屏幕截图。您还会注意到它正在显示所有博客文章。所以这意味着在 single.php 中的某个地方,我需要进行调整,以便只显示选定的帖子。这是我的 single.php 文件的链接:pastebin.com/7LkJN6TY
  • 让我检查一下,给我一点时间,我会回复你的。
  • 兄弟,您的代码无法正常工作,因为一开始您在第 1 行注释了 while 循环。 8 //while (have_posts()) : the_post();删除双斜线,您不需要在 single.php 文件中进行自定义查询,因为它采用默认循环并且仅在 single.php 上获得一篇帖子。为什么要在 single.php 文件中添加自定义查询
  • 因为你评论了 endwhile 以及这就是你得到这个错误的原因。在线编号230
  • 你有没有在get_permalink()的地方试试_permalink()函数;
【解决方案2】:

首先,关键是单个帖子页面根据WordPress Hierarchy通过single.php显示。

所以我需要创建一个 single.php 文件——这个文件的大部分代码仍然与 home.php 的代码相同——它仍然会显示标题、英雄图像、导航菜单、页脚,内容仍将包含在&lt;div&gt; 中。

不同的是我需要把这个拿出来:

$paged = ( get_query_var('page') ) ? get_query_var('page') : '1';
$args = array('post_type' => 'post', 'posts_per_page' => 3, 'order' => 'ASC', 'orderby' => 'title', 'paged' => $paged);
$posts = new WP_Query($args);

然后将 WordPress 循环从:if ($posts-&gt;have_posts()): while ($posts-&gt;have_posts()) : $posts-&gt;the_post(); 更改为:if (have_posts()): while (have_posts()) : the_post();。我们已经加载了帖子,现在我们只需要显示它们。

根据显示单个帖子与显示多个帖子的不同,我需要获取/回显的任何 WP 功能都会发生变化。我可以拿出来:

        get_template_part( 'content', get_post_format());
        echo "<p class='post-paragraph'>".get_the_excerpt()."</p>"; 
        echo "<p><a href=".get_permalink()."><span class='read-more'>Read More >></span></a></p> </div>";

因为我不再需要它们了。

关键是链接在 WordPress 中的工作方式与基本 HTML 不同。它不像在 HTML 中,如果您有 &lt;a href="post-page.html"&gt;Read More&lt;/a&gt;,当您单击阅读更多时,您将被带到 post-page.html。由于它是一个 WP 站点,它使用 PHP,并且您使用 WP 函数 .get_permalink(). 将永久链接包装在 &lt;a&gt; 标记中。然后永久链接会将您带到 single.php。

【讨论】:

    猜你喜欢
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    相关资源
    最近更新 更多