【问题标题】:Two Wordpress search results pages两个 Wordpress 搜索结果页面
【发布时间】:2015-04-29 15:50:10
【问题描述】:

我希望根据用户所在的页面有两个不同的搜索结果。

我复制了之前搜索内容的显示方式,添加了我希望人员搜索显示的额外代码,然后在其周围放置了一个 if 语句。

如果用户在“员工”页面上 - 然后显示这个 别的 显示这个。

我似乎无法让 if 语句工作,任何帮助将不胜感激。

编辑

这是原始工作代码,在我放入 IF 语句之前,尝试为员工从特定页面搜索创建另一组结果。

<?php
if ( is_home() ) {
    $paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : get_query_var( 'paged' );

    $args = array(
        'showposts' => (int) get_option('trim_homepage_posts'),
        'paged' => $paged,
        'category__not_in' => (array) get_option('trim_exlcats_recent'),
    );
    query_posts( apply_filters( 'et_home_args', $args ) );
}
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="entry post clearfix preownedSearch">

    <h2 class="et_pt_portfolio_title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php 
        $index_postinfo = get_option('trim_postinfo1');
        if ( $index_postinfo ){
            echo '<p class="meta">';
            et_postinfo_meta( $index_postinfo, get_option('trim_date_format'), esc_html__('0 comments','Trim'), esc_html__('1 comment','Trim'), '% ' . esc_html__('comments','Trim') );
            echo '</p>';
        }
    ?>

    <div class="post-content clearfix">
        <?php
            $thumb = '';
            $width = apply_filters('et_image_width',260);
            $height = apply_filters('et_image_height',170);
            $classtext = '';
            $titletext = get_the_title();
            $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
            $thumb = $thumbnail["thumb"];
        ?>
        <?php if ( '' != $thumb && 'on' == get_option('trim_thumbnails_index') ) { ?>
            <div class="featured_box filter">
            <a href="<?php the_permalink(); ?>">
            <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>  
                </a>    
            </div>  <!-- end .featured_box -->
        <?php } ?>
    </div>
</article>  <!-- end .post-->

<?php
endwhile;
if (function_exists('wp_pagenavi')) { wp_pagenavi(); }
else { get_template_part('includes/navigation','entry'); }
else:
get_template_part('includes/no-results','entry');
endif;
if ( is_home() ) wp_reset_query(); ?>

这是我将 if 语句放入后的编码 -

<?php
if ( is_home() ) {
    $paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : get_query_var( 'paged' );

    $args = array(
        'showposts' => (int) get_option('trim_homepage_posts'),
        'paged' => $paged,
        'category__not_in' => (array) get_option('trim_exlcats_recent'),
    );
    query_posts( apply_filters( 'et_home_args', $args ) );
}
?>

<?php if (is_page('stock-managers')) { ?>

<article class="entry post clearfix preownedSearch">

    <h2 class="et_pt_portfolio_title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?><?php the_meta(); ?></a></h2>
    <?php 
        $index_postinfo = get_option('trim_postinfo1');
        if ( $index_postinfo ){
            echo '<p class="meta">';
            et_postinfo_meta( $index_postinfo, get_option('trim_date_format'), esc_html__('0 comments','Trim'), esc_html__('1 comment','Trim'), '% ' . esc_html__('comments','Trim') );
            echo '</p>';
        }
    ?>

    <div class="post-content clearfix">
        <?php
            $thumb = '';
            $width = apply_filters('et_image_width',260);
            $height = apply_filters('et_image_height',170);
            $classtext = '';
            $titletext = get_the_title();
            $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
            $thumb = $thumbnail["thumb"];
        ?>
        <?php if ( '' != $thumb && 'on' == get_option('trim_thumbnails_index') ) { ?>
            <div class="featured_box filter">
            <a href="<?php the_permalink(); ?>">
            <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>  
                </a>    
            </div>  <!-- end .featured_box -->
        <?php } ?>
    </div>
</article>  <!-- end .post-->

<?php } else { ?>   

<article class="entry post clearfix preownedSearch">

    <h2 class="et_pt_portfolio_title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php 
        $index_postinfo = get_option('trim_postinfo1');
        if ( $index_postinfo ){
            echo '<p class="meta">';
            et_postinfo_meta( $index_postinfo, get_option('trim_date_format'), esc_html__('0 comments','Trim'), esc_html__('1 comment','Trim'), '% ' . esc_html__('comments','Trim') );
            echo '</p>';
        }
    ?>

<div class="post-content clearfix">
        <?php
            $thumb = '';
            $width = apply_filters('et_image_width',260);
            $height = apply_filters('et_image_height',170);
            $classtext = '';
            $titletext = get_the_title();
            $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
            $thumb = $thumbnail["thumb"];
        ?>
        <?php if ( '' != $thumb && 'on' == get_option('trim_thumbnails_index') ) { ?>
            <div class="featured_box filter">
            <a href="<?php the_permalink(); ?>">
            <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>  
                </a>    
            </div>  <!-- end .featured_box -->
        <?php } ?>
    </div>
</article>  <!-- end .post-->

<?php } endif; ?>




<?php
endwhile;
if (function_exists('wp_pagenavi')) { wp_pagenavi(); }
else { get_template_part('includes/navigation','entry'); }
else:
get_template_part('includes/no-results','entry');
endif;
if ( is_home() ) wp_reset_query(); ?>

编辑 2 匹配

