0

I have set up this function which in theory should display 2 Related Posts to some Post, but it makes my site go blank.

<?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

The ultimate answer will be in your server logs, I'm afraid. (You can also turn on debugging in wp-config - sometimes that helps too)

But I did notice a few things that hopefully can help you track the issue:

<?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...
}
?>

Good luck!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.