-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordpress_code.php
More file actions
297 lines (218 loc) · 8.57 KB
/
wordpress_code.php
File metadata and controls
297 lines (218 loc) · 8.57 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
Template Header/Bloginfo Tags
* <?php bloginfo('url'); ?> – Your site’s URL
* <?php bloginfo('stylesheet_url'); ?> – Link to your themes’s stylesheet file
* <?php bloginfo('template_url'); ?> – Location of your site’s theme file
* <?php site_url(); ?> – The root URL for your site
https://premium.wpmudev.org/blog/developer-super-cheat-sheet/
creating the template shortcode in wordpress
function portfolio_shortcode() {
ob_start();
get_template_part('portfolios');
return ob_get_clean();
}
add_shortcode( 'portfolio', 'portfolio_shortcode');
displaying the all post and featured image and link to it
<?php
$loop = new WP_Query(array('post_type' => 'post', 'posts_per_page' => -1));
$count = 0;
if ($loop) :
while ($loop->have_posts()) : $loop->the_post();
?>
<ul class="nav">
<li><a href="<?php the_permalink() ?>"> <?php the_title(); ?> </a></li>
<li><a href="<?php the_permalink() ?>"> <?php the_post_thumbnail(); ?> </a></li>
</ul>
<?php
endwhile;
else: ?><div>no post images</div><?php
endif;
?>
Create Custom Page
<?php
/*
Template Name: Name of Template
*/
?>
wp image path
<img src="<?php bloginfo('template_url'); ?>/asset/img/img.jpg">
get Template Part
<?php get_template_part( 'foo','bar' ); ?>
<?php echo get_home_url(); ?> --- home url
<?php echo do_shortcode('[CONTACT-US-FORM]'); ?>
include ----- ../wp-content/themes/your-theme-slug/foo-bar.php
Trimming post content
<? php echo wp_trim_words( get_the_content(), 40, '...' ); ?>
add image size thumbnail
<? php add_image_size( 'wordpress-thumbnail', 200, 200, array( 'center', 'center' ) ); ?>
Create Custom Post
<?php
add_action('init', 'project_custom_init_Blog');
function project_custom_init_Blog()
{
$labels = array(
'name' => _x('Blog', 'post type general name'),
'singular_name' => _x('Blog', 'post type singular name'),
'add_new' => _x('Add New', 'Blog'),
'add_new_item' => __('Add New Blog'),
'edit_item' => __('Edit Blog'),
'new_item' => __('New Blog'),
'view_item' => __('View Blog'),
'search_items' => __('Search Blog'),
'not_found' => __('No Blog found'),
'not_found_in_trash' => __('No Blog found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Blog'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','author','thumbnail','comments')
);
// The following is the main step where we register the post.
register_post_type('blog',$args);
// Initialize New Taxonomy Labels
$labels = array(
'name' => _x( 'Category', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Types' ),
'all_items' => __( 'All Category' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category Name' ),
);
// Custom taxonomy for Project Tags
register_taxonomy('category_blog',array('blog'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'blog-category' ),
));
}
Custom logo upload
<a href='<?php echo esc_url(home_url('/')); ?>' title='<?php echo esc_attr(get_bloginfo('name', 'display')); ?>' rel='home'>
<img src='<?php echo esc_url(get_theme_mod('m1_logo')); ?>' alt='<?php echo esc_attr(get_bloginfo('name', 'display')); ?>'>
</a>
Function.php
function m1_customize_register( $wp_customize ) {
$wp_customize->add_setting( 'm1_logo' ); // Add setting for logo uploader
// Add control for logo uploader (actual uploader)
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'm1_logo', array(
'label' => __( 'Upload Logo (replaces text)', 'm1' ),
'section' => 'title_tagline',
'settings' => 'm1_logo',
) ) );
}
Add Span tag in Text Editor
add_action( 'customize_register', 'm1_customize_register' );
// for support i tag in wp text block
function override_mce_options($initArray) {
$opts = '*[*]';
$initArray['valid_elements'] = $opts;
$initArray['extended_valid_elements'] = $opts;
return $initArray;
}
add_filter('tiny_mce_before_init', 'override_mce_options');
Hide Admin Bar
// hide admin bar
add_filter('show_admin_bar', '__return_false');
Add Script in function.php
add_action('wp_footer','addmyScript');
function addmyScript() {
?>
<script>
jQuery(document).ready(function() {
});
</script>
<?php
}
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt) >= $limit) {
array_pop($excerpt);
$excerpt = implode(" ", $excerpt) . '...';
} else {
$excerpt = implode(" ", $excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt);
return $excerpt;
}
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content) >= $limit) {
array_pop($content);
$content = implode(" ", $content) . '...';
} else {
$content = implode(" ", $content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
then in your template code you just use..
<?php echo excerpt(25); ?>
/* Wordpress Loop */
<?php
$args = array(
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if ($post_query->have_posts()) {
while ($post_query->have_posts()) {
$post_query->the_post();
?>
<div class="card news-section">
<a href="<?php the_permalink() ?>">
<?php if (has_post_thumbnail($post->ID)): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail'); ?>
<img class="thumbnail_img" src="<?php echo $image[0]; ?>">
<?php endif; ?>
<div class="data">
<h3><?php the_title(); ?></h3>
<p class="time"><?php the_date(); ?> | <?php the_author(); ?></p>
<p><?php echo excerpt(25); ?></p>
</div>
</a>
</div>
<?php
}
}
?>
/* thumbnail Attachedment url */
<?php
$post_thumbnail_id = get_post_thumbnail_id($post->ID);
$post_thumbnail_url = wp_get_attachment_url($post_thumbnail_id);
?>
<a href="<?php echo $post_thumbnail_url; ?>">
<?php if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?>
<?php echo get_the_post_thumbnail( $post->ID, 'thumbnail' ); ?>
<?php endif; ?>
</a>
<a href="<?php echo $post_thumbnail_url; ?>">
/* create custom menu location */
function wpb_custom_new_menu() {
register_nav_menus(
array(
'topbar-menu' => __( 'Topbar Menu' ),
'footer-menu' => __( 'Footer Menu' )
)
);
}
add_action( 'init', 'wpb_custom_new_menu' );
// Custom Thumbnail size
add_image_size( 'product-thumbnail', 300, 300, true );
<?php echo get_the_post_thumbnail( $product->get_ID(), 'product-thumbnail' ); ?>