-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
131 lines (95 loc) · 4.04 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/* ========================================================================
Register menus
=========================================================================*/
register_nav_menus( array(
'main-nav' => 'Main navigation',
'meta-nav' => 'Meta navigation'
) );
/* ========================================================================
END: Register menus
=========================================================================*/
/* ========================================================================
Register widget - sidebar
=========================================================================*/
function theme_slug_blog_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'Blog sidebar', 'theme_slug_blog' ),
'id' => 'blog-sidebar',
'description' => esc_html__( 'Add widgets here.', 'theme_slug_blog' ),
'before_widget' => '<div class="blog-sidebar-widgets">',
'after_widget' => '</div>',
'before_title' => '<h3 class="sidebar-subtitle">',
'after_title' => '</h3>',
));
}
add_action( 'widgets_init', 'theme_slug_blog_widgets_init' );
/* ========================================================================
END: Register widget - sidebar
=========================================================================*/
/* ========================================================================
Search function for posts
=========================================================================*/
function wpshock_search_filter( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array('post') );
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');
function cf_search_join( $join ) {
global $wpdb;
if ( is_search() ) {
$join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
}
return $join;
}
add_filter('posts_join', 'cf_search_join' );
/* Modify the search query with posts_where */
function cf_search_where( $where ) {
global $pagenow, $wpdb;
if ( is_search() ) {
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
}
return $where;
}
add_filter( 'posts_where', 'cf_search_where' );
/* Prevent duplicates */
function cf_search_distinct( $where ) {
global $wpdb;
if ( is_search() ) {
return "DISTINCT";
}
return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );
/* ========================================================================
End: Search function for posts
=========================================================================*/
/* ========================================================================
Add function for post thumbnail image
=========================================================================*/
add_theme_support( 'post-thumbnails' );
/* ========================================================================
End: Add function for post thumbnail image
=========================================================================*/
/* ========================================================================
Register Widget sidebar
=========================================================================*/
function custom_post_sidebar_widget() {
register_sidebar( array(
'name' => __( 'Single Post Sidebar', 'singlepost' ),
'id' => 'single_post_sidebar',
'description' => __( 'Widgets in this area will be shown on all posts.', 'singlepost' ),
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widge__ttitle">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'custom_post_sidebar_widget' );
/* ========================================================================
End: Register Widget sidebar
=========================================================================*/