【问题标题】:Where to add google custom search code in search php在搜索php中在哪里添加google自定义搜索代码
【发布时间】:2014-08-24 09:29:23
【问题描述】:

我需要关于在 search.php 文件中的何处添加我的谷歌自定义搜索引擎代码的建议?

这里是php文件:

请注意,endwhile 和 endif 包括在内,我在 youtube 上进行了检查,并尝试删除(如果有帖子)和(endif)之间的代码,并在那里添加 google 代码,但它不起作用..

    <?php $mts_options = get_option('point'); ?>
<?php get_header(); ?>
<div id="page" class="home-page">
    <div class="content">
        <div class="article">
            <h1 class="postsby">
                <span><?php _e("Search Results for:", "mythemeshop"); ?></span> <?php the_search_query(); ?>
            </h1>   
            <?php  $j=0; $i =0; if (have_posts()) : while (have_posts()) : the_post(); ?>
                <article class="<?php echo 'pexcerpt'.$i++?> post excerpt <?php echo (++$j % 2 == 0) ? 'last' : ''; ?>">
                    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow" id="featured-thumbnail">
                        <?php if ( has_post_thumbnail() ) { ?> 
                            <?php echo '<div class="featured-thumbnail">'; the_post_thumbnail('featured',array('title' => '')); echo '</div>'; ?>
                        <?php } else { ?>
                            <div class="featured-thumbnail">
                                <img src="<?php echo get_template_directory_uri(); ?>/images/nothumb.png" class="attachment-featured wp-post-image" alt="<?php the_title(); ?>">
                            </div>
                        <?php } ?>
                        <div class="featured-cat"><?php $category = get_the_category(); echo $category[0]->cat_name; ?></div>
                    </a>
                    <header>                        
                        <h2 class="title">
                            <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a>
                        </h2>
                        <div class="post-info"><span class="theauthor"><?php the_author_posts_link(); ?></span> | <span class="thetime"><?php the_time( get_option( 'date_format' ) ); ?></span></div>

                    </header><!--.header-->
                    <div class="post-content image-caption-format-1">
                        <p>
                            <?php echo mts_excerpt(29);?>
                            <span class="readMore"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow"><?php _e('Read More','mythemeshop'); ?></a></span>
                        </p>
                    </div>
                </article>
            <?php endwhile; else: ?>
                <div class="no-results">
                    <h5><?php _e('No Results found. We apologize for any inconvenience, please hit back on your browser or use the search form below.', 'mythemeshop'); ?></h5>
                    <?php get_search_form(); ?>
                </div><!--noResults-->
            <?php endif; ?> 
            <!--Start Pagination-->
            <?php if ( isset($mts_options['mts_pagenavigation']) && $mts_options['mts_pagenavigation'] == '1' ) { ?>
                <?php  $additional_loop = 0; global $additional_loop; mts_pagination($additional_loop['max_num_pages']); ?>           
            <?php } else { ?>
                <div class="pagination">
                    <ul>
                        <li class="nav-previous"><?php next_posts_link( __( '&larr; '.'Older posts', 'mythemeshop' ) ); ?></li>
                        <li class="nav-next"><?php previous_posts_link( __( 'Newer posts'.' &rarr;', 'mythemeshop' ) ); ?></li>
                    </ul>
                </div>
            <?php } wp_reset_query(); ?>
            <!--End Pagination-->           
        </div>
        <?php get_sidebar(); ?>
<?php get_footer(); ?>

问候!!

【问题讨论】:

    标签: php search google-search


    【解决方案1】:

    只需创建一个名为 searchform.php 的新文件,然后将您的 Google 自定义搜索代码粘贴到那里。

    【讨论】:

    • searchform.php 只是用于搜索框外观不是吗?对于结果,我需要编辑 search.php .... 如果我错了,请纠正我?
    • 我已经在我的主题文件(searchform.php)上添加了代码,就这样?
    • 尝试检查您的网站...如果不起作用,请在 Google 上搜索解决方案。
    • 这很好,并在谷歌中打开了搜索...我希望在原始站点中打开搜索...无论如何谢谢
    【解决方案2】:

    我设法更改了添加代码的位置,这里是 search.php 的完整代码

    <?php $mts_options = get_option('point'); ?>
    <?php get_header(); ?>
    <div id="page" class="home-page">
        <div class="content">
            <div class="article">
                <h1 class="postsby">
                    <span><?php _e("Search Results for:", "mythemeshop"); ?></span> <?php the_search_query(); ?>
                </h1>   
                <?php  $j=0; $i =0; if (have_posts()) : while (have_posts()) : the_post(); ?>
    <script>
      (function() {
        var cx = '000306723311332550300:tfoukb5efrq';
        var gcse = document.createElement('script');
        gcse.type = 'text/javascript';
        gcse.async = true;
        gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
            '//www.google.com/cse/cse.js?cx=' + cx;
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(gcse, s);
      })();
    </script>
    <gcse:search></gcse:search>
    </div>
                    </article>
                <?php endwhile; else: ?>
                    <div class="no-results">
                        <h5><?php _e('No Results found. We apologize for any inconvenience, please hit back on your browser or use the search form below.', 'mythemeshop'); ?></h5>
                        <?php get_search_form(); ?>
                    </div><!--noResults-->
                <?php endif; ?> 
                <!--Start Pagination-->
                <?php if ( isset($mts_options['mts_pagenavigation']) && $mts_options['mts_pagenavigation'] == '1' ) { ?>
                    <?php  $additional_loop = 0; global $additional_loop; mts_pagination($additional_loop['max_num_pages']); ?>           
                <?php } else { ?>
                    <div class="pagination">
                        <ul>
                            <li class="nav-previous"><?php next_posts_link( __( '&larr; '.'Older posts', 'mythemeshop' ) ); ?></li>
                            <li class="nav-next"><?php previous_posts_link( __( 'Newer posts'.' &rarr;', 'mythemeshop' ) ); ?></li>
                        </ul>
                    </div>
                <?php } wp_reset_query(); ?>
                <!--End Pagination-->           
            </div>
    <?php get_footer(); ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多