• I’m trying to get WordPress to print out an attribution from a custom field if the post is in a category of “news”, and not otherwise. So the logic would be, “If category = “news,” print attribution (e.g., New York Times) and number of comments. Else, print only number of comments.”

    I tried this, but it didn’t work:

    <?php
       if ( in_category( '3' )) {
          echo 'From <?php if ( function_exists( 'get_custom_field_value' ) ) get_custom_field_value( 'Attribution', true ); ?> | <?php comments_popup_link('No comments', '1 comment ?', '% comments ?'); ?>';
       } else {
          echo '<?php comments_popup_link('No comments', '1 comment ?', '% comments ?'); ?>'
       }
    ?>

    …but then, not being a PHP’er, I didn’t think it would. But I hope someone can tell from the kludge above what the intent was.

    Another way to do this, I think, would be to simply test if the custom field exists, and print out different lines accordingly. But I don’t know how to do that, either.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Try:

    <?php if ( in_category( '3' )) echo 'From ' . get_custom_field_value( 'Attribution', true ) . '|' ;
    echo '<?php comments_popup_link('No comments', '1 comment ?', '% comments ?'); ?>

    Thread Starter MtnExile

    (@mtnexile)

    That gave me a parse error:

    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'

    I also tried this

    <?php if (is_category('3') ) { ?>
        <p class="attribution">From the <?php if ( function_exists( 'get_custom_field_value' ) ) get_custom_field_value( 'Attribution', true ); ?> | <?php comments_popup_link('No comments', '1 comment &raquo;', '% comments &raquo;'); ?></p>
    <?php } elseif (is_category('-3') ) { ?>
        <p class="attribution"><?php comments_popup_link('No comments', '1 comment &raquo;', '% comments &raquo;'); ?></p>
    <?php } ?>

    and that didn’t throw an error, but it also didn’t work. I doesn’t print out the custom field value.

    Sorry – I left some of your bad code in there:

    <?php if ( in_category( '3' )) echo 'From ' . get_custom_field_value( 'Attribution', true ) . '|' ;
    comments_popup_link('No comments', '1 comment ?', '% comments ?'); ?>
    Thread Starter MtnExile

    (@mtnexile)

    That’s functioning correctly–by which I mean, it’s drawing from the custom field if the post is in category ‘3’, and printing only the comment count otherwise. But it’s also doing something screwy, and danged if I can figure out why.

    You have ‘From ‘ preceding the custom field value; but it’s printing out like this:

    The New York TimesFrom |15 comments ?

    Not sure how you’re storing these custom values but try:

    <?php if ( in_category( '3' )) echo 'From ' . get_post_meta( $post->ID, 'Attribution', true ) . '|' ;
    comments_popup_link('No comments', '1 comment ?', '% comments ?'); ?>
    Thread Starter MtnExile

    (@mtnexile)

    Thanks, that did it.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Need help with syntax of conditional category statement’ is closed to new replies.