【问题标题】:If/Else statement on single blog page in WordpressWordpress 中单个博客页面上的 If/Else 语句
【发布时间】:2014-12-22 21:08:26
【问题描述】:

我正在开发一个不是我创建的 Wordpress 网站。开发人员将 content-single.php 页面用于站点上的其他内容。现在客户想要一个博客,但我不能使用 content-single php。

不幸的是,Wordpress 为博客上的单个页面引用了 content-single.php,但这些需要不同的格式。我可以使用 if/else 语句吗?我不是 PHP 开发人员,但正在尝试此修复:

<?php   if ( is_category('7') ) {
?>


<p>Thanks so much for your help!</p>




<?php   } else { ?>




<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    <div class="container">
        <?php if (get_the_post_thumbnail( $post_id, 'full')) { ?>
            <div class="row">
                <div class="col-sm-4 col-xs-12 landing-col">
      <?php echo get_the_post_thumbnail( $post_id, 'full'); ?>
        </div>
        <div class="col-sm-8 col-xs-12">
            <?php } ?>

                <header class="entry-header">
                    <h1 class="entry-title"><?php the_title(); ?></h1>

                    <div class="entry-meta">
                        <?php superhero_posted_on(); ?>
                    </div><!-- .entry-meta -->
                </header><!-- .entry-header -->

                <div class="entry-content">
                    <?php the_content(); ?>
                    <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'superhero' ), 'after' => '</div>' ) ); ?>
                </div><!-- .entry-content -->

                <footer class="entry-meta">
                    <?php
                        /* translators: used between list items, there is a space after the comma */
                        $category_list = get_the_category_list( __( ', ', 'superhero' ) );

                        /* translators: used between list items, there is a space after the comma */
                        $tag_list = get_the_tag_list( '', __( ', ', 'superhero' ) );

                        if ( ! superhero_categorized_blog() ) {
                            // This blog only has 1 category so we just need to worry about tags in the meta text
                            if ( '' != $tag_list ) {
                                $meta_text = __( 'This entry was tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'superhero' );
                            } else {
                                $meta_text = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'superhero' );
                            }

                        } else {
                            // But this blog has loads of categories so we should probably display them here
                            if ( '' != $tag_list ) {
                                $meta_text = __( 'This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'superhero' );
                            } else {
                                $meta_text = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'superhero' );
                            }

                        } // end check for categories on this blog

                        printf(
                            $meta_text,
                            $category_list,
                            $tag_list,
                            get_permalink(),
                            the_title_attribute( 'echo=0' )
                        );
                    ?>

                    <?php edit_post_link( __( 'Edit', 'superhero' ), '<span class="edit-link">', '</span>' ); ?>
                </footer><!-- .entry-meta -->

      <?php if (get_the_post_thumbnail( $post_id, 'full')) {  // if there is a first featured image & the featured image is not set to hidden, close the Bootsrap columns ?>
      </div><!-- /.col -->
    </div><!-- /.row -->
  <?php } ?>

  </div><!-- /.container -->    
</article><!-- #post-## -->

<?php  } ?>

从技术上讲,第 7 类(博客)应该有“非常感谢您的帮助”,而其余页面则输出原始代码。但这并没有发生。类别 7 仍在输出旧代码。

非常感谢您对此提供的任何帮助。

卡斯

【问题讨论】:

    标签: wordpress loops templates conditional


    【解决方案1】:

    is_category() 函数用于检查存档页面上的类别。我明白这一点,你想检查帖子(post_category)。为此,您必须使用 in_category($CAT_ID);使用此功能,您在插入参数类别中的帖子返回 true,如果不返回 false。

    【讨论】:

      【解决方案2】:

      您的if 语句中缺少右括号。这会导致解析错误,并且因为您没有调试,导致页面为空白。

      应该是:

      if ( in_category('7') ) {
      

      【讨论】:

      • 您的新代码创建了一个完全不同的问题。我很确定您正在寻找:if ( get_the_category() == 7 ) {
      • 试过了,还是不行...类别 7 仍然显示相同的代码。
      • 是正确答案!!
      猜你喜欢
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 2019-01-21
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      相关资源
      最近更新 更多