Monday, 30 November -0001 00:00

Modifying wordpress menu code

I've recently needed to modify the wordpress menu code so that I could insert bootstrap classes directly into it.

Add the following to the functions file:

// custom menu example @ https://digwp.com/2011/11/html-formatting-custom-menus/
function clean_custom_menus() {
	$menu_name = 'nav-primary'; // specify custom menu slug
	if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
		$menu = wp_get_nav_menu_object($locations[$menu_name]);
		$menu_items = wp_get_nav_menu_items($menu->term_id);

		$menu_list = '<nav>' ."\n";
		$menu_list .= "\t\t\t\t". '<ul>' ."\n";
		foreach ((array) $menu_items as $key => $menu_item) {
			$title = $menu_item->title;
			$url = $menu_item->url;
			$menu_list .= "\t\t\t\t\t". '<li><a href="'. $url .'">'. $title .'</a></li>' ."\n";
		}
		$menu_list .= "\t\t\t\t". '</ul>' ."\n";
		$menu_list .= "\t\t\t". '</nav>' ."\n";
	} else {
		// $menu_list = '<!-- no list defined -->';
	}
	echo $menu_list;
}

Make sure to replace the menu slug. To do this make sure the menu is assigned to a location on Wordpress. The menu slug is infact the location using lowercase and dashes (for example: footer-menu).

It might be necessary to create a new menu potion in the functions file using the following: 


function register_my_menu() {
register_nav_menu('journal',__( 'journal' ));
}
add_action( 'init', 'register_my_menu' );

 

To display the menu, insert the following into the theme:

<?php if (function_exists(clean_custom_menus())) clean_custom_menus(); ?>

More information on this function at this blog post

Published in Blog

The following solutions pertain to the excellent wordpress plug Advanced Custom Fields.

To display fields outside of teh wordpress posts loop use the following code but replace the post ID 123:


$value = get_field( "text_field", 123 );
echo $value;

More info here - and here's how to do it inside the loop.

What about repeater fields?

I'm glad you asked. Here's a PHP snippet to echo the values for a repeating field that I've called logo (just remember to change the ID 1862 to match the post or page ID):


while( have_rows('repeater', 1862) ): the_row();
the_sub_field('logo');
endwhile;

Output individual fields contained with a group field

Advanced Custom Fields allows the grouping of fileds into a single group field. The following will output individual sub fields (remember to replace the post ID):


$group_field = get_field( "my_group_field", 5563 );
echo $group_field['my_sub_field'];

Published in Blog

This is a super handy snippet that will display a specific pages content. It's very usefull to display content in a module or widget area outside of the main content area.

Published in Blog
Wednesday, 12 July 2017 08:29

Display a widget title

The following is a snippet of code I used to display a widget with the ID video title after the widget (rather than before the widget).


<?php $sidebar_id = 'video';
$sidebars_widgets = wp_get_sidebars_widgets();
$widget_ids = $sidebars_widgets[$sidebar_id];
foreach( $widget_ids as $id ) {
$wdgtvar = 'widget_'._get_widget_id_base( $id );
$idvar = _get_widget_id_base( $id );
$instance = get_option( $wdgtvar );
$idbs = str_replace( $idvar.'-', '', $id );
} ?>

<section id="video">
<div class="inner">
<div class="container">
<?php dynamic_sidebar( 'video' ); ?>
<?php echo $instance[$idbs]['title']; ?>
</div>
</div>
</section>

 

Published in Blog

A big thankyou to Evan Herman over at Code Parrots who helped me out with the following solution to an request that I submitted. He has developed a terrific timeline plugin for Wordpress, that is very flexible and looks super jazzy.

Published in Blog
Tuesday, 23 May 2017 02:17

Creating new editor styles in Tiny MCE

Here is a handy snippet that will allow you to create your own preformatted custom styles within the Wordpress text editor. The new styles are selected by a drop down field titled Formats. 

Published in Blog
Sunday, 21 May 2017 16:50

Modifying Wordpress user info

The following adds a bunch of usefull social media links to the wordpress user profiles - and also allows simple HTML within the user bio. Add this code to the theme functions.php file.

