Skip to content

Instantly share code, notes, and snippets.

@nncl
Last active November 29, 2016 18:28
Show Gist options
  • Save nncl/bfcfcd1e5760055c1af8 to your computer and use it in GitHub Desktop.
Save nncl/bfcfcd1e5760055c1af8 to your computer and use it in GitHub Desktop.
Here we'll teach you how to do some useful stuff with WordPress.

WordPress Stuff

Here we'll teach you how to do some useful stuff with WordPress.

What you'll see:

  1. [Loopings] (https://gist.github.com/nncl/bfcfcd1e5760055c1af8#loopings)
  2. [Categories] (https://gist.github.com/nncl/bfcfcd1e5760055c1af8#categories)
  3. [Dates] (https://gist.github.com/nncl/bfcfcd1e5760055c1af8#dates)
  4. [Pagination] (https://gist.github.com/nncl/bfcfcd1e5760055c1af8#pagination)
  5. [Related Posts] (https://gist.github.com/nncl/bfcfcd1e5760055c1af8#related-posts)
  6. [Author Signature] (https://gist.github.com/nncl/bfcfcd1e5760055c1af8#author-signature)
  7. [Comments] (https://gist.github.com/nncl/bfcfcd1e5760055c1af8#comments)
  8. [Tags] (https://gist.github.com/nncl/bfcfcd1e5760055c1af8#tags)
  9. [Building your own panel] (https://gist.github.com/nncl/bfcfcd1e5760055c1af8#building-your-own-panel)
  10. [Directories] (https://gist.github.com/nncl/bfcfcd1e5760055c1af8#directories)

Loopings

Regular Posts looping

    <?php query_posts('posts_per_page=5'.'&paged='.$paged);?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
        <h1><?php the title(); ?></h1>
        
        <img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id()); ?>" alt="<?php the_title() ?>" />
        
        <div>
            <?php the_excerpt(); ?>
        </div>
    
    <?php endwhile; else: ?>
    
        <h2>Nada Encontrado</h2>
    	
    <?php endif; ?>
    <?php wp_reset_query(); ?>

Categories

<?php
	// Getting the category/categories from this post
	$categories = get_the_category();
	$catname = '';
	$catlink = '';
	$array = array();
	if( $categories ) {
		foreach ($categories as $category) {
			$catname = $category->name;
			$catlink = get_category_link( $category->term_id );
			$array[] = '<a href="'.$catlink.'">'.$catname.'</a>';
		}
	}
?>

<span class="cat-link"><?php echo implode(', ' , $array) ?></span>

Dates

<?php the_time('d') ?> &#149; <?php the_time('m') ?> &#149; <?php the_time('Y') ?>

You can read more here.

Pagination

Let's build a function for it. The way we'll call this functions is like that - put it on your HTML file:

<?php if (function_exists("pagination")) {
  pagination($additional_loop->max_num_pages);
} ?>

On your functions.php, add this:

// pagination with NO plugin
function pagination($pages = '', $range = 4)
{
     $showitems = ($range * 2)+1;

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }

     if(1 != $pages)
     {
         echo "<div class=\"normal pagination\"><span>Página ".$paged." de ".$pages."</span>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; Primeira</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Anterior</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"page larger\">".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Última &raquo;</a>";
         echo "</div>\n";
     }
}

Related Posts

Let's build the PHP backend on the view itself.

<?php

$orig_post = $post;
global $post;
$categories = get_the_category($post->ID);

// Check if there is one or more categories
if ($categories) {
	$category_ids = array();
	
	foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
		$args=array(
			'category__in' => $category_ids,
			'post__not_in' => array($post->ID),
			'posts_per_page'=> 2,
			'caller_get_posts'=>1
		);
		
		$my_query = new wp_query( $args );
		
		if( $my_query->have_posts() ) {
			echo '<section class="related">';
			echo 	'<h3 class="title-coalhand simple-title">Talvez você goste de...</h3>';
			
			while( $my_query->have_posts() ) {
				$my_query->the_post();?>
				
				<h1><?php the_title(); ?></h1>
				
			<?php }
			
			echo '</section>';
			}
		}
		
		$post = $orig_post;
		wp_reset_query();
		
		?>

Author Signature

<?php
	$author_id=$post->post_author;
?>

<?php echo get_avatar( $author_id, 40 ); /* id, resolution/width */ ?>
<h2>Por <?php the_author_posts_link(); ?></h2>

Please, check it here too.

Comments

// WordPress Comments
<?php comments_template(); ?>

Also, you can get the number of the comments of a post. See:

<?php wp_count_comments( post_id ); ?> 

// Or with this way:
<?php comments_number( 'no responses', 'one response', '% responses' ); ?>.

Tags

For list all tags of a blog, we can do this:

<?php wp_tag_cloud(''); ?>

And from a post that it is inside of a loop:

<?php the_tags(); ?>

or

<?php the_tags( $before, $sep, $after ); ?>

We can customize the way the tags are displayed:

<?php the_tags( 'Tags: ', ', ', '<br />' ); ?>

or

<?php the_tags( '<ul><li>', '</li><li>', '</li></ul>' ); ?>

And many others. You can read more here.

Building your own panel

An example:

On your functions.php:

// create custom plugin settings menu
add_action('admin_menu', 'baw_create_menu');

function baw_create_menu() {

	//create new top-level menu
	add_menu_page('PA Configurações', 'Petit Andy Configurações', 'administrator', __FILE__, 'petit_settings_page', get_template_directory_uri() . '/assets/img/petitconfig.jpg');

	//call register settings function
	add_action( 'admin_init', 'register_mysettings' );
}


function register_mysettings() {
	//register our settings
	register_setting( 'petit-settings-group', 'new_option_name' );
	register_setting( 'petit-settings-group', 'some_other_option' );
	register_setting( 'petit-settings-group', 'option_etc' );
}

function petit_settings_page() { ?>
  <div class="wrap">
    <h2>Configurações</h2>

    <form method="post" action="options.php">
        <?php settings_fields( 'petit-settings-group' ); ?>
        <table class="form-table">
          <tr valign="top">
            <th scope="row">Example...</th>
            <td><input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" /></td>
          </tr>

          <tr valign="top">
            <th scope="row">Some Other Option</th>
            <td><input type="text" name="some_other_option" value="<?php echo get_option('some_other_option'); ?>" /></td>
          </tr>

          <tr valign="top">
            <th scope="row">Options, Etc.</th>
            <td><input type="text" name="option_etc" value="<?php echo get_option('option_etc'); ?>" /></td>
          </tr>
        </table>

        <p class="submit">
          <input type="submit" class="button-primary" value="<?php _e('Salvar') ?>" />
        </p>

    </form>
  </div>
<?php }

Directories

Use get_template_directory_uri function to link files.. Ex.:

<link rel="apple-touch-icon" type="image/png" href="<?php echo get_template_directory_uri() . '/favicon.png' ?>" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment