Customize any WordPress, add new custom post type, meta query, lookup
in order to customize any WordPress theme you need to have a wordpress installation and two plugins, for the first part we recommend greenbackend wordpress solution which will handle any WordPress website with all the different needs and capabilities, we recommend the Unlimited and Business Geko Pro wordpress hosting, you could check them here http://greenbackend.com/wordpress-hosting
Additionally, We need the following two plugins to manage the database and the UI:
1-Custom Post Type UI By WebDevStudios – link
2-Advanced Custom Fields By Elliot Condon – link
These two plugins are used to create new entities such as database tables and fields or columns, since WordPress is an EAV framework it uses WordPress tables and meta table to create the tables and the columns.
In this scenario, We will create a program table ” in WordPress we call it a program custom post type“.
1-program custom post type
2- After saving the program custom post type you will see it under the main menu with the given cpt properties
3- We need to add extra columns to the program post type
Logo – Image Field, it got Image URL in its return format
ٍSummary – Text Area
Gallery – Post object to load a woo commerce product and its images, Filter used is Product, return format is Post Object
facebook – Text Field, to store social profile link
Note: Its important to map the current field to be within the Location of Program, so Post type shoud be “is equal to” “Program” inside the Location Rules.
4- Save the field and open new or edit program screen you will see the following, just assign the product and different items to the program and you are done
In the previous links we’ve seen how to link program with other objects such as product, such that each program might have a product item, but some times a program might have many news, many events or activities, in this case we need to link the news item or event item with the program it self, this relation is called 1 to many whereas the program to product relation is called one to one.
5- in order to create the one to many relationship in WordPress we have to navigate to many and add it to one like the following:
The same rule applied for the events.
6- Link many to one, simply navigate to any post and link it with the program field and you are done.
You did a great job in the previous steps, now its time to make a page inside the theme and display the related fields, we will leave you a code snippets from the program template inside the theme:
1- Create a file named – single-program.php
2- Insert the following script to view the program
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
<?php /** * The Template for displaying all single program */ get_header(); $column_classes = qhub_get_columns_classes(); if ( ! ( get_post_format( get_queried_object_id() ) == 'video' ) ) : ?> <div id="content" class="<?php echo esc_attr( $column_classes['main_column_class'] ); ?>"> <?php endif; // video post check; ?> <style> ul.program-recent-posts { list-style: none; } ul.program-recent-posts li { list-style: none; display: block; float: left; position: relative; clear: both; } a.media-left { float: left; min-height: 80px; display: block; padding: 10px; border: 1px solid #efefef; margin: 10px; } ul.program-recent-posts { list-style: none; height: 100vw; position: relative; } .isotope-item{ float:left; margin-left:10px; } .item-layout-item-regular.col-lg-4.col-md-4.col-sm-6.col-xs-12.actions.business.dream.founder.popular.bottommargin_30 { float: left; width: 210px; height: 130px; } </style> <div> <div class="bootstrap-tabs "> <ul class="nav nav-tabs" role="tablist"> <li id="tablist1-tab1"> <a href="#tab-657f80519bf4f5dd8d9478c8186c47ed-0" role="tab" data-toggle="tab" aria-expanded="true"> <?php echo icl_t('greenbackend', 'gbe_txt_about', 'About');?> </a> </li> <li id="tablist1-tab2" class=""> <a href="#tab-657f80519bf4f5dd8d9478c8186c47ed-1" role="tab" data-toggle="tab" aria-expanded="false"> <?php echo icl_t('greenbackend', 'gbe_tab_events', 'Events');?> </a> </li> <li id="tablist1-tab3" class=""> <a href="#tab-657f80519bf4f5dd8d9478c8186c47ed-2" role="tab" data-toggle="tab" aria-expanded="false"> <?php echo icl_t('greenbackend', 'gbe_tab_news', 'News');?> </a> </li> <li id="tablist1-tab4" class=""> <a href="#tab-657f80519bf4f5dd8d9478c8186c47ed-3" role="tab" data-toggle="tab" aria-expanded="false"> <?php echo icl_t('greenbackend', 'gbe_tab_gallary', 'Gallery');?> </a> </li> </ul> <div class="tab-content "> <div class="tab-pane fade active in" id="tab-657f80519bf4f5dd8d9478c8186c47ed-0"> <?php //1-Get the field name from the ACF $logo = get_field( 'logo', $post->ID ); $summary = get_field( 'summary', $post->ID ); ?> <p id="qhub_bloginfo-2" class="widget widget_bloginfo"> <img width="100%" src="<?php echo $logo; ?>"> </p> <p class="summary" style=" font-weight: 400; line-height: 1.5em; font-size: 1.1em; "><?php echo $summary; ?></p> <?php // Start the Loop. while ( have_posts() ) : the_post(); /* * Include the post format-specific template for the content. If you want to * use this in a child theme, then include a file called called content-___.php * (where ___ is the post format) and that will be used instead. */ get_template_part( 'template-parts/content', get_post_format() ); // If comments are open or we have at least one comment, load up the comment template. if ( comments_open() || get_comments_number() ) { // comments_template(); } // Previous/next post navigation. Uncomment following line if you need post navigation qhub_post_nav(); endwhile; ?> </div> <!-- .eof tab-pane --> <div class="tab-pane fade" id="tab-657f80519bf4f5dd8d9478c8186c47ed-1"> <?php //2-Query All Events related to Program query_data($post->ID,"fw-event"); ?> </div> <!-- .eof tab-pane --> <div class="tab-pane fade" id="tab-657f80519bf4f5dd8d9478c8186c47ed-2"> <?php //3-Query All Posts related to Program query_data($post->ID,"post"); ?> </div> <!-- .eof tab-pane --> <div class="tab-pane fade" id="tab-657f80519bf4f5dd8d9478c8186c47ed-3"> <?php //3-Generate Product images for the program generate_gallary($post->ID,"product"); ?> </div> <!-- .eof tab-pane --> </div> </div> </div> </div><!--eof #content --> <!-- main aside sidebar --> <aside class="col-sm-3 col-md-4 col-lg-4 prosiderbar"> <div> <div class="textwidget custom-html-widget probuttons"> <a href="javascript:void(0)" class="theme_button color1 xtheme-tab-about" ><?php echo icl_t('greenbackend', 'gbe_txt_about', 'About');?></a> <a href="javascript:void(0)" class="theme_button color2 xtheme-tab-events"><?php echo icl_t('greenbackend', 'gbe_tab_events', 'Events');?></a> <a href="javascript:void(0)" class="theme_button color3 xtheme-tab-news"><?php echo icl_t('greenbackend', 'gbe_tab_news', 'News');?></a> <a href="javascript:void(0)" class="theme_button color2 inverse xtheme-tab-gallary"><?php echo icl_t('greenbackend', 'gbe_tab_gallary', 'Gallery');?></a></div> <?php $facebook = get_field( 'facebook', $post->ID ); $twitter = get_field( 'twitter', $post->ID ); $youtube = get_field( 'youtube', $post->ID ); $instagram = get_field( 'instagram', $post->ID ); ?> <ul class="icon-list"> <li><a href="<?php echo $facebook;?>"> <div class="teaser_icon "> <i class="social-icon soc-facebook"></i> </div> <span class="content"> </span></a> </li> <li><a href="<?php echo $twitter;?>"> <div class="teaser_icon "> <i class="social-icon soc-twitter"></i> </div> <span class="content"> </span></a> </li> <li><a href="<?php echo $youtube;?>"> <div class="teaser_icon "> <i class="social-icon soc-google"></i> </div> <span class="content"> </span></a> </li> <li><a href="<?php echo $instagram;?>"> <div class="teaser_icon "> <i class="social-icon soc-instagram"></i> </div> <span class="content"> </span></a> </li> </ul> </div> </aside> <!-- eof main aside sidebar --> <?php function generate_gallary($id,$type){ $gallery = get_field( 'gallery', $post->ID ); $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $gallery->ID, ); $attachments = get_posts( $args ); if ( $attachments ) { echo '<div class="row masonry-layout" data-filters=".isotope_filters-5d024de59687f" >'; foreach ( $attachments as $attachment ) { ?> <div class="item-layout-item-regular col-lg-4 col-md-4 col-sm-6 col-xs-12 actions business dream founder popular bottommargin_30" > <div class="vertical-item gallery-item content-absolute text-center"> <div class="item-media"> <div class="item-meida entry-thumbnail post-thumbnail"> <img width="770" height="515" src="<?php echo wp_get_attachment_image_url( $attachment->ID, 'full' ); ?>" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" sizes="(max-width: 770px) 100vw, 770px"> </div> <!-- .item-media --> <div class="media-links"> <div class="links-wrap"> <a class="p-view prettyPhoto " data-gal="prettyPhoto[gal-5d024de59687f]" href="<?php echo wp_get_attachment_image_url( $attachment->ID, 'full' ); ?>" rel="prettyPhoto[gal-5d024de59687f]"></a> <a class="p-link" href="<?php echo wp_get_attachment_image_url( $attachment->ID, 'full' ); ?>/" rel="prettyPhoto[gal-5d024de59687f]"></a> </div> </div> </div> <div class="item-content"> <h4 class="item-meta"> <a href="<?php echo wp_get_attachment_image_url( $attachment->ID, 'full' ); ?>" rel="prettyPhoto[gal-5d024de59687f]"> <?php echo apply_filters( 'the_title', $attachment->post_title ); ?> </a> </h4> </div> </div> </div> <?php } echo '</div>'; } } function query_data($id,$type){ $args = array( 'post_type' => $type, 'meta_key' => 'program', 'meta_query' => array( array( 'key' => 'program', 'value' => $id, 'compare' => 'LIKE' ), ), ); $query = new WP_Query( $args ); // Output. $results = array( 'total' => absint( $query->found_posts ), 'current_page' => absint( $args['page'] ) ? absint( $args['page'] ) : 1, 'pages' => array(), 'total_pages' => 1, 'threads' => array(), ); if ( $query->found_posts > 1 ) { $results['pages'] = range( 1, absint( ceil( absint( $query->found_posts ) / absint( $args['limit'] ) ) ) ); $results['total_pages'] = absint( count( $results['pages'] ) ); } $messages = $query->posts; $messages = is_array( $messages ) ? $messages : array(); $my_threads = array(); /* WP_Post Object ( [ID] => 1683 [post_author] => 1 [post_date] => 2019-03-17 13:20:27 [post_date_gmt] => 2019-03-17 11:20:27 [post_content] => This an example for event will take place in specific day and place [post_title] => Greenbackend Debate Tournament 2019 [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => greenbackend-debate-tournament-2019 [to_ping] => [pinged] => [post_modified] => 2019-06-13 14:09:19 [post_modified_gmt] => 2019-06-13 12:09:19 [post_content_filtered] => [post_parent] => 0 [guid] => http://greenbackend.com/?post_type=fw-event&p=1683 [menu_order] => 0 [post_type] => fw-event [post_mime_type] => [comment_count] => 0 [filter] => raw ) */ echo '<ul class="program-recent-posts ">'; // Loop all posts to get post object. foreach ( $messages as $_post ) { if ( has_post_thumbnail($_post->ID) ) { $featured_image = get_the_post_thumbnail_url($_post->ID); } ?> <li> <a href="<?php echo $_post->guid; ?>" class="media-left"> <img width="150" height="150" src="<?php echo $featured_image; ?>" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" sizes="(max-width: 150px) 100vw, 150px"> </a> <div class="media-body"> <h4 class="h6 item-title"><a href="<?php echo $_post->guid; ?>"><?php echo $_post->post_title; ?></a></h4> <div class="item-meta"> <span class="widget-post-date"> <?php echo $_post->post_modified; ?></span> </div> </div> </li> <? } echo '</ul>'; } ?> <script type="text/javascript"> jQuery(document).ready(function( $ ) { $("a[rel^='prettyPhoto']").prettyPhoto({ theme: 'facebook', slideshow: 5000, autoplay_slideshow: false }); $(document).on('click', '#tablist1-tab1', function () { removeSidebarClasses($); $(".xtheme-tab-about").addClass( "active"); }); $(document).on('click', '#tablist1-tab2', function () { removeSidebarClasses($); $(".xtheme-tab-events").addClass( "active"); }); $(document).on('click', '#tablist1-tab3', function () { removeSidebarClasses($); $(".xtheme-tab-gallary").addClass( "active"); }); $(document).on('click', '#tablist1-tab4', function () { removeSidebarClasses($); $(".xtheme-tab-contact").addClass( "active"); }); $(document).on('click', '.xtheme-tab-about', function () { $('#tablist1-tab1 a').click(); }); $(document).on('click', '.xtheme-tab-events', function () { $('#tablist1-tab2 a').click(); }); $(document).on('click', '.xtheme-tab-news', function () { $('#tablist1-tab3 a').click(); }); $(document).on('click', '.xtheme-tab-gallary', function () { $('#tablist1-tab4 a').click(); }); }); function removeContentClasses($){ $( ".tabcontent.responsive-tabs__panel" ).each(function() { $( this ).hide(); }); } function removeSidebarClasses($){ $( ".probuttons a" ).each(function() { $( this ).removeClass( "active" ); }); } </script> <?php get_footer(); |
Note: You might have noticed that we used “icl_t” which indicates that we’ve used a translatable WordPress strings, you could checkout in this link here a complete example how to use localizable strings within WordPress and wpml.
Wow, you did it, if you feel this lesson was helpful give us a message thru whats-app www.wtsb.me also you could give us a donation on Paypal at usd.paypal@greenbackend.com if you wish to see more free posts like these in the future.
Thanks for viewing this! <3 -Ahmad Naser