【问题标题】:Can you add an if/get/echo statement in a shortcode您可以在简码中添加 if/get/echo 语句吗
【发布时间】:2020-07-27 04:19:10
【问题描述】:

一段时间以来,我一直在创建自定义简码来帮助在我的 Wordpress 网站上显示各种信息,而无需创建自定义页面/帖子模板,而且效果很好。

我最近也一直在实施高级自定义字段插件,与锄头相比,我最初是为自定义帖子类型添加元框,它改进了我开发网站的方式。

在创建自定义帖子模板时,我可以使用这样的东西来显示信息:

<?php if(get_field('email_address'))
{ echo '<a href="mailto://' .get_field('email_address') . '">EMAIL</a></div>'; }
?>

基本上,如果它有信息就会显示,否则什么也不做。在我有我的问题并且无法弄清楚的地方,有没有办法在 add_shortcode 详细信息中添加 if / get 语句?

下面是一个简码,它列出了一个目录教会位置及其详细信息,但在某些情况下,他们不会拥有所有信息。当它只是像物理地址这样的纯文本时,这很容易,就好像没有可用的东西一样 - 没有任何内容显示,因为文本的部分没有像填充一样的 CSS。

我遇到问题的地方是电子邮件或网站。即使他们不提供其中任何一个,编码仍然显示文本“电子邮件”或“网站”,但显然没有链接。理想情况下 - 此文本根本不会显示。

那么是否有可能使用带有 add_shortcode 编码的 if / get 类型的语句来实现这一点?

这是我目前使用的编码..提前感谢您的任何帮助和建议..

add_shortcode( 'directory' , 'Directory' );

function Directory($atts) {

$atts = shortcode_atts( array(
    'category' => ''
), $atts );

$categories  = explode(',' , $atts['category']);

$args = array(
        'post_type'     => 'church-directory',
        'post_status'   => 'publish',
        'orderby'         => 'title',
        'order'         => 'ASC',
        'posts_per_page'=> -1,
        'tax_query'     => array( array(
                            'taxonomy'  => 'category',
                            'field'     => 'term_id',
                            'operator' => 'AND',
                            'terms'     => $categories
                        ) )
    );

    $string = '';
    $string .= '<div style="margin-top: -30px;">';
    $query = new WP_Query( $args );

    if( ! $query->have_posts() ) {
        return false;
    }

    while( $query->have_posts() ){
        $query->the_post();
        $string .= '<div id="directoryWrapper">';
        $string .= '<div><h1><a style="text-transform: uppercase;" href="' .get_field('custom_url') . '">' . get_the_title() . '</a></h1></div>';
        $string .= '<div>' .get_field('address') . '</div><div style="clear: both;"></div>';
        $string .= '<div>' .get_field('phone_number') . '&nbsp;&nbsp;';
        $string .= '<a href="mailto://' .get_field('email_address') . '">EMAIL</a>&nbsp;&nbsp;';
        $string .= '<a href="' .get_field('custom_url') . '">WEBSITE</a></div><div style="clear: both;"></div>';
        $string .= '</div>';
    }

    $string .= '</div>';
    wp_reset_postdata();

    return $string;

}

【问题讨论】:

    标签: advanced-custom-fields shortcode


    【解决方案1】:

    所以我想通了.. 这是最终代码,以防它帮助想要做类似事情的人:

        add_shortcode('directory', 'Directory');
        function directory($atts){
    
        $atts = shortcode_atts( array(
        'category' => ''
    ), $atts );
    
        $categories  = explode(',' , $atts['category']);
    
    $args = array(
            'post_type'     => 'directory',
            'post_status'   => 'publish',
            'orderby'         => 'title',
            'order'         => 'ASC',
            'posts_per_page'=> -1,
            'tax_query'     => array( array(
                                'taxonomy'  => 'category',
                                'field'     => 'term_id',
                                'operator' => 'AND',
                                'terms'     => $categories
                            ) )
        );
    
    ob_start();
    
    $my_query = new WP_Query( $args );
    if( $my_query->have_posts() ) {
        ?>
            <?php
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    
            <div id="directoryWrapper">
            <h1><a style="text-transform: uppercase; font-weight: 600;" href="' .get_field('custom_url') . '"><?php the_title(); ?></a></h1>
            <?php if(get_field('address')) { echo '' . get_field('address') . ''; } ?>
            <?php if(get_field('phone_number')) { echo '<br>' . get_field('phone_number') . '&nbsp;'; } ?>
            <?php if(get_field('email_address')) { echo '<a href="mailto://' .get_field('email_address') . '">EMAIL</a>&nbsp;'; } ?>
            <?php if(get_field('custom_url')) { echo '<a href="' .get_field('custom_url') . '">WEBSITE</a>'; } ?>
            <div style="clear: both;"></div>
            </div>
    
    
            <?php endwhile; ?>
        <?php 
    }
    wp_reset_query();//reset the global variable related to post loop
    $retVal = ob_get_contents();
    ob_end_clean();
    
    return $retVal;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      相关资源
      最近更新 更多