Design Meets Code

Design Meets Code

WordPress · PrestaShop

  • Startseite
  • WordPress
  • PHP / Datenbanken
  • Ubuntu
  • Prestashop
  • Verschiedenes
  • Grafik
  • Glossar

Shortcode für Zufallsartikel

29. November 2026 Leave a Comment

function zufalls_artikel_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'anzahl' => 5,
        'bild'   => 'medium', // Bildgröße: thumbnail, medium, large oder "nein"
    ), $atts );

    $query = new WP_Query( array(
        'post_type'           => 'post',
        'posts_per_page'      => intval( $atts['anzahl'] ),
        'orderby'             => 'rand',
        'ignore_sticky_posts' => true,
        'no_found_rows'       => true,
    ) );

    if ( ! $query->have_posts() ) return '';

    $out = '
    '; while ( $query->have_posts() ) { $query->the_post(); $link = esc_url( get_permalink() ); $out .= '
  • '; if ( $atts['bild'] !== 'nein' && has_post_thumbnail() ) { $out .= '' . get_the_post_thumbnail( null, sanitize_key( $atts['bild'] ), array( 'loading' => 'lazy', 'alt' => esc_attr( get_the_title() ) ) ) . ''; } $out .= '' . esc_html( get_the_title() ) . ''; $out .= '
  • '; } wp_reset_postdata(); return $out . '
'; } add_shortcode( 'zufalls_artikel', 'zufalls_artikel_shortcode' );

[zufalls_artikel anzahl=“5″] zeigt kleine Vorschaubilder (Größe „medium“).
[zufalls_artikel bild=“nein“] zeigt wie bisher nur die Titel.

.zufalls-artikel {
    list-style: none;
    margin: 0;
    padding: 0;
}
.zufalls-artikel li {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}
.zufalls-artikel-bild img {
    width: 70px;
    height: 70px;
    object-fit: cover;
    border-radius: 4px;
    display: block;
}

Filed Under: Allgemein, WordPress

Standard Sortierung von WooCommerce auf „zufällig“ ändern (mit Tagescache)

11. November 2026 Leave a Comment

Die Standardsortierung bei Woocommerce lässt sich mit folgendem Code auf „zufällig“ ändern, wobei nur 1x am Tag sortiert wird, so dass sich es beim Durchblättern keine Dubletten gibt und nicht bei jedem Seitenaufruf alles wieder neu zufällig sortiert wird.

// Zufällige Sortierung, die sich nur einmal pro Tag ändert
add_filter('woocommerce_get_catalog_ordering_args', 'custom_random_ordering_args');

function custom_random_ordering_args($args) {
if (is_admin() && !defined('DOING_AJAX')) {
return $args;
}

// Sortierung auf Zufall mit täglichem Seed setzen
$args['orderby'] = 'RAND(' . date('Ymd') . ')';
return $args;
}

Filed Under: WordPress

Produktkatalog für einen WooCommerce Shop in Masonry Layout darstellen

21. Oktober 2026 Leave a Comment

Damit die Produktauflistung in einem Woocommerce Shop im Masonry Layout dargestellt wird, wird folgender Code in die funstions.php des Childthemes oder in ein Code Snippet Plugin eingefügt.

function mein_masonry_laden() {
    wp_enqueue_script( 'masonry' );
    
    static $inline_done = false;
    if ( $inline_done ) return;
    $inline_done = true;

    wp_add_inline_script( 'masonry', "
        document.addEventListener('DOMContentLoaded', function () {
            document.querySelectorAll('ul.products').forEach(function (grid) {
                grid.classList.add('ist-masonry');
                imagesLoaded(grid, function () {
                    new Masonry(grid, {
                        itemSelector: 'li.product',
                        percentPosition: true
                    });
                });
            });
        });
    " );
}

// Shop- und Archivseiten
add_action( 'wp_enqueue_scripts', function () {
    if ( is_shop() || is_product_category() || is_product_tag() ) {
        mein_masonry_laden();
    }
} );

// Überall, wo der [products]-Shortcode ausgegeben wird
add_action( 'woocommerce_shortcode_before_products_loop', 'mein_masonry_laden' );

Falls du Masonry auch bei den Related Products möchtest: Ergänze in der Bedingung || is_product(). Die Bedingung lautet dann if ( is_shop() || is_product_category() || is_product_tag() || is_product() ). Weil das CSS jetzt an die Klasse gebunden ist, greift es dort automatisch mit.

Beim Astra Theme muss noch das CSS Grid überschrieben werden

.woocommerce ul.products.ist-masonry {
    display: block !important;
    margin: 0 -10px;
}
.woocommerce ul.products.ist-masonry::before,
.woocommerce ul.products.ist-masonry::after {
    display: none !important;
}
.woocommerce ul.products.ist-masonry li.product {
    float: none;
    margin: 0 0 20px !important;
    padding: 0 10px;
    box-sizing: border-box;
    width: 33.333% !important;
}
.woocommerce ul.products.ist-masonry.columns-2 li.product { width: 50% !important; }
.woocommerce ul.products.ist-masonry.columns-3 li.product { width: 33.333% !important; }
.woocommerce ul.products.ist-masonry.columns-4 li.product { width: 25% !important; }
.woocommerce ul.products.ist-masonry.columns-5 li.product { width: 20% !important; }

@media (max-width: 921px) {
    .woocommerce ul.products.ist-masonry li.product { width: 50% !important; }
}
@media (max-width: 544px) {
    .woocommerce ul.products.ist-masonry li.product { width: 100% !important; }
}

Es giibt auch eine reine CSS Lösung, allerdings laufen dann die Produkte spaltenweise von oben nach unten statt zeilenweise. Die Sortierung kann daher verwirrend wirken.

ul.products { display:block; columns: 3; column-gap: 20px; } 
li.product { width:100% !important; break-inside: avoid; }

Filed Under: WordPress Tagged With: Masonry, WooCommerce

  • 1
  • 2
  • 3
  • …
  • 46
  • Next Page »
Anzeige

Themen

Apache BuddyPress chmod Code Snippets Custom Post Types Datenbank Email Enfold fail2ban Foren genesis Grid View htaccess i-mscp Indesign Katalogpreisregel Layout Builder LibreOffice List View Mitgliederbereich MySQL PHP PHP Anfänger PHP Variablen Plugins Prestashop Prestashop 1.5 Prestashop 1.6 Prestashop 8 Prestashop Module Registrierung RSS Feed s2member Server Shell ssl Startseite Strings Tipps Vergleichsoperatoren Windows Windows 10 WooCommerce WooCommerce Sortierung WordPress
Anzeige

Letzte Kommentare

  • dmc zu Woocommerce: Produktbewertungen deaktivieren
  • medienplus zu Woocommerce: Produktbewertungen deaktivieren
  • Martin zu CSS: Automatische Silbentrennung in einzelnen Wörtern verhindern
  • Martin zu CSS: Automatische Silbentrennung in einzelnen Wörtern verhindern
  • Klaus zu Woocommerce: Shop Seiten neu generieren

Informationen

  • Kontakt
  • Datenschutzerklärung
  • Impressum
  • Cookie-Richtlinie (EU)
  • Kontakt
  • Datenschutzerklärung
  • Impressum
  • Cookie-Richtlinie (EU)
Anzeige