Php Wordpress—If elseif else语句—无法找出错误
所以我一直在尝试让index.php做三件事 如果有缩略图,则显示缩略图并进行特定的样式设置 如果没有缩略图,请执行其他操作 最后一个语句将显示,如果页面是单数的,它将显示其他内容 出于某种原因,它一直在失败。我在这里看过一些演示,但我搞不懂Php Wordpress—If elseif else语句—无法找出错误,php,wordpress,if-statement,Php,Wordpress,If Statement,所以我一直在尝试让index.php做三件事 如果有缩略图,则显示缩略图并进行特定的样式设置 如果没有缩略图,请执行其他操作 最后一个语句将显示,如果页面是单数的,它将显示其他内容 出于某种原因,它一直在失败。我在这里看过一些演示,但我搞不懂 <?php /** this is the first if statement **/?> <?php if ( is_home () || is_category() || is_archive() ): ?&g
<?php /** this is the first if statement **/?>
<?php if ( is_home () || is_category() || is_archive() ): ?>
<?php if ( has_post_thumbnail() ) { ?>
<article class="blog-card" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php optz_post_thumbnail(); ?>
<?php
if ( is_singular() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
endif;
if ( 'post' === get_post_type() ) : ?>
<div class="entry-meta">
</div><!-- .entry-meta -->
<?php
endif; ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php
the_excerpt( sprintf(
wp_kses(
/* translators: %s: Name of current post. Only visible to screen readers */
__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'optz' ),
array(
'span' => array(
'class' => array(),
),
)
),
get_the_title()
) );
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'optz' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<footer class="entry-footer">
<?php
optz_posted_on();
optz_posted_by();
?>
<?php optz_entry_footer(); ?>
</footer><!-- .entry-footer -->
</article><!-- #post-<?php the_ID(); ?> -->
<?php /** this is the first second else statement **/ elseif (is_home () || is_category() || is_archive()) :>
<article class="blog-card" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php
the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
if ( 'post' === get_post_type() ) : ?>
<div class="entry-meta">
</div><!-- .entry-meta -->
<?php
endif; ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php
the_content( sprintf(
wp_kses(
/* translators: %s: Name of current post. Only visible to screen readers */
__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'optz' ),
array(
'span' => array(
'class' => array(),
),
)
),
get_the_title()
) );
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'optz' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<footer class="entry-footer">
<?php
optz_posted_on();
optz_posted_by();
?>
<?php optz_entry_footer(); ?>
</footer><!-- .entry-footer -->
</article><!-- #post-<?php the_ID(); ?> -->
**更新代码:**
<?php if ( is_home () || is_category() || is_archive() ) { ?>
<?php if ( has_post_thumbnail() ) { ?>
<article class="blog-card" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php anasa_post_thumbnail(); ?>
<?php
if ( is_singular() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
endif;
if ( 'post' === get_post_type() ) : ?>
<div class="entry-meta">
</div><!-- .entry-meta -->
<?php
endif; ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php
the_excerpt( sprintf(
wp_kses(
/* translators: %s: Name of current post. Only visible to screen readers */
__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'anasa' ),
array(
'span' => array(
'class' => array(),
),
)
),
get_the_title()
) );
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'anasa' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<footer class="entry-footer">
<?php
anasa_posted_on();
anasa_posted_by();
?>
<?php anasa_entry_footer(); ?>
</footer><!-- .entry-footer -->
</article><!-- #post-<?php the_ID(); ?> -->
<?php /** this is the second else statement **/ } elseif (is_home () || is_category() || is_archive()) {?>
<article class="blog-card" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php
the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
if ( 'post' === get_post_type() ) : ?>
<div class="entry-meta">
</div><!-- .entry-meta -->
<?php
endif; ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php
the_content( sprintf(
wp_kses(
/* translators: %s: Name of current post. Only visible to screen readers */
__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'anasa' ),
array(
'span' => array(
'class' => array(),
),
)
),
get_the_title()
) );
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'anasa' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<footer class="entry-footer">
<?php
anasa_posted_on();
anasa_posted_by();
?>
<?php anasa_entry_footer(); ?>
</footer><!-- .entry-footer -->
</article><!-- #post-<?php the_ID(); ?> -->
<?php /** this is the last if else statement **/ } else {?>
<?php
if ( is_singular()) {
echo'hi';
}?>
<?php }} ?> <!-- this is the main php thumbnail close tag --!>
不确定这是否会影响您正在做的事情,但通常情况下,如果语句被翻转,您会希望这样做:if get_post_type==='post':?>您在这里有一个错误
<?php /** this is the first last if else statement **/ }else :{?>
不带冒号时应为:
<?php /** this is the first last if else statement **/ }else {?>
尝试对条件语句使用相同的标准,而不是在同一文件中使用不同的约定。是否缺少一个?在php结束标记中?在elseif之后的一行,你的评论是:这是elseif的第一个第二个语句 在另一行的else上,您还有一个冒号和一个花括号,其中您的另一条注释是:这是第一条if-else语句,也是最后一条if-else语句 还有一个建议,如果您在视图中与Html一起编写PHP,许多人会使用冒号而不是花括号,因此代码更容易查看。无论哪种方式,只要你更喜欢,但我认为你应该坚持这两种方式中的一种
你的陈述大体上是正确的。如果调整所有语句以使用:冒号或{,它应该都能正常工作。定义你所说的失败是什么意思?如果语句没有得到满足怎么办?它一直告诉我删除endif;我删除它,然后出现另一个错误,说删除?>等等,也许我做错了什么,我想你可以用默认wordpress中我设置的if-elseif-else语句主题是这样的,所以我就这样保留了它。我更新了上面的代码。它工作得很好,直到最后一行它忽略了该语句中的任何内容,其余的都可以。现在您有了一个额外的右括号}在您的最后一行代码中。我认为您可以将您的elseif语句更改为just else,您将进行两次相同的检查。这有帮助吗?我想我已经了解了为什么它没有显示在更新的代码中,因为我有一个在检查wordpress站点的此部分,然后尝试检查t中的单个页面当我把它移出主检查时,它就工作了。我更新了上面的代码。它工作得很好,直到最后一行它忽略了那句话中的任何内容,其余的都工作得很好。
<?php /** this is the first last if else statement **/ }else {?>
<?php elseif (is_home () || is_category() || is_archive()) :>
<?php /** this is the first last if else statement **/ }else :{?>