/*===================================================================================
* Add Author Links
* =================================================================================*/
function add_to_author_profile( $contactmethods ) {

$contactmethods['rss_url'] = 'RSS URL';
$contactmethods['google_profile'] = 'Google Profile URL';
$contactmethods['twitter_profile'] = 'Twitter Profile URL';
$contactmethods['facebook_profile'] = 'Facebook Profile URL';
$contactmethods['instagram_profile'] = 'Instagram Profile URL';
$contactmethods['linkedin_profile'] = 'Linkedin Profile URL';

return $contactmethods;
}
add_filter( 'user_contactmethods', 'add_to_author_profile', 10, 1);
//disable WordPress sanitization to allow more than just $allowedtags from /wp-includes/kses.php
remove_filter('pre_user_description', 'wp_filter_kses');
//add sanitization for WordPress posts
add_filter( 'pre_user_description', 'wp_filter_post_kses');

 

Now dial it all up within the author.php file of your theme with the following code:

<div id="description" class="node"><?php the_author_meta('description'); ?></div>


<ul class="icons">
<?php
$site = get_the_author_meta( 'user_url' );
if ( $site && $site != '' ) {
echo '<li class="site"><a target="_blank" href="' . esc_url($site) . '">Website</a></li>';
}


$rss_url = get_the_author_meta( 'rss_url' );
if ( $rss_url && $rss_url != '' ) {
echo '<li class="rss"><a target="_blank" href="' . esc_url($rss_url) . '">RSS</a></li>';
}

$google_profile = get_the_author_meta( 'google_profile' );
if ( $google_profile && $google_profile != '' ) {
echo '<li class="google"><a target="_blank" href="' . esc_url($google_profile) . '" rel="author">Google+</a></li>';
}

$twitter_profile = get_the_author_meta( 'twitter_profile' );
if ( $twitter_profile && $twitter_profile != '' ) {
echo '<li class="twitter"><a target="_blank" href="' . esc_url($twitter_profile) . '">Twitter</a></li>';
}

$facebook_profile = get_the_author_meta( 'facebook_profile' );
if ( $facebook_profile && $facebook_profile != '' ) {
echo '<li class="facebook"><a target="_blank" href="' . esc_url($facebook_profile) . '">Facebook</a></li>';
}

$linkedin_profile = get_the_author_meta( 'linkedin_profile' );
if ( $linkedin_profile && $linkedin_profile != '' ) {
echo '<li class="linkedin"><a target="_blank" href="' . esc_url($linkedin_profile) . '">Linkedin</a></li>';
}

$instagram_profile = get_the_author_meta( 'instagram_profile' );
if ( $instagram_profile && $instagram_profile != '' ) {
echo '<li class="Instagram"><a target="_blank" href="' . esc_url($instagram_profile) . '">Instagram</a></li>';
}
?>
</ul>
Published in Blog

Here's a very handy snippet to create a PHP conditional if statement based on a specific K2 item ID:


<?php
$itemid = JRequest::getInt('Itemid');

if( $itemid == 123 ) {
echo 'do something';
} else {
echo 'these are not the droids you are looking for';
}
?>


<?php
$itemid = JRequest::getInt('Itemid');

if( $itemid == 123 ) {
echo 'do something';
} else {
echo 'these are not the droids you are looking for';
}
?>

The code above can be altered using the following hooks from the page URL:


$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
$layout = JRequest::getCmd('layout');
$page = JRequest::getCmd('page');
$task = JRequest::getCmd('task');
$id = JRequest::getInt('id');

Published in Blog
Thursday, 27 April 2017 05:44

Get entry type. Is it a post or a page?

Here's a handy little snippet that determines if an entry is a post or page within a loop. I found this usefull for determining the entry type within search results, where I wanted to style the results differently depending on what type of result it was.


<?php
$pagetype = get_post_type( $post_id );

echo $pagetype;

if ($pagetype == "post") {
// do something
}
?>

Published in Blog
Tuesday, 21 February 2017 11:46

Custom post types in a loop

The following creates a loop of custom post types. This example creates a testimomnials block. 


<?php $loop = new WP_Query( array( 'post_type' => 'testimonial',
'posts_per_page' => 4 )
); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<?php
$clientname = (types_render_field( "client-name", array( 'raw' => true) ));
$quote = (types_render_field( "quote", array( 'raw' => true) ));
?>

<p><?php echo $quote; ?>&nbsp;<span><?php echo $clientname; ?></span></p>

<?php endwhile; wp_reset_query(); ?>

Check this article for more information

Published in Blog
Page 1 of 3