How i make a shortcode from this code

Question

im trying to change this code into a shortcode because the method that im using right now is calling the whole file instead, and it a bit slow.

<?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="https://wordpress.stackexchange.com/questions/390754/<?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>

in progress 0
Wally 6 months 2021-06-18T14:01:28-05:00 0 Answer 0 views 0

Answer ( 1 )

    0
    2021-08-06T05:22:43-05:00

    To make the inteire file a shortcode its at least something to considered, for that I would call the page instead. However a short code can be done like this. paste it in your functions.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');
    

    to a few more information check for example this. https://torquemag.io/2017/06/custom-shortcode/

Leave an answer

Browse
Browse