【问题标题】:Add custom description to woocommerce shop-site based on IDs根据 ID 向 woocommerce 商店网站添加自定义描述
【发布时间】:2021-01-08 09:07:44
【问题描述】:

我想为我的 woocommerce 商店添加自定义描述。我想在商店上显示不同的描述,并且描述(当点击产品时)将保持与商店上写的一样。

我想把它分开。我看到它的唯一方法是使用一些自定义 html 和 php,但我不知道。 CSS似乎没有帮助。由于我有各种产品,我想按产品 ID 将其分开,并为每个产品自己编写描述,以使其看起来像:

这是我的文件 oceanwp-strings.php:

<?php
/**
 * OceanWP theme strings
 *
 * @package OceanWP WordPress theme
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! function_exists( 'oceanwp_theme_strings' ) ) {

    /**
     * OceanWP Theme Strings
     *
     *  @author Amit Singh (apprimit@gmail.com)
     *  @since 1.8.5
     *
     * @param  string  $value  String key.
     * @param  boolean $echo   Print string.
     * @return mixed           Return string or nothing.
     */
    function oceanwp_theme_strings( $value, $echo = true ) {

        $oceanwp_strings = apply_filters(
            'oceanwp_theme_strings',
            array(

                'owp-string-header-skip-link'            => apply_filters( 'ocean_header_skip_link', __( 'Skip to content', 'oceanwp' ) ),
                'owp-string-search-text'                 => apply_filters( 'ocean_search_text', __( 'Search', 'oceanwp' ) ),
                'owp-string-mobile-search-text'          => apply_filters( 'ocean_mobile_search_text', __( 'Search', 'oceanwp' ) ),
                'owp-string-mobile-fs-search-text'       => apply_filters( 'ocean_mobile_fs_search_text', __( 'Type your search', 'oceanwp' ) ),
                'owp-string-header-replace-search-text'  => apply_filters( 'ocean_header_replace_search_text', __( 'Type then hit enter to search...', 'oceanwp' ) ),
                'owp-string-search-overlay-search-text'  => apply_filters( 'ocean_search_overlay_search_text', __( 'Type then hit enter to search', 'oceanwp' ) ),
                'owp-string-vertical-header-search-text' => apply_filters( 'ocean_vertical_header_search_text', __( 'Search...', 'oceanwp' ) ),
                'owp-string-medium-header-search-text'   => apply_filters( 'ocean_medium_header_search_text', __( 'Search...', 'oceanwp' ) ),
                'owp-string-comment-logout-text'         => apply_filters( 'ocean_comment_logout_text', __( 'Log out of this account', 'oceanwp' ) ),
                'owp-string-comment-placeholder'         => apply_filters( 'ocean_comment_placeholder', __( 'Your comment here...', 'oceanwp' ) ),
                'owp-string-comment-profile-edit'        => apply_filters( 'ocean_comment_profile_edit', __( 'Click to edit your profile', 'oceanwp' ) ),
                'owp-string-comment-post-button'         => apply_filters( 'ocean_comment_post_button', __( 'Post Comment', 'oceanwp' ) ),
                'owp-string-comment-name-req'            => apply_filters( 'ocean_comment_name_req', __( 'Name (required)', 'oceanwp' ) ),
                'owp-string-comment-email-req'           => apply_filters( 'ocean_comment_email_req', __( 'Email (required)', 'oceanwp' ) ),
                'owp-string-comment-name'                => apply_filters( 'ocean_comment_name', __( 'Name', 'oceanwp' ) ),
                'owp-string-comment-email'               => apply_filters( 'ocean_comment_email', __( 'Email', 'oceanwp' ) ),
                'owp-string-comment-website'             => apply_filters( 'ocean_comment_website', __( 'Website', 'oceanwp' ) ),
                'owp-string-search-continue-reading'     => apply_filters( 'ocean_search_continue_reading', __( 'Continue Reading', 'oceanwp' ) ),
                'owp-string-post-continue-reading'       => apply_filters( 'ocean_post_continue_reading', __( 'Continue Reading', 'oceanwp' ) ),
                'owp-string-single-related-posts'        => apply_filters( 'ocean_single_related_posts', __( 'You Might Also Like', 'oceanwp' ) ),
                'owp-string-single-next-post'            => apply_filters( 'ocean_single_next_post', __( 'Next Post', 'oceanwp' ) ),
                'owp-string-single-prev-post'            => apply_filters( 'ocean_single_prev_post', __( 'Previous Post', 'oceanwp' ) ),
                'owp-string-single-screen-reader-rm'     => apply_filters( 'ocean_single_screen_reader_rm', __( 'Read more articles', 'oceanwp' ) ),

                // Woocommerce.
                'owp-string-woo-quick-view-text'         => apply_filters( 'ocean_woo_quick_view_text', __( 'Quick View', 'oceanwp' ) ),
                'owp-string-woo-quick-view-close'        => apply_filters( 'ocean_woo_quick_view_close', __( 'Close quick preview', 'oceanwp' ) ),
                'owp-string-woo-floating-bar-select-btn' => apply_filters( 'ocean_woo_floating_bar_select_btn', __( 'Select Options', 'oceanwp' ) ),
                'owp-string-woo-floating-bar-selected'   => apply_filters( 'ocean_woo_floating_bar_selected', __( 'Selected:', 'oceanwp' ) ),
                'owp-string-woo-floating-bar-out-stock'  => apply_filters( 'ocean_woo_floating_bar_out_stock', __( 'Out of stock', 'oceanwp' ) ),
            )
        );

        if ( is_rtl() ) {
            // do your stuff.
        }

        $owp_string = isset( $oceanwp_strings[ $value ] ) ? $oceanwp_strings[ $value ] : '';

        /**
         * Print or return strings
         */
        if ( $echo ) {
            echo $owp_string; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        } else {
            return $owp_string;
        }
    }
}

