Showing posts with label Wordpress. Show all posts
Showing posts with label Wordpress. Show all posts

Monday, November 25, 2013

Solution to order pages/posts specific by id in array with WP Query

Have you had problem with not getting the specific posts displaying in the order you have wanted it to be. Normally the Wordpress WP Query is sorted by the post title but what if you don't want to order the query and just have it "un-ordered", the way you write the id's to be.

The solution to this problem is to order your result with the pages to get.

$you_query = new WP_Query(array('post_type' => 'page', 'post__in' => 'array(12, 10, 18, 22)', 'orderby' => 'post__in'));

So that's how you order your result with your array or string of posts. No manual need of ordering or any other hassling.

Wednesday, November 13, 2013

Bootstrap navbar as your Wordpress menu

Do you want to use the bootstrap navbar as a menu in Wordpress? No problems. It takes just a few seconds to config.

What we want to achieve is to use the bootstrap navbar. No plugin is required but we need to download the helper file to achieve this. That one is called wp-bootstrap-navwalker - "A custom Wordpress walker to implement the Twitter Bootstrap dropdown navigation using the Wordpress built in menu manager." That can be found here and the direct download location of the navbar here. Just note that this is for bootstrap 3.0+ and if you have the legacy version (2.3.2) then you need another version of the helper.

Include the file from your functions.php-file. The file should be placed somewhere in your theme-folder.

require_once('wp_bootstrap_navwalker.php');

Then you need to update your wp_nav_menu() in your theme.
    wp_nav_menu( array(
        'menu'              => 'primary',
        'theme_location'    => 'primary',
        'depth'             => 2,
        'container'         => 'div',
        'container_class'   => 'collapse navbar-collapse navbar-ex1-collapse',
        'menu_class'        => 'nav navbar-nav',
        'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',
        'walker'            => new wp_bootstrap_navwalker())
    );

"primary" is the name of your menu that you have probably already defined somewhere in your functions.php-file but if you don't have done that this is how you could do it:

register_nav_menus( array(    'primary' => __( 'Primary menu', 'yourthemename' ),) );

That's it. But you may need to put some surrounding code to get the appearance you look for. It could be something like this in the source of your theme-file:

<div class="navbar navbar-default navbar-fixed-top" role="navigation"><nav>    <div class="container">        <div class="navbar-header">            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">                <span class="sr-only">Toggle navigation</span>                <span class="icon-bar"></span>                <span class="icon-bar"></span>                <span class="icon-bar"></span>            </button>            <a class="navbar-brand" href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a>        </div>
        <?php            wp_nav_menu( array(                'menu'              => 'primary',                'theme_location'    => 'primary',                'depth'             => 2,                'container'         => 'div',                'container_class'   => 'collapse navbar-collapse navbar-ex1-collapse',                'menu_class'        => 'nav navbar-nav',                'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',                'walker'            => new wp_bootstrap_navwalker())            );        ?>    </div></nav></div>

Heads up! This menu does only support 2-level of depths. Check out the links above to learn more about the navbar.

Monday, November 11, 2013

Custom image sizes missing in the media up-loader in Wordpress administration

Does your Wordpress media uploader in the administration miss the custom images sizes that you have configured? Don't worry.

In Wordpress 3.5 something seemed to have changed and any custom image sizes weren't available to select when inserting an image to a post or page. I really don't know what have changed but I know the solution and it's quite simple.

The thing you need to do is to create an filter in your functions.php that modify the Wordpress built-in function "image_size_names_choose()" and add all your custom image size to the "list".

Add this following code to your functions.php-file:

function my_custom_sizes( $sizes ) {    return array_merge( $sizes, array(        'custom-300' => __('Custom 300')    ) );}add_filter( 'image_size_names_choose', 'my_custom_sizes' );
That's it!

And if your don't remember how to add a custom image size in Wordpress. It's simple. Add this code to your functions.php-file:

add_image_size( 'custom-300', 300, 300 );

Wednesday, November 6, 2013

Change or modify elements in the content in Wordpress

Have you ever wanted to change the output of a Wordpress page or a post? Maybe modify the H1-elements in the content or something else. The solution is quite simple to achieve this.

Lets say we wanted to add something, in this case a header-element before the H1-element and a closing one right after. This is how you do it:

Put this code below in your functions.php-file:
function filter_h1_pages( $content ) {
$content = str_ireplace( '<h1>', '<header><h1>', $content ); return str_ireplace( '</h1>', '</header></h1>', $content );}add_filter( 'the_content', 'filter_h1_pages' );