<?php
if ( is_home() ) {
    $paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : get_query_var( 'paged' );

    $args = array(
        'showposts' => (int) get_option('trim_homepage_posts'),
        'paged' => $paged,
        'category__not_in' => (array) get_option('trim_exlcats_recent'),
    );
    query_posts( apply_filters( 'et_home_args', $args ) );
}
?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php if ($post->post_name('stock-managers')) { ?>

<article class="entry post clearfix preownedSearch">

    <h2 class="et_pt_portfolio_title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?><?php the_meta(); ?></a></h2>
    <?php 
        $index_postinfo = get_option('trim_postinfo1');
        if ( $index_postinfo ){
            echo '<p class="meta">';
            et_postinfo_meta( $index_postinfo, get_option('trim_date_format'), esc_html__('0 comments','Trim'), esc_html__('1 comment','Trim'), '% ' . esc_html__('comments','Trim') );
            echo '</p>';
        }
    ?>

    <div class="post-content clearfix">
        <?php
            $thumb = '';
            $width = apply_filters('et_image_width',260);
            $height = apply_filters('et_image_height',170);
            $classtext = '';
            $titletext = get_the_title();
            $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
            $thumb = $thumbnail["thumb"];
        ?>
        <?php if ( '' != $thumb && 'on' == get_option('trim_thumbnails_index') ) { ?>
            <div class="featured_box filter">
            <a href="<?php the_permalink(); ?>">
            <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>  
                </a>    
            </div>  <!-- end .featured_box -->
        <?php } ?>
    </div>
 </article>     <!-- end .post-->

 <?php else : ?>    

 <article class="entry post clearfix preownedSearch">

    <h2 class="et_pt_portfolio_title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php 
        $index_postinfo = get_option('trim_postinfo1');
        if ( $index_postinfo ){
            echo '<p class="meta">';
            et_postinfo_meta( $index_postinfo, get_option('trim_date_format'), esc_html__('0 comments','Trim'), esc_html__('1 comment','Trim'), '% ' . esc_html__('comments','Trim') );
            echo '</p>';
        }
    ?>

    <div class="post-content clearfix">
        <?php
            $thumb = '';
            $width = apply_filters('et_image_width',260);
            $height = apply_filters('et_image_height',170);
            $classtext = '';
            $titletext = get_the_title();
            $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
            $thumb = $thumbnail["thumb"];
        ?>
        <?php if ( '' != $thumb && 'on' == get_option('trim_thumbnails_index') ) { ?>
            <div class="featured_box filter">
            <a href="<?php the_permalink(); ?>">
            <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>  
                </a>    
            </div>  <!-- end .featured_box -->
        <?php } ?>
    </div>
</article>  <!-- end .post-->

<?php endif; ?>

<?php
endwhile;
if (function_exists('wp_pagenavi')) { wp_pagenavi(); }
else { get_template_part('includes/navigation','entry'); }
else:
get_template_part('includes/no-results','entry');
endif;
if ( is_home() ) wp_reset_query(); ?>

【问题讨论】:

    标签: wordpress search if-statement


    【解决方案1】:

    你的 php 语法错误。

    PHP 语法检查:解析错误:语法错误,第 7 行代码中出现意外的 'endif' (T_ENDIF) &lt;?php } endif;?&gt; PHP 语法检查:解析代码时出错

    删除endif;你很高兴

    <?php if (is_page('staff')) { ?>
    
    <article class="entry post clearfix">
        <-- Title / Thumbnail / Price / Description coding -->
    </article> 
    
     <?php } else { ?>  
    
    <article class="entry post clearfix">
        <-- Title / Thumbnail / Description coding -->
    </article> 
    
    <?php }  ?>
    

    了解更多关于 PHP 的Alternativ syntax

    answer查看使用if(condition){}if(condition):endif;的解释

    编辑:

    如果您在循环中使用 is_page,则始终为 false /see Source

    相反,您可以将所选值(此处为 staff)与当前帖子/页面的任何其他字段进行比较。

    <?php if ( $post->post_name=='staff' ) { ?>
    
    <article class="entry post clearfix">
        <-- Title / Thumbnail / Price / Description coding -->
    </article> 
    
     <?php } else { ?>  
    
    <article class="entry post clearfix">
        <-- Title / Thumbnail / Description coding -->
    </article> 
    
    <?php }  ?>
    

    编辑二:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
        <?php if ($post->post_name('stock-managers')) { ?>
    
            <article class="entry post clearfix preownedSearch">
    
            </article>  <!-- end .post-->
    
        <?php else : ?>   
    
            <article class="entry post clearfix preownedSearch">
    
    
            </article>  <!-- end .post-->
    
        <?php endif; ?>
    
    <?php
    endwhile;
    ?>
    

    【讨论】:

    • 感谢您的回复,我试过了,不幸的是它也没有用。它周围一定有什么事情发生了,那就是让它无法工作。
    • 此标签 (is_page) 必须在循环之前使用,并且在循环内不起作用(请参阅下面的注释)。 codex.wordpress.org/Function_Reference/is_page
    • 我添加了更多代码,以便更好地了解我在做什么。当我删除编码的最后一部分时,最后,只会出现 1 个结果,没有要求的额外信息。
    • 如果不正确,您只是将 Loop If 替换为您自己的。您应该启用 Wordpress 的调试模式以查看 PHP 语法错误...再次检查我编辑的帖子,该帖子现在应该适合您。否则我帮不了你....
    • 我添加了我在上面发布的代码(编辑两个匹配项),它没有改变任何东西,然后我使用调试,只有这个出现:“wp_enqueue_script 被错误地调用”
    猜你喜欢
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多