【问题讨论】:

  • 你真的想要一个不同的描述吗?听起来您已经输入了正确的描述,它只是在商店页面上以不合适的格式获得 输出。因此,负责后者的模板部分可能应该进行相应的修改。
  • 其实我不想要另一个。我只想正确显示它。我已经上传了我的主题正在使用的模板(或我认为的模板)。感谢您以更好的方式描述我的问题。

标签: php css wordpress woocommerce shop


【解决方案1】:

你所谓的描述实际上是the_excerpt();。这几乎是您帖子内容的简短版本。你可以在这里@https://developer.wordpress.org/reference/functions/the_excerpt/和https://developer.wordpress.org/reference/functions/get_the_excerpt/了解更多信息

由于每个主题都是不同的,因此现在已经消除了混淆,您可能在主题文件的某个地方有一个产品的模板部分。我需要文件的代码才能走得更远。

使用产品模板部分代码编辑您的帖子,我会看看。


编辑 1.2:在做任何事情之前,将产品模板的副本保存在安全的地方

if ( ! $length ) {
   echo wp_kses_post( strip_shortcodes( $post->post_excerpt ) );
} else {
   echo wp_trim_words( strip_shortcodes( $post->post_excerpt ), $length );
}

这用于从数据库$post-&gt;post_excerpt 中获取摘录。没有与之关联的格式。

我们将使用get_the_excerpt() 作为格式传递。
来源@https://developer.wordpress.org/reference/functions/get_the_excerpt/

if ( ! $length ) {
   echo strip_shortcodes( 'EDIT 1.2:'.get_the_excerpt() );
} else {
   echo wp_trim_words( strip_shortcodes( 'EDIT 1.2:'.get_the_excerpt()  ), $length );
}

【讨论】:

  • 你是对的。这是节选。我已经更新了它。我希望我选对了。非常感谢。
  • 请参阅 EDIT 1.1 关于您的最后评论。如果这回答了您的问题,请不要忘记投票并接受答案。
  • 我按照描述进行了编辑,但它似乎不起作用。没有任何变化。我什至清除了缓存以确保并使用另一个浏览器查看它。
  • 你自己写摘录吗?还是自动的?
  • 我自己在 WooCommerce/Products 下的产品选项中编写了它。可能所有信息都写在“简短描述”上?我尝试在应该是描述的文本区域进行测试(我更改为简短的,以便在产品站点的图像旁边显示它)但描述在商店页面上没有改变。
猜你喜欢
  • 2014-05-27
  • 1970-01-01
  • 1970-01-01
  • 2015-04-13
  • 1970-01-01
  • 1970-01-01
  • 2016-09-21
  • 2018-05-12
  • 1970-01-01
相关资源
最近更新 更多