Skip to content

Instantly share code, notes, and snippets.

@serkanalgur
Forked from bulentsakarya/taxonomy-sehir.php
Last active May 23, 2018 08:46
Show Gist options
  • Save serkanalgur/c7afa08f1c982ed5f7fb7cdc087028d5 to your computer and use it in GitHub Desktop.
Save serkanalgur/c7afa08f1c982ed5f7fb7cdc087028d5 to your computer and use it in GitHub Desktop.
<?php
//Aşağıdaki kodlar functions.php içine eklenecek
add_action( 'generate_rewrite_rules', 'ilan_ek_kurallar_temiz' );
function ilan_ek_kurallar_temiz( $wp_rewrite ) {
$new_rules = array(
'products/([^/]+)/?$' => 'index.php?sehir=' . $wp_rewrite->preg_index( 1 ),
'products/([^/]+)/([^/]+)/?$' => 'index.php?post_type=ilan&sehir=' . $wp_rewrite->preg_index( 1 ) . '&ilan=' . $wp_rewrite->preg_index( 2 ),
'products/([^/]+)/([^/]+)/page/(\d{1,})/?$' => 'index.php?post_type=ilan&sehir=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 3 ),
'products/([^/]+)/([^/]+)/([^/]+)/?$' => 'index.php?post_type=ilan&sehir=' . $wp_rewrite->preg_index( 2 ) . '&ilan=' . $wp_rewrite->preg_index( 3 ),
'products/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$' => 'index.php?post_type=ilan&sehir=' . $wp_rewrite->preg_index( 3 ) . '&ilan=' . $wp_rewrite->preg_index( 4 ),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function alt_kategorileri_duzenleyen_kural($query) {
if ( isset( $query['post_type'] ) && 'ilan' == $query['post_type'] ) {
if ( isset( $query['ilan'] ) && $query['ilan'] && isset( $query['sehir'] ) && $query['ilan'] ) {
$query_old = $query;
if ( 'page' == $query['sehir'] ) {
$query['paged'] = $query['name'];
unset( $query['sehir'], $query['name'], $query['ilan'] );
}
$query['fields'] = 'ids';
$query['posts_per_page'] = 1;
$_query = new WP_Query( $query );
if ( ! $_query->posts ) {
$query = array( 'sehir' => $query['ilan'] );
if ( isset( $query_old['sehir'] ) && 'page' == $query_old['sehir'] ) {
$query['paged'] = $query_old['name'];
}
}
}
}
return $query;
}
add_filter( 'request', 'alt_kategorileri_duzenleyen_kural', 10 );
function son_bir_yazilar_icin_duzenleme($link, $post)
{
if ($post->post_type != 'ilan')
return $link;
if ($cats = get_the_terms($post->ID, 'sehir'))
{
$link = str_replace('%sehir%', get_taxonomy_parents(array_pop($cats)->term_id, 'sehir', false, '/', true), $link); // see custom function defined below\
$link = str_replace('//', '/', $link);
$link = str_replace('http:/', 'http://', $link);
}
return $link;
}
add_filter('post_type_link', 'son_bir_yazilar_icin_duzenleme', 10, 2);
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {
$chain = '';
$parent = get_term($id, $taxonomy);
if (is_wp_error($parent)) {
return $parent;
}
if ($nicename)
$name = $parent -> slug;
else
$name = $parent -> name;
if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {
$visited[] = $parent -> parent;
$chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);
}
if ($link) {
// nothing, can't get this working :(
} else
$chain .= $name . $separator;
return $chain;
}
<?php
//Aşağıdaki eklemeler post type ve taxonomy tanımına eklenmeli
//Post Type için
'rewrite' => array( 'slug' => 'ilan/%sehir%', 'hierarchical' => true, 'with_front' => false )
//Taxonomy için
'rewrite' => array( 'slug' => 'sehir', 'hierarchical' => true, 'with_front' => false ),
<?php
$sehiri = get_queried_object();
if($sehiri->parent != 0 ) {
$ilce_selected = $sehiri->slug;
$slug_getir = get_term_by('id',$sehiri->parent,'sehir');
$sehir_selected = $slug_getir->slug;
$yazi = $sehiri->name ." İlanlar";
$child = $sehiri->parent;
} else {
$ilce_selected = '';
$sehir_selected = $sehiri->slug;
$yazi = $sehiri->name ." İlanlar";
$child = $sehiri->term_id;
}
?>
<form action="<?php esc_url(bloginfo('url')); ?>/" method="get">
<?php
$args = array(
'taxonomy' => 'sehir',
'name' => 'sehir',
'show_option_all' => 'Şehir Seçiniz',
'hierarchical' => 1,
'depth' => 1,
'show_count' => 0,
'orderby' => 'name',
'order' => 'ASC',
'echo' => 0,
'value_field' => 'slug',
'hide_empty' => 1,
'selected' => $sehir_selected
);
$select = wp_dropdown_categories($args);
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<noscript><input class="rtd-btn primary" type="submit" value="View" /></noscript>
</form>
<form action="<?php esc_url(bloginfo('url')); ?>/" method="get">
<?php
$args2 = array(
'taxonomy' => 'sehir',
'name' => 'sehir',
'show_option_all' => 'İlçe Seçiniz',
'hierarchical' => 1,
'depth' => 1,
'show_count' => 0,
'orderby' => 'name',
'order' => 'ASC',
'echo' => 0,
'value_field' => 'slug',
'hide_empty' => 1,
'selected' => $ilce_selected,
'child_of' => $child
);
$select2 = wp_dropdown_categories($args2);
$select2 = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select2);
echo $select2;
?>
<noscript><input class="rtd-btn primary" type="submit" value="View" /></noscript>
</form>
<h1><?php echo $yazi; ?></h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment