我如何从这个代码中创建一个短码

时间:2021-06-18 作者:Wally

我试图将此代码更改为一个短代码,因为我现在使用的方法是调用整个文件,速度有点慢。

<?php
/**
 * The template for displaying Archive pages.
 *
 * @package GeneratePress
 */

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

get_header(); 
?>

<div class="testttt">
    <div class="containertest">
        <div class="scroll">
<?php // Get the taxonomy\'s terms
$terms = get_terms(
    array(
        \'taxonomy\'   => \'job_empresas\',
        \'hide_empty\' => true,
    )
);

// Check if any term exists
if ( ! empty( $terms ) && is_array( $terms ) ) {
    // Run a loop and print them all
    foreach ( $terms as $term ) { ?>
       
            <div class="card">
                <div class="card-image">
                <img class="no-lazzy" src="<?php echo z_taxonomy_image_url($term->term_id, array(200, 200));  ?>"></a>
                </div>
                <div class="card-inner">
                    <h3 class="text text-title"><?php echo $term->name; ?></h3>
                    <div class="ofertas-company">Ofertas disponibles: <?php echo $term->count ?></div>
                    <input class="company-button" type="button" value="Ver ofertas" onclick="location.href=\'<?php echo esc_url( get_term_link( $term ) ) ?>\'">
                </div>
            </div>
    <?php
    }
} ?>
        </div>
    </div>
</div>
<script>
    const scroll = document.querySelector(".scroll");
var isDown = false;
var scrollX;
var scrollLeft;

// Mouse Up Function
scroll.addEventListener("mouseup", () => {
    isDown = false;
    scroll.classList.remove("active");
});

// Mouse Leave Function
scroll.addEventListener("mouseleave", () => {
    isDown = false;
    scroll.classList.remove("active");
});

// Mouse Down Function
scroll.addEventListener("mousedown", (e) => {
    e.preventDefault();
    isDown = true;
    scroll.classList.add("active");
    scrollX = e.pageX - scroll.offsetLeft;
    scrollLeft = scroll.scrollLeft;
});

// Mouse Move Function
scroll.addEventListener("mousemove", (e) => {
    if (!isDown) return;
    e.preventDefault();
    var element = e.pageX - scroll.offsetLeft;
    var scrolling = (element - scrollX) * 2;
    scroll.scrollLeft = scrollLeft - scrolling;
});

</script>

1 个回复
SO网友:tiago calado

要使inteire文件成为一个短代码,至少需要考虑一下,因此我将调用该页面。然而,一个简短的代码可以这样做。将其粘贴到函数中。php

if (! function_exists(\'my_custom_shortcode_with_the_name_I_had_choosen\')) {
  function my_custom_shortcode_with_the_name_I_had_choosen(){
     ... your code ....
  }
}

add_shortcode(\'whatever_name\', \'my_custom_shortcode_with_the_name_I_had_choosen\');
要了解更多信息,请查看此示例。https://torquemag.io/2017/06/custom-shortcode/

相关推荐

Shortcode not being executed

我将以下代码放在WP“init”回调中(或加载插件时)。add_shortcode(\'my_shortcode\', function($atts, $content =\'\') { die(); } ); if (!shortcode_exists(\'my_shortcode\')) die(); 在我的页面中,我放入“[my_shortcode]“”当我查看页面时,我得到“****”知道我的代码怎么了吗?更