0

I'm trying to run a functions to get related posts by tag, i've been getting a unexpected end of file error and after some debugging i found out that it's this query causing the error. I've tried lots of solutions but can't seem to fix the issue !

This is the query that is placed in the beginning of my single.php file

$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args_related=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'posts_per_page'=>10,
'caller_get_posts'=>1
);
$related_articles_query = new WP_Query($args_related);

This is how i'm using it in the template

      <div class="cards display--flex ">
                <?php if ($related_articles_query->have_posts()) : ?>
                <?php while ($related_articles_query->have_posts()) : $related_articles_query->the_post(); ?>

                <!-- =============================== -->
                <!-- Card Template -->
                <div class="card">
                    <?php if(has_post_thumbnail( ))  : ?>

                    <div class="card__image display--flex">
                        <!--imagem -->
                        <?php the_post_thumbnail( ); ?>
                    </div>
                    <!-- end-magem -->
                    <?php else : ?>
                    <!-- none -->
                    <div class="card__image__none">
                    </div>
                    <?php endif;?>
                    <!-- end-none -->
                    <div class="card__container">
                        <p class="card__container__subtitle">
                            <?php echo get_the_date(); ?>
                        </p>
                        <h2 class="card__container__title">
                            <a href="<?php the_permalink(); ?>">
                                <?php the_title(); ?>
                            </a>
                        </h2>
                    </div>
                 <!--   -->
                </div>
                <!--card  -->
                <?php endwhile; wp_reset_query();  ?>
                <!-- =============================== -->
                <?php else : ?>
                <p><?php __('No Page Found'); ?></p>
                <?php endif; ?>
            </div> <!-- Card container-->

This is inside a single article and i'm creating a slider that has the related posts using glider.js , thats why i need the related articles

  • 1
    where did you close if ($tags) { in code ?? – Chetan Vaghela Oct 8 '19 at 11:56
1

You arent closing your if tag:

$tags = wp_get_post_tags($post->ID);
if ($tags) {
    echo 'Related Posts';
    $first_tag = $tags[0]->term_id;
    $args_related = array(
        'tag__in' => array($first_tag), 
        'post__not_in' => array($post->ID), 
        'posts_per_page' => 10,
        'caller_get_posts' => 1
    );
    $related_articles_query = new WP_Query($args_related);
}

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.