相关帖子功能不起作用

时间:2017-03-06 作者:sergej

我已经建立了这个功能,理论上应该显示2个相关的职位,一些职位,但它使我的网站变成空白。

<?php
$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
\'tag__in\' => $tag_ids,
\'post__not_in\' => array($post->ID),
\'posts_per_page\'=>2, // Number of related posts to display.
\'caller_get_posts\'=>1
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>

    <li class="full entry animate-box ll-article" style="background-image: url(\'<?php set_featured_image_bg(); ?>\');" data-animate-effect="fadeIn">
        <div class="overlay"></div>
        <div class="entry-desc">
            <div class="border-police">
            <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
            </div>
        </div>
    </li>

<? }
}
$post = $orig_post;
wp_reset_query();
?>

1 个回复
最合适的回答,由SO网友:nibnut 整理而成

恐怕最终的答案将出现在您的服务器日志中。(您还可以在wp config中打开调试-有时也会有帮助)

但我确实注意到了一些可以帮助您跟踪问题的事情:

<?php
// You should not require the $orig_post variable...
global $post;
// Get the tag ids directly:
$tag_ids = wp_get_post_tags( $post->ID, array( \'fields\' => \'ids\' ) );
if (!empty($tag_ids)) {
$args=array(
\'tag__in\' => $tag_ids,
\'post__not_in\' => array($post->ID),
\'posts_per_page\'=>2, // Number of related posts to display.
\'caller_get_posts\'=>1
);
$my_query = new WP_Query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
// **** What is the "set_featured_image_bg" function? Is it defined?
?>

    <li class="full entry animate-box ll-article" style="background-image: url(\'<?php set_featured_image_bg(); ?>\');" data-animate-effect="fadeIn">
        <div class="overlay"></div>
        <div class="entry-desc">
            <div class="border-police">
            <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
            </div>
        </div>
    </li>

<? }
    wp_reset_postdata(); // use this instead of "wp_reset_query", and right after you end your while loop...
}
?>
祝你好运!

相关推荐

Shortcode to pull posts

我需要一些帮助。我创建了一个短代码来引入帖子,然后使用ajax加载更多帖子。我的一切都正常,但它复制了第一组帖子。下面是我的代码。这是shortcode函数:add_shortcode( \'articles-grid\', \'articles_grid\' ); function articles_grid( $atts ) { $showdate = $showauthor = $post_meta = $post_author = $post_seperator