0

I am new to this and I don't know php. I get this error on WordPress posts:

Warning: count(): Parameter must be an array or an object that implements Countable in /www/tastingvictoria_289/public/wp-content/themes/astra-child/single.php on line 32 Which where if(count($related_article) > 0){?> is.

Here is the file it is coming from. Is it a syntax thing?

Any cue? Thanks

    <div id="primary" <?php astra_primary_class(); ?>>
        <?php astra_primary_content_top(); ?>
        <?php
        $related_article = get_field( "select_article", 'option' );
        if(count($related_article) > 0){
        ?>
                <?php
                    ?>
                    <div class="article">
                        <div class="wrap">
                            <a href="<?php echo $link; ?>">
                                <div class="article-image" style="background-image: url(<?php echo $post_thumb; ?>);"></div>
                            </a>
                            <div class="infos">
                                <div class="infos-wrap">
                                    <p class="date"><?php echo $postdate; ?></p>
                                    <a href="<?php echo $link; ?>"><h4 class="title"><?php echo $title; ?></h4></a>
                                    <p class="excerpt"><?php echo $excerpt; ?></p>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php
                }
                ?>
        <?php   
        ?>

    </div><!-- #primary -->

<?php if ( astra_page_layout() == 'right-sidebar' ) : ?>

    <?php get_sidebar(); ?>

<?php endif ?>

1 Answer 1

1

Explanation:

The count() Method Counts all elements in an array when used with an array. the condition: count($related_article) > 0 checks if the array is not null.

therefore the variable $related_article must be an array. php recognizes thats not an array and throws an error. its not a syntax error but a missuse of the count() method.

php documentaion - https://www.php.net/manual/en/function.count.php

solution

to fix this error i would try to change code count($related_article) > 0 to that isset($related_article)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.