• Resolved acp693

    (@acp693)


    Hi, I have the following .php function to put a language switcher in my header.

    /**
     * Polylang Shortcode - https://www.ads-software.com/plugins/polylang/
     * Add this code in your functions.php
     * Put shortcode [polylang_langswitcher] to post/page for display flags
     *
     * @return string
     */
    function custom_polylang_langswitcher() {
    	$output = '';
    	if ( function_exists( 'pll_the_languages' ) ) {
    		$args   = [
    			'show_flags' => 0,
    			'show_names' => 1,
    			'echo'       => 0,
    		];
    		$output = '<ul class="polylang_langswitcher">'.pll_the_languages( $args ). '</ul>';
    	}
    
    	return $output;
    }
    
    add_shortcode( 'polylang_langswitcher', 'custom_polylang_langswitcher' );
    

    I used a Gutenberg shortcode block to apply the shortcode and it gives me this:

    I want to disable the list view of the languages, and have them in one row next to each other. I wrote this in my style.css file:

    /*
    Theme Name: twentytwentythree-child
    Theme URI:
    Author:
    Author URI:
    Description:
    Requires at least: 6.0
    Tested up to: 6.2.2
    Requires PHP: 5.7
    Version: 0.0.1
    License: GNU General Public License v2 or later
    License URI: https://www.gnu.org/licenses/gpl-2.0.html
    Template: twentytwentythree
    Text Domain: twentytwentythreechild
    Tags: blog, news, portfolio, one-column, wide-blocks, accessibility-ready, block-patterns, block-styles, custom-colors, custom-logo, custom-menu, editor-style, featured-images, full-site-editing, rtl-language-support, sticky-post, threaded-comments, translation-ready,
    */

    .lang-item{
    display:inline;
    padding-left:5px;
    list-style:none;
    }

    How do I apply the css to my shortcode in the Gutenberg editor?

Viewing 4 replies - 1 through 4 (of 4 total)
  • You would need to add that using editor styles.

    The best way to do it is to save it as a separate css file and link that to both your themes public output and to the editor area.

    Thread Starter acp693

    (@acp693)

    Thank you for your help. I added this to my functions.php file:

    /**
     * Registers an editor stylesheet for the theme.
     */
    function wpdocs_theme_add_editor_styles() {
        add_editor_style( 'custom-editor-style.css' );
    }
    add_action( 'admin_init', 'wpdocs_theme_add_editor_styles' );

    And then created a file: custom-editor-style.css

    I put this in the file:

    .lang-item{
    display:inline;
    padding-left:5px;
    list-style:none;
    }

    But I’m not seeing any changes. I Know I’ve probably got the syntax wrong, I just don’t know what. Thanks again!

    Thread Starter acp693

    (@acp693)

    I read somemore about the 2023 theme and added this to my Child theme Functions.php file but it’s still not working:

    /**
     * Registers support for editor styles & Enqueue it.
     */
    function fse_child_styles() {
    	wp_enqueue_style( 'fse-child-style', get_stylesheet_uri() );
    }
    add_action( 'wp_enqueue_scripts', 'fse_child_styles' );

    If anyone could help me solve this, I’d be very grateful ??

    Thread Starter acp693

    (@acp693)

    Did some one break in in the night and fix this because it’s working now! I woke up this morning and it was working, I think I was so tired yesterday I didn’t realize the menu had changed to how I wanted it. Just to recap for anyone else trying to add a language switcher for Polylang non pro version, I’m using a child of the Twenty TwentyThree theme created by CreateBlockTheme plugin.

    Add a Functions.php file to the theme. Add these functions:

    <?php
    
    /**
     * Registers support for editor styles & Enqueue it.
     */
    function fse_child_styles() {
    	wp_enqueue_style( 'fse-child-style', get_stylesheet_uri() );
    }
    add_action( 'wp_enqueue_scripts', 'fse_child_styles' );
    
    
    /**
     * Polylang Shortcode - https://www.ads-software.com/plugins/polylang/
     * Add this code in your functions.php
     * Put shortcode [polylang_langswitcher] to post/page for display flags
     *
     * @return string
     */
    function custom_polylang_langswitcher() {
    	$output = '';
    	if ( function_exists( 'pll_the_languages' ) ) {
    		$args   = [
    			'show_flags' => 0,
    			'show_names' => 1,
    			'echo'       => 0,
                            //'hide_current' => 1,
    		];
    		$output = '<ul class="polylang_langswitcher">'.pll_the_languages( $args ). '</ul>';
    	}
    
    	return $output;
    }
    
    add_shortcode( 'polylang_langswitcher', 'custom_polylang_langswitcher' );
    
    ?>

    Add a Gutenberg Shortcode block where you want the language switcher and add this line to it:

    [polylang_langswitcher]

    Next in the child theme’s Style.css file add whatever styling you want, I added this:

    .lang-item {
      display: inline;
      padding-left: 5px;
      list-style: none;
      
    }
    
    .lang-item a:link {
      text-decoration: none;
    }
    
    .lang-item a:visited {
      text-decoration: none;
    }
    
    .lang-item a:hover {
      text-decoration: underline;
    }
    
    .lang-item a:active {
      text-decoration: underline !important;
    }

    I had to clear the cache of my browser occasionally to see the changes.

    • This reply was modified 1 year, 5 months ago by acp693.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Applying custom css to a shortcode block’ is closed to new replies.