WordPress Kategori Bazlı Arşiv Sayfası Oluşturma

W

Size şimdide küçük bir eklenti ile arşiv sayfası oluşturmayı öğreteceğim. Bu arşiv sayfası her ay açılan konuları değilde kategorilerdeki konularınızı yenisi üstte olacak şekilde göstermeye yarıyor sadece 3 aşamada bunu yapmak mümkün…

1. Aşama: öncelikle aşağıdaki kodları kopyalayın

[php]

<?php
/*
Plugin Name: Archives by Selected Categories
Plugin URI: http://www.dagondesign.com/articles/archives-by-selected-categories-plugin-for-wordpress/
Description: Generates a simple archive of posts by selected categories - a very basic version of my sitemap generator plugin.
Author: Dagon Design
Version: 1.2
Author URI: http://www.dagondesign.com
*/
 
$ddabsc_version = '1.2';
 
// Setup defaults if options do not exist
add_option('ddabsc_cats', '');
add_option('ddabsc_cats_ex', '');
 
function ddabsc_add_option_pages() {
    if (function_exists('add_options_page')) {
        add_options_page('Archives by Selected Categories', 'DDArchivesSelCats', 8, __FILE__, 'ddabsc_options_page');
    }
}
 
function ddabsc_options_page() {
 
    global $ddabsc_version;
 
    if (isset($_POST['set_defaults'])) {
        echo '<div id="message"><p><strong>';
 
        update_option('ddabsc_cats', '');
        update_option('ddabsc_cats_ex', '');
 
        echo 'Default Options Loaded!';
        echo '</strong></p></div>';
 
    } else if (isset($_POST['info_update'])) {
 
        echo '<div id="message"><p><strong>';
 
        update_option('ddabsc_cats', (string)$_POST["ddabsc_cats"]);
        update_option('ddabsc_cats_ex', (string)$_POST["ddabsc_cats_ex"]);
 
        echo 'Configuration Updated!';
        echo '</strong></p></div>';
 
    } ?>
 
    <div>
 
    <h2>Archives by Selected Categories v<?php echo $ddabsc_version; ?></h2>
 
    <p>For information and updates, please visit:<br />
    <a href="http://www.dagondesign.com/articles/archives-by-selected-categories-plugin-for-wordpress/">http://www.dagondesign.com/articles/archives-by-selected-categories-plugin-for-wordpress/</a></p>
 
    <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
    <input type="hidden" name="info_update" id="info_update" value="true" />
 
    <h3>Options</h3>
    <table width="100%" border="0" cellspacing="0" cellpadding="6">
 
    <tr valign="top"><td width="20%" align="right">
        <strong>Categories</strong>  &nbsp;
    </td><td align="left">
        <input name="ddabsc_cats" type="text" size="50" value="<?php echo stripslashes(htmlspecialchars(get_option('ddabsc_cats'))) ?>"/>
        <br />- A comma-separated list of category IDs you want listed
        <br />- Leave blank for all categories
        <br /><br />
    </td></tr>
 
    <tr valign="top"><td width="20%" align="right">
        <strong>Excluded Categories</strong>  &nbsp;
    </td><td align="left">
        <input name="ddabsc_cats_ex" type="text" size="50" value="<?php echo stripslashes(htmlspecialchars(get_option('ddabsc_cats_ex'))) ?>"/>
        <br />- A comma-separated list of category IDs you want excluded
    </td></tr>
 
    </table>
 
    <div>
        <input type="submit" name="set_defaults" value="<?php _e('Load Default Options'); ?> &raquo;" />
        <input type="submit" name="info_update" value="<?php _e('Update options'); ?> &raquo;" />
    </div>
 
    </form>
    </div><?php
}
 
