Skip to content

Instantly share code, notes, and snippets.

@robbiegod
Last active October 10, 2017 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbiegod/50668f266082bb93c599 to your computer and use it in GitHub Desktop.
Save robbiegod/50668f266082bb93c599 to your computer and use it in GitHub Desktop.
Wordpress Short Title Function and Google Analytics Shortcode
<?php
/* Short Title
* return a short title with ellipsis if the title is too long
* accepts a maxchar value
* Usage Example: echo short_title( $maxchar = 100 );
* Place the code inside the loop and set the character limit. If the title is longer than the character limit set,
* it will display ellipsis at the end.
--------------------------------------------------------------------*/
function short_title( $maxchar = 90 ) {
$fullTitle = get_the_title();
// get the length of the title
$titleLength = strlen($fullTitle);
if($maxchar > $titleLength) {
return $fullTitle;
} else {
$shortTitle = substr( $fullTitle, 0, $maxchar );
return $shortTitle . " &hellip;";
}
}
/* Short Excerpt
* return a short excerpt with ellipsis if the excerpt is too long
* If excerpt is empty, then the content is used as backup
* accepts a maxchar value
* Usage Example: echo short_excerpt( $maxchar = 100 );
* Place the code inside the loop and set the character limit. If the excerpt is longer than the character limit set,
* it will display ellipsis at the end.
--------------------------------------------------------------------*/
function short_excerpt( $maxchar = 90 ) {
$get_excerpt = get_the_excerpt();
if(empty($get_excerpt)) {
$get_excerpt = get_the_content();
}
// get the length of the title
$excerptLength = strlen($get_excerpt);
if($maxchar > $excerptLength) {
return $get_excerpt;
} else {
$shortExcerpt = substr( $get_excerpt, 0, $maxchar );
return $shortExcerpt . " &hellip;";
}
}
// SHORTCODES
// This shortcode will allow you to add a link to a wysiwyg box and have event tracking added to the link
// Use like this, for example:
// [analyticsLink url='http://www.crosbymarketing.com' target='_blank' evcat='external_links' evaction='click' evlabel='home_learn_more' textlink='Learn More']
// target="_blank" is the default behavior for the link.
// ga_link is the default event category; click is the default event action; learn_more is the default event label; Read More is the default textlink
// It will output the hyperlink and use the textlink as the clickable text for the link. The event are the values you want to set for Google Analytics
if ( ! function_exists( 'analyticsLink' ) ) :
function analyticsLink( $atts ) {
extract( shortcode_atts( array(
'url' => 'http://www.crosbymarketing.com/',
'target' => '_blank',
'evcat' => 'ga_link',
'evaction' => 'click',
'evlabel' => 'learn_more',
'textlink' => 'Read More'
), $atts ) );
$alink = "<a href='".$url."' target='".$target."' onClick=\"ga('send','event','".$evcat."','".$evaction."','".$evlabel."');\">";
$alink .= $textlink;
$alink .= "</a>";
return $alink;
}
add_shortcode("analyticslink", "analyticsLink"); // name of shortcode, function called
endif; // ####
?>
<h2>Most Popular "<?php the_title('<em>','</em>'); ?>" Content</h2>
<p>This section displays the child pages of this section.</p>
<?php
// Define the array container
$data = array('child_page_array' => array());
// Load the child pages of this page
$args = array(
'post_type' => 'page',
'post_parent' => $page_id,
'posts_per_page' => 10,
'ignore_sticky_posts' => 1
);
$qry_child_pages = new WP_Query($args);
if( $qry_child_pages->have_posts() ):
while ($qry_child_pages->have_posts()) : $qry_child_pages->the_post();
$child_page_id = get_the_ID();
$page_icon = get_field('page_icon');
$child_page_title = get_the_title();
$child_page_slug = $post->post_name;
?>
<p><img src="<?php echo $page_icon; ?>"><br/><a href="<?php echo the_permalink(); ?>"><?php echo the_title(); ?></a></p>
<?php
// Fill the array with the slug and title of the child pages
$data['child_page_array'][$child_page_id] = array( // need to fix this.
'child_page_id' => $child_page_id,
'child_page_title' => $child_page_title,
'child_page_slug' => $child_page_slug
);
?>
<?php
endwhile;
endif;
?>
<?php
/*
[Repeater field with two relationship fields, one to choose parent page and the other to pick 6 related posts]
*/
?>
<?php
foreach($data['child_page_array'] as $child_page){
// setup the values from the array into variables
$child_page_id = $child_page['child_page_id'];
$child_page_title = $child_page['child_page_title'];
$child_page_slug = $child_page['child_page_slug'];
echo "<h3>".$child_page_title."</h3>";
// custom function for displaying posts tagged with the slug of this page
// alternately we can add in a text field and put a matching value to the tag if it turns out the page slug can't match the tag slug.
// Right now, this relies on that, but would be easy to change
display_child_pages( $child_page_slug );
}
wp_reset_query();
// Add this to your functions.php file
// Display 6 child pages using the child page id
function display_child_pages( $child_page_slug ) {
// Load the child pages of this page
$args = array(
'post_type' => 'post',
'posts_per_page' => 6,
'ignore_sticky_posts' => 1,
'tag' => $child_page_slug
);
$qry_by_tag = new WP_Query($args);
if( $qry_by_tag->have_posts() ):
echo "<ul>";
while ($qry_by_tag->have_posts()) : $qry_by_tag->the_post();
$qbt_title = get_the_title();
$qbt_permalink = get_the_permalink();
echo '<li><a href="'.$qbt_permalink.'">'.$qbt_title.'</a></li>';
endwhile;
echo "</ul>";
endif;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment