by Garth | Mar 17, 2019 | PHP, Wordpress
<?php
$category = get_terms('gallery_albums');//custom category name
foreach ($category as $catVal) {
$AlbumName[] = $catVal->name;
}
echo "<pre>";
print_r($AlbumName);
echo "</pre>";
?>
by Garth | Mar 4, 2019 | PHP, Wordpress
<?php
$category = get_terms('task_categories');//custom category name
foreach ($category as $catVal) {
echo '<h2>'.$catVal->name.'</h2>';
}
?>
by Garth | Jan 9, 2019 | PHP, Wordpress
WPBeginner
function posts_for_current_author($query) {
global $pagenow;
if( 'edit.php' != $pagenow || !$query->is_admin )
return $query;
if( !current_user_can( 'edit_others_posts' ) ) {
global $user_ID;
$query->set('author', $user_ID );
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
The post title says it all…
Note: If you cut down admin privileges for user roles as well you can use the admin section for front end posting. Just remember you need to include admin styles differently when its for the admin section. You need a script for the functions.php file.
by Garth | Sep 3, 2018 | PHP, Wordpress
<?php edit_post_link('Button Text'); // Always handy to have Edit Post Links available ?>
by Garth | Sep 3, 2018 | PHP, Wordpress
Firstly you have your while loop to run through all your posts.
And above your while loop you have your args.
You need to add this arg
$args['paged'] = $paged;
Below is a template to start your while loop
$args['post_type'] = 'post';
$query = new WP_Query($args);
while ( $query->have_posts() ) : the_post(); ?>
<?php
$query->the_post();
$post_id = get_the_ID();
?>
<div class="card">
<a href="<?php the_permalink(); ?>" class="card-header text-center">
<?php
the_title();
?>
</a>
<div class="card-body">
<?php
the_content();
?>
</div>
</div>
<?php endwhile; ?>
Your Args must be before your Query!
Then after your endwhile. Paste this code to display your pagination links
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Previous', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Next', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
by Garth | Jul 24, 2018 | PHP, Snippets, Wordpress
Here’s the Stackoverflow link
Here’s the improved answer
//The Function
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
$found = 'yes';
}
}
$found = 'no';
return false;
}
//The Function in use
//Set the result as a var so you can check it.
$found = in_array_r($search_pre_order_date,$dates_array) ? 'found' : 'not found';
//If the result is equal to something do something...
if ($found == 'found') {
echo 'Yes this item is in the array';
}elseif ($found == 'not found') {
echo 'Nope not in the array';
}
by Garth | Jul 23, 2018 | PHP, Snippets, Wordpress
add_action( 'admin_head', 'remove_my_meta_boxen' );
function remove_my_meta_boxen() {
remove_meta_box( 'postimagediv', 'countries', 'side' );
add_meta_box('postimagediv', __('Country Flag'), 'post_thumbnail_meta_box', 'countries', 'side', 'high');
}
The post type in this example is countries
by Garth | Jul 22, 2018 | PHP, Plugins, Wordpress
Meta Box – WordPress Custom Fields Framework
The code below relates to the meta box plugin. And the code below relates to 2 custom post types. Players and Coaches. And then adding meta fields to those custom post types.
// Custom Post 1
register_post_type( 'coaches',
array(
'labels' => array(
'name' => __( 'Coaches' ),
'singular_name' => __( 'Coach' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail')
)
);
register_taxonomy_for_object_type( 'category', 'coaches' );
// Custom Post 2
register_post_type( 'players',
array(
'labels' => array(
'name' => __( 'Players' ),
'singular_name' => __( 'Player' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail')
)
);
register_taxonomy_for_object_type( 'category', 'players' );
// Meta Fields
function add_meta_fields( $meta_boxes ) {
$meta_boxes[] = array(
'id' => 'twitter_url',
'title' => 'Twitter Url' ,
'post_types' => array( 'players','coaches' ),
'context' => 'normal',
'autosave' => false,
'fields' => array(
array(
'id' => 'twitter_url',
'type' => 'url',
'name' => 'URL',
),
),
);
$meta_boxes[] = array(
'id' => 'instagram_url',
'title' => 'Instagram Url' ,
'post_types' => array( 'players','coaches' ),
'context' => 'normal',
'autosave' => false,
'fields' => array(
array(
'id' => 'instagram_url',
'type' => 'url',
'name' => 'URL',
),
),
);
$meta_boxes[] = array(
'id' => 'facebook_url',
'title' => 'Facebook Url' ,
'post_types' => array( 'players','coaches' ),
'context' => 'normal',
'autosave' => false,
'fields' => array(
array(
'id' => 'facebook_url',
'type' => 'url',
'name' => 'URL',
),
),
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'add_meta_fields' );
Below is how to add a location/Address field
// Address
$meta_boxes[] = array(
'id' => 'address',
'title' => 'Address' ,
'post_types' => array( 'countries' ),
'context' => 'normal',
'autosave' => false,
'fields' => array(
array(
'id' => 'address',
'name' => 'Address',
'type' => 'text',
),
array(
'id' => 'map',
'name' => 'Location',
'type' => 'map',
// Default location: 'latitude,longitude[,zoom]' (zoom is optional)
'std' => '-6.233406,-35.049906,15',
// Address field ID
'address_field' => 'address',
// Google API key
'api_key' => 'AIzaSyBVKag0eBXXIjS0rvi6qUYZ9sScw5kNeXQ',
),
),
);
by Garth | Jul 16, 2018 | Debugging, PHP, Snippets
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
by Garth | Jul 13, 2018 | CSS, PHP, Wordpress
// Styles and Scripts
add_action('admin_head', 'my_custom_styles_and_scripts');
function my_custom_styles_and_scripts() {
echo '
<script src="/wp-content/plugins/webfootprint-stock-manager-old/assets/js/scripts.js"></script>
<link href="/wp-content/plugins/webfootprint-stock-manager-old/assets/css/styles.css" rel="stylesheet">
';
}