Ask your WordPress questions! Pay money and get answers fast! (more info)

Help with PHP Query WordPress

  • SOLVED

Hello, I currently have this query on my page's title to change the class of the title depending on which page its parent is, this works great at the moment.

<?php
wp_reset_query();
global $post;
if ($post->post_parent) {
$parent = $post->post_parent;
$ancestors = get_post_ancestors($post->ID);
}
if (( $parent == 8 ) OR (in_array(8,$ancestors))) { ?>
<div class="pagetitleshops"><?php the_title(); ?></div>
<?php } else if (( $parent == 35 ) OR (in_array(35,$ancestors))) { ?>
<div class="pagetitlefood"><?php the_title(); ?></div>
<?php } else if (( $parent == 36 ) OR (in_array(36,$ancestors))) { ?>
<div class="pagetitlemusic"><?php the_title(); ?></div>
<?php } else { ?>
<div class="pagetitle"><?php the_title(); ?></div>
<?php } ?>


However I would like to incorporate this bit of code too:

<?php $value = get_field( "page_logo" );
if( $value ) { ?>
<img src="<?php echo do_shortcode('[acf field="page_logo" post_id="'.get_the_ID().'"]'); ?>" width="100%" alt="<?php the_title(); ?>"/>
<? } else { ?>
<div class="pagetitlemusic"><?php the_title(); ?></div>
<?php } ?>


So IF there is a custom field 'page_logo' show image IF NOT then show title as per first bit of code.

Thanks

Answers (3)

2014-08-18

Navjot Singh answers:

Try

<?php

wp_reset_query();

global $post;

if ($post->post_parent) {

$parent = $post->post_parent;

$ancestors = get_post_ancestors($post->ID);

}

$value = get_field( "page_logo" );

if( $value ) { ?>

<img src="<?php echo do_shortcode('[acf field="page_logo" post_id="'.get_the_ID().'"]'); ?>" width="100%" alt="<?php the_title(); ?>"/>

<? } else if (( $parent == 8 ) OR (in_array(8,$ancestors))) { ?>

<div class="pagetitleshops"><?php the_title(); ?></div>

<?php } else if (( $parent == 35 ) OR (in_array(35,$ancestors))) { ?>

<div class="pagetitlefood"><?php the_title(); ?></div>

<?php } else if (( $parent == 36 ) OR (in_array(36,$ancestors))) { ?>

<div class="pagetitlemusic"><?php the_title(); ?></div>

<?php } else { ?>

<div class="pagetitle"><?php the_title(); ?></div>

<?php } ?>

2014-08-18

S├ębastien | French WordpressDesigner answers:

<?php $value = get_field( "page_logo" );

if( $value ) { ?>

<img src="<?php echo do_shortcode('[acf field="page_logo" post_id="'.get_the_ID().'"]'); ?>" width="100%" alt="<?php the_title(); ?>"/>

<? } else {

wp_reset_query();

global $post;

if ($post->post_parent) {

$parent = $post->post_parent;

$ancestors = get_post_ancestors($post->ID);

}

if (( $parent == 8 ) OR (in_array(8,$ancestors))) { ?>

<div class="pagetitleshops"><?php the_title(); ?></div>

<?php } else if (( $parent == 35 ) OR (in_array(35,$ancestors))) { ?>

<div class="pagetitlefood"><?php the_title(); ?></div>

<?php } else if (( $parent == 36 ) OR (in_array(36,$ancestors))) { ?>

<div class="pagetitlemusic"><?php the_title(); ?></div>

<?php } else { ?>

<div class="pagetitle"><?php the_title(); ?></div>

<?php }

} ?>


S├ębastien | French WordpressDesigner comments:

in fact, you must just replace, in the second code this line :
<div class="pagetitlemusic"><?php the_title(); ?></div>
by all the first code

Try and said me if it's ok

2014-08-18

Arnav Joy answers:

here is the full code

<?php

wp_reset_query();

global $post;

if ($post->post_parent) {

$parent = $post->post_parent;

$ancestors = get_post_ancestors($post->ID);

}


$value = get_field( "page_logo" ,$post->ID );
if( !empty( $value )){ ?>

<img src="<?php echo do_shortcode('[acf field="page_logo" post_id="'.get_the_ID().'"]'); ?>" width="100%" alt="<?php the_title(); ?>"/>

<?php } else {


if (( $parent == 8 ) OR (in_array(8,$ancestors))) { ?>

<div class="pagetitleshops"><?php the_title(); ?></div>

<?php } else if (( $parent == 35 ) OR (in_array(35,$ancestors))) { ?>

<div class="pagetitlefood"><?php the_title(); ?></div>

<?php } else if (( $parent == 36 ) OR (in_array(36,$ancestors))) { ?>

<div class="pagetitlemusic"><?php the_title(); ?></div>

<?php } else { ?>

<div class="pagetitle"><?php the_title(); ?></div>

<?php } } ?>