WordPress.org

Forums

Pods - Custom Content Types and Fields
[resolved] error on code (9 posts)

  1. moin shaikh
    Member
    Posted 1 year ago #

    hi i have used this code in search.php template it works perfectly but when i put this code in 404.php template a warning is shown .

    Invalid argument supplied for foreach()

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    										<?php
    // call your taxonomy as a pod because when you extend it, it makes it a pod object
    $store = pods( 'store' );
    // Get terms for post using typical wordpress terms loop
    $terms = get_the_terms( $post->ID , 'store' );
    // Loop over each item since it's an array
    foreach ( $terms as $term ) {
    //do a typical fetch to get your pod fields
    $store->fetch( $term->term_id );
    $store_logo = $store->get_field('store_logo');
    $store_currency = $store->get_field('store_currency');
    $store_permalink = $store->get_field('permalink');
    }
    ?>

    please help me out.
    thanks.

    https://wordpress.org/plugins/pods/

  2. Josh Pollock
    Member
    Plugin Author

    Posted 1 year ago #

    The problem here is this line $terms = get_the_terms( $post->ID , 'store' ); which is not giving you a valid post ID. Because you are on the 404 template, the $post object is not populated.

  3. moin shaikh
    Member
    Posted 1 year ago #

    how can i Solve this issue.
    i Am just a php learner.

    thanks

  4. Josh Pollock
    Member
    Plugin Author

    Posted 1 year ago #

    You need to use some other way of getting a valid ID into get_the_terms(). That really depends on which post's terms you want to show on the 404. You could hardcode an ID there, if you had a specific post you wanted to show the terms in this situation.

  5. Scott Kingsley Clark
    Member
    Plugin Author

    Posted 1 year ago #

    Also, use field / display as documented below, instead of get_field:

    http://pods.io/docs/code/pods/field/
    http://pods.io/docs/code/pods/display/

  6. Scott Kingsley Clark
    Member
    Plugin Author

    Posted 1 year ago #

    Replace $post->ID usage with get_the_ID()

  7. moin shaikh
    Member
    Posted 1 year ago #

    get_the_ID() is not working its showing same error.

  8. Josh Pollock
    Member
    Plugin Author

    Posted 1 year ago #

    In any case but the 404 template, get_the_ID() is better to use than $post->ID for the code you showed us. On the 404 template, neither is going to work, since there is no post ID set on a 404 template.

    You must choose a specific post ID to use to get the terms from in this template and hardcode its value.

  9. moin shaikh
    Member
    Posted 1 year ago #

    thanks.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags