Hot answers tagged

135

COMPLETE GUIDE TO EXCERPTS I've recently answered a few questions regarding excerpts, so I'm going to give a detailed explanation covering as much as I can. PREFACE There seems to be a couple of questions arising from this answer on where the code should go, and the answer is, it is really up to you and how you see fit. There are are a couple of options ...


133

get_permalink() is only really useful for single pages and posts, and only works inside the loop. The simplest way I've seen is this: global $wp; echo home_url( $wp->request ) $wp->request includes the path part of the URL, eg. /path/to/page and home_url() outputs the URL in Settings > General, but you can append a path to it, so we're appending the ...


72

When a post is updated there are some hooks that are fired: 'pre_post_update' is an action fired just before the post is updated, the argument passed are 2: $post_ID and $data that is an array of all the other database colums of the post table 'transition_post_status' is an hook fired on update, and pass 3 arguments: $new_post_status, $old_post_status and $...


57

Use get_page_by_path($page_path): $page = get_page_by_path( 'about' ); echo get_the_title( $page ); This will return a regular post object. Documentation: https://developer.wordpress.org/reference/functions/get_page_by_path/ https://developer.wordpress.org/reference/functions/get_the_title/


56

You need to pass an additional argument to get_terms(). The default is to hide "empty" terms-- terms which are assigned to no posts. $terms = get_terms([ 'taxonomy' => $taxonomy, 'hide_empty' => false, ]);


55

Its pretty easy one you have worked out the data added in the post meta. Trouble I am having is adding downloadable products to the store. below is the code i am using it lists all the post meta that is used by woo commerce. This publishes a product however the download link will not attach. Originally when i started i made an error with the array that ...


47

