开发者

WordPress Conditional: Only show if current page title is equal to post meta

I am calling a bunch of posts under a certain post type in WordPress which works but I am trying to add a conditional that will check first if those post's custom meta field (labeled "disc") is equal to the current post's title.

Here is what I have but this conditional does not seem to work:

<?php
   $pages = get_posts('numberposts=9999&post_type=song&post_status=publish&order=ASC&orderby=date');

   $i = 1;
   foreach( $pages as $page ) {
       $content = $page->post_title;
       if( empty($content) ) continue;

       $content = apply_filters('the_content', $content); ?>

       <?php if(get_post_meta($page->ID, "p30-disc", true)=="the_title()") { ?>

       <tbody class="vevent">

       <?php if ($i%2===0) { ?><tr class="gigpress-row gigpress-alt">

       <?php } else { ?><tr class="gigpress-row"><?php } ?>

       <td><?php echo $page->post_title ?></td>
        <td><?php echo get_post_meta($page->ID, "p30-length", true); ?></td>
        <td><a href="http://itunes.com/<?php echo get_post_meta($page->ID, "p30-itunes-song", true); ?>">BUY</a></开发者_如何学编程td>

        </tr>

        <tr class="gigpress-info">

        <td colspan="3"><?php echo $page->post_content ?></td>

        </tr>

        </tbody>    

<?php $i++;

   } } ?>

When I simply echo "get_post_meta($page->ID, "p30-disc", true)" or "the_title()" it spits out their proper values, which are equal, so obviously something is just wrong with that conditional.

Thanks,

Wade


Turned out there is a function called get_the_title() that is used for this sort of thing. Worked perfectly.


It looks like you're "quoting" the title function - essentially, turning it into a string, when you should just be calling it.

<?php if(get_post_meta($page->ID, "p30-disc", true)==the_title()) { ?>

that should work?


You can use get_the_title() function to route your pages or display specific content.

if ( is_front_page() || is_home() ) :
    get_template_part( 'template-parts/content', 'home' );
elseif(get_the_title() === 'account'):
    get_template_part( 'template-parts/content', 'account' );
elseif(get_the_title() === 'login'):
    get_template_part( 'template-parts/content', 'login' );
else :
    get_template_part( 'template-parts/content', 'page' );

note this is case sensitive so if your page title is all lower case then your string should be to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