1

I'm new to WordPress, and I'm having an issue with "The Loop." I have 2 custom post type named 'book' and 'author' .in author post type I have custom filed checkbox which can choose between author and translator. also in book post type I have 2 metaboxs which user must choose the name of author and translator from those. all metabox and custom post type work well but when I want to call them and use the values of each meta-box I have problem. my code can read author values well, but translator values just show the last value of author and I cant' figure out why this happen ? I think foreach is the problem. but I don't know how can I solve it. here is my code for single-book.php

<?php $args = array( 'post_type' => 'book'); 
    $loop = new WP_Query( $args );
    while ( have_posts() ) : the_post();
// for reading author which choose from cheak box in each book pages.
    $post_id = get_the_ID(); 
    $key = 'save-author-to-book';
    $key2='save-trans-to-book';
    $vals=get_post_meta($post_id, $key2, true);
    $values = get_post_meta( $post_id, $key, true );
    $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    ?>
    echo '<h4 class="text-right color-style"> نویسنده : ';
    foreach($values as $value){
        $author=get_post($value);
        echo '<a href="'.
        get_post_permalink($value).'" target="_blank">'.
        $author->post_title .'</a> ، ' ;}
    echo '</h4>';

    echo '<h4 class="text-right color-style"> مترجم : ';
        foreach($vals as $val){
        $trans=get_post($val);
        echo '<a href="'.
        get_post_permalink($val).'" target="_blank">'.
        $author->post_title.'</a>، ';}

    echo '</h4>';

any idea would be appreciated.

  • the second 'foreach' loop should have this line: $trans->post_title.'</a>، ';} ... – Michael Aug 30 '16 at 3:36
1

I believe your issue is with $vals=get_post_meta($post_id, $key2, true);

If you check the codex, the last parameter for get_post_meta is whether to return a single value or array of values. You have set it to true which means return only one value.

Try it with false (the default) it should work.

  • it must be true because one book may have more than one translator and it return array then. – mkafiyan Aug 29 '16 at 7:06
0

I cant comment bynicolas answer, but I agree with him. If I understood correctly your issue, it is because your are using true at the end of:

$vals=get_post_meta($post_id, $key2, true);
$values = get_post_meta( $post_id, $key, true );

When you use true, from the Codex:

$single (bool) (Optional) Whether to return a single value.

Return (mixed) Will be an array if $single is false. Will be value of meta data field if $single is true.

You can't foreach a string, only arrays, hence why if you don't use false instead, your code won't work.

Or, if you only want the first result of the post_meta, keep using true, but then why do you use the foreach? You dont need it, you can simply use this:

if($value != '') {
    $author=get_post($value);
    echo '<a href="'.get_post_permalink($value).'" target="_blank">'.$author->post_title .'</a> ، ' ;
}
0

Careful on line 11:

    $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    ?>
    echo '<h4 class="text-right color-style"> نویسنده : ';

Do you use exactly this code? Because you close the PHP syntax exactly after getting the $feat_image and after that, the code continues with PHP syntax like echo.

I believe that the code breaks because of that little ?> and the first loop never ends.

  • no I didn't use exactly this code I close php after echo part :D I wrote it wrong here. – mkafiyan Aug 30 '16 at 4:16

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.