wp query - Getting the post terms 'wp_get_post_terms' per post when within the functions.php file

I have used a form with an Ajax action to pull post information through on submit.It works a charm. However, I have used

I have used a form with an Ajax action to pull post information through on submit.

It works a charm. However, I have used categories to separate the work into categories. One of which is the brand.

Within the page template the script I use works, but when used in the functions.php file. It doesn't pull the required results through.

I think it may be something to do with when the query to the post is triggered or how the add_action is set up.

Can someone please help me pull through the name of the category found within the ID of 31? When used in the functions.php.

Here is what I have written:

if( $query->have_posts() ) :
    while( $query->have_posts() ): $query->the_post();

        echo "<div class=\"portfolio-piece\" style=\"background-image: url(" . get_the_post_thumbnail_url() . ");\">";

            echo "<a href=\"" . get_the_permalink() . "\" class=\"box-link\"></a>";

            echo "<div class=\"portfolio-piece-hover\">";

            echo "</div>";

            echo "<div class=\"portfolio-piece-inner\">";

                echo "<h5 class=\"portfolio-tags\">" . the_tags('', ' / ', '') . "</h5>";
                echo "<h4 class=\"portfolio-title\">";
                echo the_title();
                echo "</h4>";

                echo "<h4 class=\"portfolio-company\">";

                    $brands = wp_get_post_terms( $post->ID, 'category', array(
                        'orderby' => 'name',
                        'order' => 'DESC',
                        'parent' => 31
                    ) );
                    foreach ( $brands as $brand ) {
                        echo $brand->name;
                    };

                echo "</h4>";

            echo "</div>";

        echo "</div>";

    endwhile;
    wp_reset_postdata();

Where literally everything works but the portfolio-company / $brands = wp_get_post_terms part.

Thanks, Jason.

I have used a form with an Ajax action to pull post information through on submit.

It works a charm. However, I have used categories to separate the work into categories. One of which is the brand.

Within the page template the script I use works, but when used in the functions.php file. It doesn't pull the required results through.

I think it may be something to do with when the query to the post is triggered or how the add_action is set up.

Can someone please help me pull through the name of the category found within the ID of 31? When used in the functions.php.

Here is what I have written:

if( $query->have_posts() ) :
    while( $query->have_posts() ): $query->the_post();

        echo "<div class=\"portfolio-piece\" style=\"background-image: url(" . get_the_post_thumbnail_url() . ");\">";

            echo "<a href=\"" . get_the_permalink() . "\" class=\"box-link\"></a>";

            echo "<div class=\"portfolio-piece-hover\">";

            echo "</div>";

            echo "<div class=\"portfolio-piece-inner\">";

                echo "<h5 class=\"portfolio-tags\">" . the_tags('', ' / ', '') . "</h5>";
                echo "<h4 class=\"portfolio-title\">";
                echo the_title();
                echo "</h4>";

                echo "<h4 class=\"portfolio-company\">";

                    $brands = wp_get_post_terms( $post->ID, 'category', array(
                        'orderby' => 'name',
                        'order' => 'DESC',
                        'parent' => 31
                    ) );
                    foreach ( $brands as $brand ) {
                        echo $brand->name;
                    };

                echo "</h4>";

            echo "</div>";

        echo "</div>";

    endwhile;
    wp_reset_postdata();

Where literally everything works but the portfolio-company / $brands = wp_get_post_terms part.

Thanks, Jason.

Share Improve this question asked Mar 19, 2019 at 11:46 Jason Is My NameJason Is My Name 3782 gold badges7 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

(Revised answer)

So I'm assuming that there are actually posts in the category (ID: 31), and based on the wp_get_post_terms() reference:

$post_id
(int) (Optional) The Post ID. Does not default to the ID of the global $post. Default 0.

It's likely that you're not passing the proper post ID to the wp_get_post_terms() function:

wp_get_post_terms( $post->ID, 'category', array(...) );

In that code, you have $post and yet, it's not defined anywhere in your code as in the question.

So if you're trying to get the ID of the current post in the WordPress Loop, where the post data is stored in the global $post variable, then you should first make the variable accessible in your function/code using global $post; like so:

global $post;
// here and after this line, you may now use $post->ID
if( $query->have_posts() ) :
...
endif;

Or you could also use get_the_ID() without having to do global $post;, particularly if all you wanted is to get the post ID:

wp_get_post_terms( get_the_ID(), 'category', array(...) ); // here I don't use $post->ID

And it's probably worth mentioning that in standard templates such as page.php, the global $post variable is already "global-ed", hence you may access the $post anywhere in the template. (Well, not exactly "anywhere"; however, I'm not going to dig deeper into that in this answer..)

Additional Notes

This is probably just a typo, but in the code in the question, you got a syntax error here:

foreach ( $brands as $brand ) {
  echo $brand->name;
}; // <- remove that ;

And you forgot the closing endif;..

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745675409a4639658.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信