28

When your going through your loop add this: global $post; $post = get_post( $ID, OBJECT ); setup_postdata( $post ); //Do something wp_reset_postdata();


10

I would recommend you use home_url() to get the URL to the front page of the currently viewed site. If you want to get the main site URL (the network home), use network_home_url().


9

Yep, wp_trim_words(): <?php $trimmed = wp_trim_words( $text, $num_words = 55, $more = null ); ?> Or in your case: <?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 55 ) ); ?>


7

First you need to get all the assigned tag id:s by calling wp_get_post_tags because the include parameter in wp_tag_cloud only works with tags id, Not page id. So when you have all the id:s put them in the include parameter within the wp_tag_cloud like this: <?php // Get the assigned tag_id $tag_ids = wp_get_post_tags( $post->ID, array( '...


7

Since editor displays projected permalink for slug editor, it must have some way to figure it out. From looking at source that is handled by get_sample_permalink_html() and get_sample_permalink(). Since we only need link without form cruft, we can rework it into something like: function get_draft_permalink( $post_id ) { require_once ABSPATH . '/wp-...


5

Check for global variable $wp_customize: if ( empty ( $GLOBALS['wp_customize'] ) ) { // show something } Update: in WordPress 4.0, you can use is_customize_preview().


5

Try this simply code if (is_author()){ $author = get_queried_object(); $author_id = $author->ID; }


5

post_password_required(): whether post requires password and correct password has been provided. <?php if ( post_password_required() ) { echo 'xxx'; } else { echo 'zzz'; }


5

<?php echo get_bloginfo( 'url' )?> was exactly was I was looking for.


4

The Codex page for is_single should be updated, what you're seeing is correct behavior. According to the main Conditional Tags Codex page: is_single() When any single Post (or attachment, or custom Post Type) page is being displayed. (False for Pages) You can exclude attachments by also checking ! is_attachment(). Also, you may want to move that ...


4

Is there a specific reason to use get_parent_theme_file_path() over get_template_directory() Yes. This new function comes with a filter Filter parent_theme_file_path And argument to the file path If you take a look at the get_parent_theme_file_path() source, it's just returning get_template_directory() with extra functionality like if a file is given as ...


3

'p' is for querying a post, as opposed to 'page_id' for querying a page. It's documented in the WP_Query codex


3

Some post-related data is not available to get_posts by default, such as post content through the_content(), or the numeric ID. This is resolved by calling an internal function setup_postdata(), with the $post array as its argument: <?php $args = array( 'posts_per_page' => 3 ); $lastposts = get_posts( $args ); foreach ( $lastposts as $post ) : ...


3

A template tag is just a function, so I can't understand the difference from a function in functions.php and a template tag. So the choiches are 2: function VS file. The right choice depend case by case, and all things @shazzad pointed found me agree. I prefer use file and get_template_part when the code need to contain a lot of html, because I don't like ...


2

Ok, if you take a look at implementation of is_front_page, you will see the following: /** * Is the query for the front page of the site? * * This is for what is displayed at your site's main URL. * * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'. * * If you set a static page for the front page of ...


2

To specifically target single items of a particular post type use is_singular(): if( is_singular( 'post' ) ) { // Only execute this block on a single page for a post with type 'post' }


2

A shortcode is not supposed to echo its content, it's meant to return it e.g.: function show_review($atts) { return "this is a review form"; } add_shortcode('urp_review', 'show_review'); The function is attached to a filter, it filters/processes shortcodes/contents. If you do an echo, it prints out your code before the content has finished being ...


2

get_term_by() might help you here. $tag = get_term_by('id', 12, 'post_tag'); //or whatever taxonomy you are using echo $tag->name;


2

First line of the codex for get_content: Displays the contents of the current post. This template tag must be within The_Loop. Also, this line: <meta property="og:description" content="<?php string_limit_words(the_content(), 15);" ?> /> would need to get_the_content(), as the_content() will output, and you need to return. <meta ...


2

Check out get_next_post_sort and get_previous_post_sort filters. You can modify the ORDER BY clause to whatever you want (including ). For example, if you want to sort the 'next post' to be the post with the smallest adjacent post ID: function wp28041_get_next_post_sort($where){ return 'ORDER BY ID ASC LIMIT 1'; } add_filter('get_next_post_sort', '...


2

Alternatively global $wp_customize; if (isset($wp_customize)) { // do stuff }


2

You can use get_site_url() in combination with get_current_blog_id().


2

get_template_part() should be a correct choice. Because, If the template file is missing, it wont be triggering any error, just blank. Others/ or you can find the file by name for future edits Plugin can use the hook and change the file dynamically Where, using custom function could - trigger error if it's missing somehow It would take some time to find ...


2

You can make use of the items_wrap parameter to achieve this. For example like this: 'items_wrap' => '<ol id="%1$s" class="%2$s">%3$s</ol>' More information at codex: wp_nav_menu.


2

You can format it as, there's also no need to put the echo <?php next_post_link('%link',"<img src=" . CFS()->get('image_location') . "/>"); ?> CFS()->get('image_location') gets the field based on the current post id. If you want to use it for the next or previous, you have to pass the next or previous posts ID. You can do something like ...


2

You send your AJAX request to /wp-admin/admin-ajax.php (which is very good), so it's not a typical WP request. During such request, request is not parsed, no global WP_Query is created, and so on. is_author is a Conditional Tag, which checks if an Author archive page is being displayed. So, by design, it will return false during AJAX request, because AJAX ...


1

From Wordpress 4.0 you can use is_customize_preview();


1

Consider this snippet here to accomplish your task. If you add it to your functions.php file, the Open Graph tags will be populated automatically as part of the wp_head action.


1

That's because you don't have the $terms[0] element. Use following code to get the first key of $terms array: <?php // Set the pointer to the first element // you don't need this if there is the only array element reset($terms); // get the key of the current position $selected_id = key($terms); You MUST enable WP_DEBUG when on develop stage to see ...


1

To further what Rarst stated, named templates can be placed in a subfolder, as can partials (those consumed via get_template_part). In my experience, the built-in templates that WordPress normally looks for, such as header.php, single.php, and home.php, among others, must be located in the template root folder, not a subfolder. That said, my theme directory ...


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