CodeHQ

Archive for the ‘PHP’ Category

« Older Entries | Newer Entries »

Query custom post type based on custom field | WordPress

Monday, October 5th, 2015

<?php 
$cityname = get_field('city_name');

$args = array(
	'posts_per_page' => '100',
	'post_type' 	 => 'neighborhood',
	'meta_key'	 => 'city_n',
	'meta_value'	 => $cityname,
	'order'	         => 'asc'
);

$cityquery = new WP_Query($args);
$i = 1;
while ( $cityquery->have_posts() ) : $cityquery-> the_post();?>

Tags: , , , ,
Posted in PHP, WordPress | No Comments »

Adding a back to parent item link | WordPress

Thursday, July 30th, 2015

<?php
$this_page = get_post($id);
$parent_id = $this_page->post_parent;
if ($parent_id) {
	$parent = get_page($parent_id);
	echo '';
}
?>

Tags: , , ,
Posted in PHP, WordPress | No Comments »

Display only child posts of parent | WordPress Query

Monday, July 27th, 2015

$args = array(
	'post_type' => 'page',
	'post_parent' => $post->ID,
);

Tags: , , , ,
Posted in PHP, WordPress | No Comments »

Strip content from a post (with character limit)

Monday, February 16th, 2015

<?php echo substr(strip_tags($post->post_content), 0, 140);?>

Tags:
Posted in PHP, WordPress | No Comments »

Alt tags on images that use custom fields

Saturday, December 13th, 2014

$image = get_field('case_tile_image');

<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

Posted in HTML, PHP, WordPress | No Comments »

Exclude category from loop with name

Saturday, December 13th, 2014

$exclude = get_cat_ID('News'); 

$q = 'cat=-'.$exclude; query_posts($q);

Posted in PHP, WordPress | No Comments »

WordPress pagination

Saturday, December 13th, 2014

//paste this where the pagination must appear

global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ( $total > 1 )  {
     // get the current page
     if ( !$current_page = get_query_var('paged') )
          $current_page = 1;
     // structure of "format" depends on whether we're using pretty permalinks
     if( get_option('permalink_structure') ) {
	     $format = '&paged=%#%';
     } else {
	     $format = 'page/%#%/';
     }
     echo paginate_links(array(
          'base'     => get_pagenum_link(1) . '%_%',
          'format'   => $format,
          'current'  => $current_page,
          'total'    => $total,
          'mid_size' => 4,
          'type'     => 'list'
     ));
}
// CSS Code - add this to the style.css file
.navigation { list-style:none; font-size:12px; }
.navigation li{ display:inline; }
.navigation li a{ display:block; float:left; padding:4px 9px; margin-right:7px; border:1px solid #efefef; }
.navigation li span.current { display:block; float:left; padding:4px 9px; margin-right:7px; border:1px solid #efefef; background-color:#f5f5f5;  }	
.navigation li span.dots { display:block; float:left; padding:4px 9px; margin-right:7px;  }	

Posted in CSS, PHP, WordPress | No Comments »

Post by taxonomy term

Saturday, December 13th, 2014

$terms = wp_get_post_terms( $post->ID , 'type' );

if ( $terms != null ){
 foreach( $terms as $term ) {
	$args = array(
		'post_type' => 'portfolio',
		'posts_per_page' => '3',
		'tax_query' => array (
			array (
				'taxonomy' => 'type',
				'field' => 'slug',
				'terms' =>  $term->slug
			)
		)
	);
	
}}

$query = new WP_Query($args);
$i = 1;
while ( $query->have_posts() ) : $query->
the_post(); 

Posted in PHP, WordPress | No Comments »

Shortcodes

Saturday, December 13th, 2014

Code not escaping properly – download this this file.

/* Add Button Shortcode */

function button($atts, $content = null) {
    extract(shortcode_atts(array(
        "link" => '#',
        "style" => 'btn-primary'
    ), $atts));
    return ''.$content.'';
}
add_shortcode("button", "button");
[button link="http://google.com"]Clicky clicky[/button]

Custom shortcode which includes php file example here

Posted in PHP, WordPress | No Comments »

A WordPress query

Saturday, December 13th, 2014

A WordPress query that can be amended – like below for different post types/amount of posts/categories etc – codex info here

$args = array(
	'post_type' => 'post',
	'posts_per_page' => '16',
	'orderby' => 'rand'
);

$query = new WP_Query($args);

 $i = 1;

while ( $query->have_posts() ) : $query-> the_post();

Posted in PHP, WordPress | No Comments »

Next Page »« Previous Page