If by 'blog page' you meant a static page set as posts page in the Reading Settings, then you could check it by doing this: if ( is_front_page() && is_home() ) { // Default homepage } elseif ( is_front_page() ) { // static homepage } elseif ( is_home() ) { // blog page } else { //everyting else } When you use is_home() and is_front_page(),...


38

Please reference the Codex entry for post/page parameters for WP_Query(). The 'p' parameter takes a single post ID, as an integer. To pass an array of posts, you need to use 'post__in': $myarray = array(144, 246); $args = array( 'post_type' => 'ai1ec_event', 'post__in' => $myarray ); // The Query $the_query = new WP_Query( $args );


38

A prettier solution would be to disable the capability of creating a custom_post_type: Simply pass the the parameter 'create_posts' => 'do_not_allow', in the capabilities array when calling register_post_type. $args = array( 'label' => __( 'Custom Post Type', 'text_domain' ), 'description' => __( 'Custom Post ...


37

I think the best balance between efficiency, and using proper wordpress methods for adding javascript would be adding something along these lines to your themes functions.php file. For Example: functions.php: function load_scripts() { global $post; if( is_page() || is_single() ) { switch($post->post_name) // post_name is the post ...


36

On a category page, you can use the function single_cat_title(), or the more generic single_term_title(). These functions pull from the global $wp_query object, via get_queried_object().


32

Use get_posts() and the parameter name which is the slug: $page = get_posts( array( 'name' => 'your-slug' ) ); if ( $page ) { echo $page[0]->post_content; } Be aware that the post type in get_posts() defaults to 'post'. If you want a page use … $page = get_posts( array( 'name' => 'your-slug', 'post_type' => 'page' ...


31

While I strongly advise against this, and it will not speed things up, your usage is incorrect. WordPress already caches these things in the object cache, you don't need to store the result and reuse, WP does that already. It's very likely your code is running slower as a result of this micro-optimisation, not faster! How To Use Globals When you try to ...


31

I know I'm three months late, but the function you want here is WordPress' get_date_from_gmt(). The function accepts a GMT/UTC date in Y-m-d H:i:s format as the first parameter, and your desired date format as the second parameter. It'll convert your date to the local timezone as set on the Settings screen. Example usage: echo get_date_from_gmt( date( 'Y-...


31

If you are merely trying to get a count, $wpdb->get_var(); along with using COUNT() in your sql will be better: ### Search for IP in database function postviews_get_ip($id, $ip) { global $post, $wpdb; $rowcount = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->wp_postviews_ips WHERE postid = $id AND ip = '$ip'"); return $rowcount; } ...


30

Just change the post_type bit to: 'post_type' => array('testimonial', 'other_post_type', 'another-post-type'), Assuming that taxonomy is valid across all 3 post types. Otherwise you'll have to leave that out. Why? You can pass an array to post_type field.


28

Something I forgot in my previous comment was that shortcodes return content, both the suggested include and my alternative get_template_part will directly output the content (which is what you are seeing with the content appearing at the top of your page instead of where the shortcode is called). To counteract this we must use output buffering. Define the ...


28

To disable user email notification, add this in a plugin or theme: add_filter( 'send_password_change_email', '__return_false' ); FYI wp_password_change_notification() controls admin email notification when a user changes their password


28

The error message shows that the class description_walker extends the Walker_Nav_Menu class and overrides the start_el() method, but the signature differs from the method definition in the parent class. As mentioned in comments, it would be best to contact the developer of your theme and ask for a corrected version of the theme. If the developer cannot be ...


27

You could check to see if the 'WooCommerce' class exists, then run the code that requires WooCommerce. <?php if ( class_exists( 'WooCommerce' ) ) { // code that requires WooCommerce } else { // you don't appear to have WooCommerce activated } ?>


23

First, on function widget: function widget( $args, $instance ) { extract( $args ); // Add this line $your_checkbox_var = $instance[ 'your_checkbox_var' ] ? 'true' : 'false'; // Change 'your_checkbox_var' for your custom ID // ... } On function update: function update( $new_instance, $old_instance ) { $instance = $old_instance; /...


21

This is not recommended in any WordPress style guide, and I think it is a bad coding style. Beginners are using this style, maybe because it feels more like HTML … Unfortunately, the default themes are using this style way too often, so some beginners might think it is part of a code style. One disadvantage of this style is comment handling. Look closely ...


21

Don't use global variables, as simple as that. Why not to use globals Because the use of globals makes it harder to maintain the software in the long term. A global can be declared anywhere in the code, or nowhere at all, therefor there is no place in which you can instinctivly look at to find some comment about what the global is used for While reading ...


21

You can use the following in your themes functions.php file: function is_blog () { return ( is_archive() || is_author() || is_category() || is_home() || is_single() || is_tag()) && 'post' == get_post_type(); } And then put this in the file you are checking: <?php if (is_blog()) { echo 'You are on a blog page'; } ?> You can use Hooks in ...


21

They are defined as follows: define( 'ABSPATH', dirname(dirname(__FILE__)) . '/' ); define( 'WPINC', 'wp-includes' ); dirname is a PHP function that returns the path of the parent directory, and wp-includes is pretty self explanatory. I would say ABSPATH is better because it's one of the first things WP loads and it also looks better:) But there is no ...


21

While I don't like taking credit for someone else's answer, I also don't like leaving a question without answer when the answer does exist and it's already been given. I hope Milo won't mind if I copy the answer from his comment. The register_taxonomy calls should be hooked to init.


20

The unfortunate situation is that there are indeed two different options: Newer timezone_string, which saves PHP–style time zone. Older gmt_offset, which saves numeric float offset in hours. But in newer environments timezone_string actually overrides gmt_offset, the value returned by the latter will be based on the former. However the opposite isn't true —...


20

Previous answer didn't worked for me (says that code is invalid, on wp login page), probably because answer is 1,5 yr old, and something is changed in WP code, so I have updated this code a bit (also from wp-login.php), here it is: function retrieve_password($user_login){ global $wpdb, $wp_hasher; $user_login = sanitize_text_field($user_login); ...


20

Updating the posts $all_posts = get_posts( 'posts_per_page' => -1, 'post_type' => 'post' ); foreach ( $all_posts as $single ) { wp_update_post( array( 'ID' => $single->ID, 'post_title' => to_title_case( $single->post_title ) // see function below )); } Converting a string to "Title Case" And, while not ...


20

Ok I seem to have got it working. I added this in the functions file... //Reposition WooCommerce breadcrumb function woocommerce_remove_breadcrumb(){ remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20); } add_action( 'woocommerce_before_main_content', 'woocommerce_remove_breadcrumb' ); function ...


Only top voted, non community-wiki answers of a minimum length are eligible