Blog

WordPress PHP and MySQL Code Cheat Sheet
Posted on November 17, 2017 in PHP, WordPress by Matt Jennings

The Loop

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php the_content(); ?>

<?php endwhile; endif; ?>

Custom Post Types using Advanced Custom Fields (ACF)

<h2 class="clear">Featured Projects</h2>

    <ul class="portfolio-list">
      <?php
      // Portfolio Feed Featured Project Posts Custom Post Type START
      $featured_portfolio_posts_args = array(
            'post_type'         => 'portfoliopost',
            'orderby'           => 'meta_value',
            'meta_key'          => '_featured_project_order',
            'order'             => 'ASC',
            'posts_per_page'    => -1
      );

      $featured_portfolio_posts_lib_query = new WP_Query($featured_portfolio_posts_args);

      if($featured_portfolio_posts_lib_query->have_posts()) {
        while ($featured_portfolio_posts_lib_query->have_posts()) : $featured_portfolio_posts_lib_query->the_post();

          // Portfolio Project thumbnail image (200 x 125 pixels)
          // that is generated by ACF
          $portfolio_thumbnail_image = get_field('portfolio_thumbnail_image', $post->ID);

          $featured_portfolio_post_thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
          $featured_portfolio_category = strstr( get_post_meta($post->ID, '_portfolio_project_category', true), '&', true );
      ?>
          <?php // Show featured projects only ?>
          <?php if( get_post_meta($post->ID, '_featured_project_radio', true) ): ?>
            <li class="featured-project<?php echo get_post_meta($post->ID, '_featured_project_order', true); ?>" data-featured-project-category="<?php echo $featured_portfolio_category; ?>"><a href="<?php the_permalink(); ?>"><img src="<?php echo $portfolio_thumbnail_image['url']; ?>" alt="<?php the_title(); ?>" width="200" height="125" /><br /><?php echo get_post_meta($post->ID, '_portfolio_project_thumbnail_summary_line_1', true); ?><br /><?php echo get_post_meta($post->ID, '_portfolio_project_thumbnail_summary_line_2', true); ?><br /><?php echo get_post_meta($post->ID, '_portfolio_project_thumbnail_summary_line_3', true); ?></a></li>
          <?php endif; ?>
      <?php
        endwhile;
        wp_reset_postdata();
      }
      // Portfolio Feed Featured Project Posts Custom Post Type END
      ?>
    </ul>

Functions for the Header, Footer, and Sidebar

<?php get_header(); ?>

<?php get_footer(); ?>

<?php get_sidebar(); ?>

Get the Current Year in PHP

<?php echo date("Y"); ?>

Get the Current URL in PHP

$_SERVER['REQUEST_URI'];

Example Menu Code Inside header.php File Showing Separate Desktop and Mobile Menus

<?php wp_nav_menu(array('menu' => 'Desktop Menu', 'container' => 'nav', 'container_id' => 'desktop-nav')); ?>

<?php wp_nav_menu(array('menu' => 'Mobile Menu', 'container' => 'nav', 'container_id' => 'mobile-nav')); ?>

Enabling Dashicons in the functions.php File

// Add dashicons to the front end
function load_dashicons_front_end() {
    wp_enqueue_style( 'dashicons' );
}
add_action( 'wp_enqueue_scripts', 'load_dashicons_front_end' );

Enabling Dashicons in the header.php File

<span id="mobile-nav-button" class="dashicons dashicons-menu">Main Menu</span>

WordPress Data Structure

Some important tables in the WordPress database:
wp_posts – columns:
ID
post_content
post_name

wp_post_meta (holds extra info about that are used in the wp_posts table) – custom post types that related
post_id
meta_key
meta_value

wp_options
siteurl
home
wp_users

Hooks: Actions and Filters

Hooks are functions in WordPress that allow me to call others functions I create at specific times. There are two types of hooks in WordPress:

  1. Actions
  2. Filters

Action

A WordPress function that is executed at specific points throughout the WordPress core.

Filters

Filters are WordPress functions that allow us to get and modify WordPress data before sending it to the database/browser using custom functions.

Example Action that Adds New Items to the Admin Menu by Passing in a new_admin_menu Function

add_action ( 'admin_menu', 'new_admin_menu' );

Example Filter that Alters the_content of a WordPress Post by Passing in an altered_content Function

add_filter( 'the_content', 'altered_content' );

Get Home Page URL in WordPress

get_home_url() or the_home_url().

 

 

Leave a Reply