使PHP显示在类别页面中的问题

时间:2019-10-06 作者:Obsidian

我使用此代码在帖子顶部显示社交媒体共享链接。目前,它们在我的页面和帖子上显示良好。有没有办法让它也显示在我的分类页面上?另一个可行的选择是让代码显示在H1和内容下方的任何地方,我认为没有任何页面不需要这些按钮,所以这可能是更简单的解决方案。

任何指导都将不胜感激☺️

function crunchify_social_sharing_buttons($content) {
    global $post;
    if(is_singular() || is_home()){

        // Get current page URL 
        $crunchifyURL = urlencode(get_permalink());

        // Get current page title
        $crunchifyTitle = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, \'UTF-8\')), ENT_COMPAT, \'UTF-8\');
        // $crunchifyTitle = str_replace( \' \', \'%20\', get_the_title());

        // Get Post Thumbnail for pinterest
        $crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'full\' );

        // Construct sharing URL without using any script
        $twitterURL = \'https://twitter.com/intent/tweet?text=\'.$crunchifyTitle.\'&url=\'.$crunchifyURL.\'&via=Crunchify\';
        $facebookURL = \'https://www.facebook.com/sharer/sharer.php?u=\'.$crunchifyURL;
        $googleURL = \'https://plus.google.com/share?url=\'.$crunchifyURL;
        $bufferURL = \'https://bufferapp.com/add?url=\'.$crunchifyURL.\'&text=\'.$crunchifyTitle;
        $linkedInURL = \'https://www.linkedin.com/shareArticle?mini=true&url=\'.$crunchifyURL.\'&title=\'.$crunchifyTitle;

        // Based on popular demand added Pinterest too
        $pinterestURL = \'https://pinterest.com/pin/create/button/?url=\'.$crunchifyURL.\'&media=\'.$crunchifyThumbnail[0].\'&description=\'.$crunchifyTitle;

        // Add sharing button at the end of page/page content
        $variable .= \'<!-- Implement your own social sharing buttons without any JavaScript loading. No plugin required. Detailed steps here: http://crunchify.me/1VIxAsz -->\';
        $variable .= \'<div class="crunchify-social">\';
        $variable .= \'<a class="crunchify-link crunchify-twitter" href="\'. $twitterURL .\'" target="_blank">Twitter</a>\';
        $variable .= \'<a class="crunchify-link crunchify-facebook" href="\'.$facebookURL.\'" target="_blank">Facebook</a>\';
        $variable .= \'<a class="crunchify-link crunchify-googleplus" href="\'.$googleURL.\'" target="_blank">Google+</a>\';
        $variable .= \'<a class="crunchify-link crunchify-buffer" href="\'.$bufferURL.\'" target="_blank">Buffer</a>\';
        $variable .= \'<a class="crunchify-link crunchify-linkedin" href="\'.$linkedInURL.\'" target="_blank">LinkedIn</a>\';
        $variable .= \'<a class="crunchify-link crunchify-pinterest" href="\'.$pinterestURL.\'" data-pin-custom="true" target="_blank">Pin It</a>\';
        $variable .= \'</div>\';

        return $variable.$content;
    }else{
        // if not a post/page then don\'t include sharing button
        return $variable.$content;
    }
};
add_filter( \'the_content\', \'crunchify_social_sharing_buttons\');
这是我的分类。php,按要求

<?php
/**
 * The Category template file.
 *
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Zeal
 */

get_header(); ?>

    <section id="primary" class="content-area">

        <main id="main" class="site-main" role="main">

            <div class="container archive-page">

                <div class="row">

                    <?php if ( have_posts() ) : ?>

                            <header class="page-header">
                                <h1 class="page-title"><h1><?php single_cat_title(); ?></h1>
                            </header><!-- .page-header -->                                          
<?php 
the_archive_description( \'<div class="taxonomy-description">\', \'</div>\' ); 
?>
<hr class="wp-block-separator">
<div id="category-menu"><a class="category-link" href="https://www.obsidianurbexphotography.com/locations/">All</a>&nbsp;<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/education/">Education</a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/industrial/">Industrial</a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/leisure/">Leisure</a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/medical/">Medical</a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/military/">Military</a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/other/">Other</a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/religious/">Religious</a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/residential/">Residential</a>&nbsp;
</br>
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/austria/"></a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/belgium/"></a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/bulgaria/"></a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/france/"></a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/germany/"></a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/great-britain/"></a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/hungary/"></a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/italy/"></a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/japan/"></a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/luxembourg/"></a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/portugal/"> </a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/romania/"></a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/ukraine/"> </a>&nbsp;
<a class="category-link" href="https://www.obsidianurbexphotography.com/locations/usa/"></a>&nbsp;</div>
</br>
                            <?php /* Start the Loop */ ?>
                            <?php while ( have_posts() ) : the_post(); ?>

                                    <?php
                                    /**
                                     * Run the loop for the search to output the results.
                                     */
                                    get_template_part( \'template-parts/content\', \'category\' );
                                    ?>

                            <?php endwhile; ?>

                            <?php the_posts_navigation(); ?>

                    <?php else : ?>

                            <?php get_template_part( \'template-parts/content\', \'none\' ); ?>

                    <?php endif; ?>

                </div>

            </div>

        </main><!-- #main -->

    </section><!-- #primary -->

<?php get_footer(); ?>

2 个回复
最合适的回答,由SO网友:Antti Koskinen 整理而成

基于您的类别。php我假设您使用的是Zeal主题,这意味着get_template_part( \'template-parts/content\', \'category\' ) 可能会吐出这样的东西,https://themes.trac.wordpress.org/browser/zeal/1.0.8/template-parts/content-category.php, 如果我错了,请纠正我。

基于这个假设,我想说没有the_content 钩在你的环中可用,所以你的crunchify_social_sharing_buttons 函数永远不会执行。但你应该能够使用the_excerpt 过滤器,组件the_excerpt 在模板零件文件中使用,以实现相同的结果。要做到这一点,您需要稍微修改您的代码-只需将社交媒体链接生成移动到一个单独的函数,然后将其与这两个函数挂钩the_contentthe_excerpt.

就像这样,

add_filter( \'the_content\', \'the_content_crunchify_social_sharing_buttons\');
function the_content_crunchify_social_sharing_buttons($content) {
  return (is_singular() || is_home()) ? crunchify_social_sharing_buttons($content) : $content;
}

add_filter( \'the_excerpt\', \'the_excerpt_crunchify_social_sharing_buttons\');
function the_excerpt_crunchify_social_sharing_buttons($excerpt) {
  return (is_archive()) ? crunchify_social_sharing_buttons($excerpt) : $excerpt;
}

function crunchify_social_sharing_buttons($content) {
  global $post;

  // Get current page URL 
  $crunchifyURL = urlencode(get_permalink());

  // Get current page title
  $crunchifyTitle = htmlspecialchars(urlencode(html_entity_decode(get_the_title(), ENT_COMPAT, \'UTF-8\')), ENT_COMPAT, \'UTF-8\');
  // $crunchifyTitle = str_replace( \' \', \'%20\', get_the_title());

  // Get Post Thumbnail for pinterest
  $crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'full\' );

  // Construct sharing URL without using any script
  $twitterURL = \'https://twitter.com/intent/tweet?text=\'.$crunchifyTitle.\'&amp;url=\'.$crunchifyURL.\'&amp;via=Crunchify\';
  $facebookURL = \'https://www.facebook.com/sharer/sharer.php?u=\'.$crunchifyURL;
  $googleURL = \'https://plus.google.com/share?url=\'.$crunchifyURL;
  $bufferURL = \'https://bufferapp.com/add?url=\'.$crunchifyURL.\'&amp;text=\'.$crunchifyTitle;
  $linkedInURL = \'https://www.linkedin.com/shareArticle?mini=true&url=\'.$crunchifyURL.\'&amp;title=\'.$crunchifyTitle;

  // Based on popular demand added Pinterest too
  $pinterestURL = \'https://pinterest.com/pin/create/button/?url=\'.$crunchifyURL.\'&amp;media=\'.$crunchifyThumbnail[0].\'&amp;description=\'.$crunchifyTitle;

  // Add sharing button at the end of page/page content
  $variable = \'<div class="crunchify-social">\';
  $variable .= \'<a class="crunchify-link crunchify-twitter" href="\'. $twitterURL .\'" target="_blank">Twitter</a>\';
  $variable .= \'<a class="crunchify-link crunchify-facebook" href="\'.$facebookURL.\'" target="_blank">Facebook</a>\';
  $variable .= \'<a class="crunchify-link crunchify-googleplus" href="\'.$googleURL.\'" target="_blank">Google+</a>\';
  $variable .= \'<a class="crunchify-link crunchify-buffer" href="\'.$bufferURL.\'" target="_blank">Buffer</a>\';
  $variable .= \'<a class="crunchify-link crunchify-linkedin" href="\'.$linkedInURL.\'" target="_blank">LinkedIn</a>\';
  $variable .= \'<a class="crunchify-link crunchify-pinterest" href="\'.$pinterestURL.\'" data-pin-custom="true" target="_blank">Pin It</a>\';
  $variable .= \'</div>\';

  return $variable.$content;
}

SO网友:KGreene

您确实需要添加is\\u post\\u归档,但您的过滤器“the\\u content”也用于帖子。您可以直接从类别调用函数。php或更改挂钩。

the_content

相关推荐

5 PHP致命错误:未捕获ArgumentCountError(找不到)

我最近将WordPress站点从PHP5迁移到PHP7。我们的一个自定义文件列表功能(从服务器目录上的文件夹输出文件列表)在PHP7上的新服务器上失败。我知道存在争论冲突,但我不知道如何纠正它。The error:5 PHP致命错误:Uncaught ArgumentCounter错误:函数printDirectory()的参数太少,传入了3个/nas/content/live/lfccedu/wp-content/plugins/lfcc/lfcc。第1026行的php和/nas/content/liv