WordPress.org

Ready to get started?Download WordPress

Forums

Conditional tag in custom field (3 posts)

  1. stemie
    Member
    Posted 2 years ago #

    Hi,

    Im trying to get a thumbnail from a custom post type to display on a particular page/post etc. depending on the conditional tag that has been included in the custom field of that post.
    For example is_home() is_page(22) etc.

    I can get the thumbnail to appear on all the pages but when I use an if statement with get_post_meta conditional tag it doesnt work

    <?php if ( get_post_meta($post->ID, 'ad-location', true) ) {  ?>
    global $post;
    $args = array( 'numberposts' => 1, 'post_type' => 'advert' );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post);
    	the_post_thumbnail('header1-ad');
    endforeach;
    <?php } ?>

    Ive been using simple ad manager but its overboard for what I need

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    depending on the conditional tag that has been included in the custom field of that post.

    what is the content of the custom field?

    (your if statement is true if anything at all is in the custom field)

    it doesnt work

    what exactly does not work in which way?

  3. stemie
    Member
    Posted 2 years ago #

    This is what Im trying to achive, say I have a custom field with a value 'is_home()' for the key 'ad-location'.

    I want the custom filed key to output its value as below:

    <?php if ( is_home() ) {  ?>
    global $post;
    $args = array( 'numberposts' => 1, 'post_type' => 'advert' );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post);
    	the_post_thumbnail('header1-ad');
    endforeach;
    <?php } ?>

    That is why I have written the code in my theme like this:

    <?php if ( get_post_meta($post->ID, 'ad-location', true) )

Topic Closed

This topic has been closed to new replies.

About this Topic