function ddabsc_generate() {
 
    global $wpdb;
    $table_prefix = $wpdb->prefix;
 
    $t_out = '';
 
    $ddabsc_cats = trim(get_option('ddabsc_cats'));
    $ddabsc_cats_ex = trim(get_option('ddabsc_cats_ex'));
 
    // generate array of selected cats
 
    $cats = array();
    if ($ddabsc_cats == '') {   // all cats
 
        $cat_list = (array)$wpdb->get_results("
            SELECT {$table_prefix}term_taxonomy.term_id as catID
            FROM {$table_prefix}posts, {$table_prefix}term_relationships, {$table_prefix}term_taxonomy, {$table_prefix}terms
            WHERE {$table_prefix}posts.ID = {$table_prefix}term_relationships.object_id
            AND {$table_prefix}term_relationships.term_taxonomy_id = {$table_prefix}term_taxonomy.term_taxonomy_id
            AND {$table_prefix}terms.term_id = {$table_prefix}term_taxonomy.term_id
            AND {$table_prefix}term_taxonomy.taxonomy = 'category'
            GROUP BY catID
        ");
        foreach ($cat_list as $c) {
            $cats[] = $c->catID;
        }
 
    } else {    // just selected cats
 
        $cats_tmp = (array)explode(',', $ddabsc_cats);
        foreach ($cats_tmp as $c) {
            $cats[] = $c;
        }
 
    }
 
    // generate array of excluded cats
 
    $cats_ex = array();
    if ($ddabsc_cats_ex != '') {
        $cats_tmp = (array)explode(',', $ddabsc_cats_ex);
        foreach ($cats_tmp as $c) {
            $cats_ex[] = $c;
        }
    }
 
    // get diff of two cats
 
    $new_cats = array_diff($cats, $cats_ex);
 
    // generate code
 
    $cat_code = '';
    if (count($new_cats) > 0) {
        foreach ($new_cats as $c) {
            $tmp_cats[] = " {$table_prefix}terms.term_id = " . $c . " ";
        }
        $cat_code = ' AND ( ' . implode(' OR ', $tmp_cats) . ' ) ';
    }
 
    $post_list = (array)$wpdb->get_results("
        SELECT ID, post_date, post_title, {$table_prefix}terms.name as catname
        FROM {$table_prefix}posts, {$table_prefix}term_relationships, {$table_prefix}term_taxonomy, {$table_prefix}terms
        WHERE {$table_prefix}posts.ID = {$table_prefix}term_relationships.object_id
        AND {$table_prefix}term_relationships.term_taxonomy_id = {$table_prefix}term_taxonomy.term_taxonomy_id
        AND {$table_prefix}terms.term_id = {$table_prefix}term_taxonomy.term_id
        AND {$table_prefix}term_taxonomy.taxonomy = 'category'
        {$cat_code}
        AND post_status = 'publish'
        AND post_type != 'page'
        ORDER BY catname, post_date DESC
    ");
 
    if (count($post_list) > 0) {
 
        $last_cat = '';
 
        foreach ($post_list as $p) {
 
            $id = $p->ID;
            $catname = $p->catname;
            $link = get_permalink($p->ID);
            $title = $p->post_title;
            $date = date("F j, Y", strtotime($p->post_date));
 
            if ($last_cat != $catname) {
                if ($last_cat != '') {
                    $t_out .= '</ul>';
                }
                $t_out .= '<p><strong>' . $catname . '</strong></p><ul>';
                $last_cat = $catname;
            }
 
            $t_out .= '<li><a href="' . $link . '" title="' . $title . '">' . $title . '</a> - ' . $date . '</li>';
 
        }
        $t_out .=  '</ul>';
 
    }
 
    return $t_out;
}
 
function ddabsc_check($content) {
 
    if (strpos($content, "<!-- ddarchivesbycat -->") !== FALSE) {
        $content = preg_replace('/<p>\s*<!--(.*)-->\s*<\/p>/i', "<!--$1-->", $content);
        $content = str_replace("<!-- ddarchivesbycat -->", ddabsc_generate(), $content);
    }
 
    return $content;
}
 
add_action('admin_menu', 'ddabsc_add_option_pages');
add_filter('the_content', 'ddabsc_check');
 
?>
[/php]

2.Aşama: şimdi ” dd-archives-sel-cats.php ” isminde bir .php uzantılı dosya oluşturup kopyaladığınız kodları içerisine yapıştırın ve kaydedin.

<!-- ddarchivesbycat -->

3.Aşama: sonra dosyayı Plugsin Klasörüne upload edin. Sonra eklentiyi admin panelinden aktif hale getirin ve yeni bir sayfa oluşturun sayfanın içeriğine aşağıdaki kodu yazdığınızda arşiviniz hazır olacaktır güle güle kullanın.

Yazar hakkında

Murat Dinç

Bozuktus.com, sistemgereksinimleri.net, klavyer.com sitelerinin kurucusu, muratdinc.com'da kişisel blog yazarı, codex.dincmedya.com.tr coder, seo.muratdinc.com'da SEO uzmanı.

3 yorum

  • Bu sayede de uzun yazılarımda yararıma olucak bi exstra eklenti oldu çok teşekkür ederim.

  • Ben bunu kategori şeklinde yapmıştım kod kullanmadan. Açılır kategoriye önce kategori arşivi yapmıştım daha sonra yıl olarak değiştirdim
    Arşiv
    2018 Arşivi
    2017 Arşivi gibi

    siteadresi.com/2018 yaptığınızda 2018 yılında yazılan bütün yazılar çıkıyor ama liste şeklinde olmuyor. Buradaki kodlar ise kategoriye ait bütün yazıları bir sayfada liste şeklinde gösteriyor sanırım. Bunu öğrendiğim gerçekten iyi oldu boş bir vaktimde deneyeceğim.
    Teşekkürler bilgilendirme için.