【问题标题】:Advanced Custom Fields Related Posts高级自定义字段相关文章
【发布时间】:2016-04-05 06:46:52
【问题描述】:

我一直在使用高级自定义字段插件在帖子内容下方显示相关帖子。昨天我添加了 Supsystic 的社交分享插件,以在我的帖子上显示分享按钮,并且效果很好。

问题是当我点击分享按钮在 Facebook 或其他社交媒体上分享帖子时,它会从相关帖子字段中获取帖子。

例如,当我单击标题为“Google Chrome 功能”的帖子上的共享按钮时,facebook 共享框架会打开,但框架中显示的帖子是“Firefox 功能”。但是,“Firefox 功能”位于“Google chrome 功能”帖子下方的相关帖子字段中。任何帮助都值得赞赏。谢谢。

【问题讨论】:

  • 您好,欢迎来到 SO 论坛。您必须向我们展示一些代码。
  • 感谢 Zvan 这么快的反应!
  • 需要看code post.single.php代码吗?
  • 您可以用您的(格式化的)代码编辑问题,而不是将其添加为评论吗?目前尚不清楚(例如)//End for each loop wp_reset_postdata(); 是一行还是两行(即 wp_reset_postdata(); 调用是否被注释掉了
  • 抱歉,没有看到您的更新 - 我的意思是编辑您的问题,而不是添加答案。您是说插件在其下添加代码共享,并且链接错误?如果您将global $post; 放在该代码的顶部,会发生什么(a)?或者 (b) 将变量 $posts 重命名为 $related_posts$post 重命名为 $related_post?可能是全局变量被破坏的问题。

标签: wordpress advanced-custom-fields


【解决方案1】:

post.single.php 中的高级自定义字段相关帖子代码

<?php  
$posts = get_field('related_posts'); 
if ($posts) { ?> 
    <h3 class="related">Related posts</h3>
    <p>
    <?php foreach($posts as $post) { setup_postdata($post); ?>
        <li><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php } //End for each loop
    wp_reset_postdata(); //Restores WP post data ?>
    </p>
    <hr />
 <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php } //End if ?>

【讨论】:

  • 你好流浪汉,有什么问题吗?
猜你喜欢
  • 2017-12-16
  • 2013-07-14
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 2017-08-28
  • 2018-08-06
  • 2013-11-25
  • 2012-08-10
相关资源
最近更新 更多