What the code does is replacing a string with something else, twice and then return it. So we have filtered the main function the_content in Wordpress.

Friday, October 4, 2013

Woocommerce - Removing or changing fields in edit shipping or billing addresses.

Have you had problems with alter/remove/change or add fields for the edit shipping or edit billing addresses? Doing a search on this topic seems like I wasn't the only one who had the problem. But I've the solution for how to do this.

The first problem I had was to to know what function that controlled this fields. But pretty soon I found out that they where in fact described in the woocommerce docs.
The shipping fields exists in this function : woocommerce_shipping_fields and the billing fields exists in this function: woocommerce_billing_fields.

So the method to change these fields are then fairly easy to do. This is an example code in the functions.php-file on how to remove an unnecessary field "shipping state" and to set an label to the "shipping address 2":

add_filter( 'woocommerce_shipping_fields' , 'custom_override_shipping_fields' );
function custom_override_shipping_fields( $fields ) {

unset($fields['shipping_state']);
$fields['shipping_address_2']['label'] = 'Address Field 2';

     return $fields;
}

The same solution goes for billing fields. 
If you want to alter any fields on the checkout page you could use this method also. The function for those fields lays in this function "woocommerce_checkout_fields". An heads up: the array $fields is one step deeper and therefore if you want to remove the shipping_state like in the example above then you would need to to it like this: "unset($fields['shipping']['shipping_state']);" - notice the extra bracket. 

If you would like to know which fields exist for e.g. the shipping fields function, then use the php function var_dump(). IE: Put "var_dump($fields)" inside the custom override function and it will show you how the array looks like and all the settings for all fields.


Monday, September 10, 2012

Image slider, vSlider to Wordpress not function in IE or Chrome

Are you having problems with the image slider plugin vSlider 4.0 or another Wordpress image slider plugin? The problem I'll describe here is that the images won't be shown / displayed in Internet Explorer and Chrome but works fine in FireFox.

Then the problem can be that the plugin is configured with an "auto resize" setting. Try with unset this and see if the problem disappear.

Hope that will help you out here.

Monday, August 15, 2011

Custom time or interval for Cron to Wordpress

If you want to specify another interval to run a cron job in Wordpress beside the 3 built in you can do so. The custom code for specify another cron interval is:

add_filter( 'cron_schedules', 'my_corn_schedules');
function my_corn_schedules(){
return array(
'per_minute' => array(
'interval' => 60,
'display' => 'Every Mintue'
)
);
}

Interval is how many seconds in between. And display is an value for naming the interval or custom cron interval. Remember that when you are creating a cron job with the built in cron function in Wordpress, the cron job triggers by a user when the user is visiting your website.

Wednesday, February 23, 2011

The Dagon Design Sitemap Generator plug-in for wordpress and WP_Error

I've had some trouble with the Dagon Design Sitemap Generator for Wordpress, version 3.15. When I installed it for my Wordpress 3.0 installation, I got an error on the page.

The error message i got was: Catchable fatal error: Object of class WP_Error could not be converted to string in .. \wp-content\plugins\sitemap-generator\sitemap-generator.php on line 513

It seems like I'm not the only one who has this problem when I googled for 'sitemap-generator.php on line 513'. Neither has the founder of the plug-in updated it lately so there seems like there is no update for this problem.

However I've found an solution or rather work around to this problem that seems to work.

What to do is to modify the PHP code with just a few lines.
The original code was on line 513:
return DDSG_CAT_HEADER . ' < a href="' . get_category_link($post_data[$p]['id']) . '" title="' . strip_tags($post_data[$p]['title']) . '">' . $post_data[$p]['title'] . '< /a>';

Now the modifications that needs to be done are:

if(is_string($p))
return DDSG_CAT_HEADER . ' < a href="' . get_category_link($post_data[$p]['id']) . '" title="' . strip_tags($post_data[$p]['title']) . '">' . $post_data[$p]['title'] . '< /a>';
else
return false;


The only thing you need to do with this is to remove the white-spaces after '<' in the code because Blogger will treat that as a link so I had to make an space in between.

The problem seems to be empty categories, but I'm not sure.

Sunday, December 5, 2010

Empty < p> paragraph and < br /> removes in Wordpress tinyMCE editor

Do you want a empty line break and you are having the problem that p paragraphs and br are removing / stripping out in your posts in Wordpress? So had I.

The solution to get around this problem was however very simple and one way to solve this was to add the TinyMCE Advanced plugin and then check the box, "Stop removing the p and br tags when saving and show them in the HTML editor".

Easy fix