Home / Widgets / deepseek2
Duplicate Snippet

Embed Snippet on Your Site

deepseek2

magazine 2

WAL HAD
<10
Code Preview
php
<?php
<?php
/**
 * Plugin Name: Cultural Magazine (8 Arabic Sections)
 * Description: Custom post types + 8 predefined sections (أخبار، تقارير خاصة، اقتصاد، ثقافة، رياضة، مقالات، صحة، منوعات)
 * Version: 1.0
 */
if ( ! defined( 'ABSPATH' ) ) exit;
// 1. Create Custom Post Types: Article, Review, Interview
function magazine_cpt() {
    $post_types = [
        'article'   => [ 'Articles', 'Article', 'dashicons-book' ],
        'review'    => [ 'Reviews', 'Review', 'dashicons-star-filled' ],
        'interview' => [ 'Interviews', 'Interview', 'dashicons-microphone' ]
    ];
    foreach ( $post_types as $slug => $labels ) {
        register_post_type( $slug, [
            'labels' => [
                'name'          => $labels[0],
                'singular_name' => $labels[1],
                'add_new'       => "Add New {$labels[1]}",
            ],
            'public'       => true,
            'show_in_rest' => true,
            'menu_icon'    => $labels[2],
            'supports'     => [ 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ],
            'has_archive'  => true,
            'rewrite'      => [ 'slug' => $slug . 's' ],
        ]);
    }
}
add_action( 'init', 'magazine_cpt' );
// 2. Register "Sections" taxonomy (hierarchical, like categories)
function magazine_taxonomy() {
    register_taxonomy( 'magazine_section', [ 'article', 'review', 'interview' ], [
        'labels' => [
            'name'              => 'الأقسام (Sections)',
            'singular_name'     => 'قسم',
            'add_new_item'      => 'إضافة قسم جديد',
            'parent_item'       => 'قسم رئيسي',
        ],
        'public'       => true,
        'show_in_rest' => true,
        'hierarchical' => true,
        'rewrite'      => [ 'slug' => 'section' ],
    ]);
}
add_action( 'init', 'magazine_taxonomy' );
// 3. Pre‑create the 8 Arabic sections (run once on plugin activation)
function magazine_create_default_sections() {
    $sections = [
        'أخبار',           // News
        'تقارير خاصة',     // Special Reports
        'اقتصاد',          // Economy
        'ثقافة',           // Culture
        'رياضة',           // Sports
        'مقالات',          // Articles (opinion)
        'صحة',             // Health
        'منوعات'           // Variety
    ];
    foreach ( $sections as $section ) {
        if ( ! term_exists( $section, 'magazine_section' ) ) {
            wp_insert_term( $section, 'magazine_section', [ 'slug' => sanitize_title( $section ) ] );
        }
    }
}
register_activation_hook( __FILE__, 'magazine_create_default_sections' );
// 4. Shortcode: Display latest posts grouped by each section (magazine grid)
// Usage: [magazine_home] 
// Or: [magazine_home posts_per_section="4"]
function magazine_home_shortcode( $atts ) {
    $atts = shortcode_atts( [ 'posts_per_section' => 4 ], $atts );
    $sections = get_terms( [
        'taxonomy'   => 'magazine_section',
        'hide_empty' => false,
        'orderby'    => 'term_id',
    ] );
    if ( is_wp_error( $sections ) ) return '';
    ob_start();
    echo '<div class="magazine-grid-container" dir="rtl">'; // RTL support
    foreach ( $sections as $section ) {
        $posts = new WP_Query( [
            'post_type'      => [ 'article', 'review', 'interview' ],
            'posts_per_page' => intval( $atts['posts_per_section'] ),
            'tax_query'      => [[
                'taxonomy' => 'magazine_section',
                'field'    => 'term_id',
                'terms'    => $section->term_id,
            ]]
        ] );
        if ( $posts->have_posts() ) {
            echo '<div class="magazine-section">';
            echo '<h2 class="section-title"><a href="' . get_term_link( $section ) . '">' . esc_html( $section->name ) . '</a></h2>';
            echo '<div class="section-posts">';
            while ( $posts->have_posts() ) {
                $posts->the_post();
                ?>
                <div class="magazine-item">
                    <?php if ( has_post_thumbnail() ) : ?>
                        <div class="item-thumb"><?php the_post_thumbnail( 'medium' ); ?></div>
                    <?php endif; ?>
                    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <div class="item-meta"><?php echo get_post_type_object( get_post_type() )->labels->singular_name; ?> • <?php echo get_the_date(); ?></div>
                    <div class="item-excerpt"><?php the_excerpt(); ?></div>
                </div>
                <?php
            }
            echo '</div></div>';
            wp_reset_postdata();
        }
    }
    echo '</div>';
    return ob_get_clean();
}
add_shortcode( 'magazine_home', 'magazine_home_shortcode' );
// 5. Add a little CSS to make the grid look like a modern magazine
function magazine_frontend_css() {
    echo '<style>
    .magazine-grid-container { max-width: 1200px; margin: 0 auto; padding: 20px; font-family: "Tahoma", "Segoe UI", sans-serif; }
    .magazine-section { margin-bottom: 50px; }
    .section-title { font-size: 28px; border-right: 5px solid #c0392b; padding-right: 15px; margin-bottom: 20px; }
    .section-title a { text-decoration: none; color: #222; }
    .section-posts { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 25px; }
    .magazine-item { background: #fff; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 8px rgba(0,0,0,0.1); transition: 0.2s; }
    .magazine-item:hover { transform: translateY(-4px); box-shadow: 0 4px 12px rgba(0,0,0,0.15); }
    .item-thumb img { width: 100%; height: 180px; object-fit: cover; display: block; }
    .magazine-item h3 { font-size: 1.2rem; margin: 12px 15px 5px; }
    .magazine-item h3 a { color: #1a1a1a; text-decoration: none; }
    .item-meta { font-size: 0.8rem; color: #777; margin: 0 15px 8px; }
    .item-excerpt { font-size: 0.9rem; color: #444; margin: 0 15px 15px; line-height: 1.4; }
    @media (max-width: 768px) { .section-posts { grid-template-columns: 1fr; } }
    </style>';
}
add_action( 'wp_head', 'magazine_frontend_css' );

Comments

Add a Comment