Hot answers tagged

64

Try global $post; echo $post->ID; or (I don't know the difference) global $wp_query; echo $wp_query->post->ID;


44

Prefix everything, especially your variables. The names you are using are used by WordPress internally (see the comment form), so WordPress might try to do something with your form values. If that fails you get an error. While $_GET problems are usually rather easy to debug, $_POST and $_REQUEST names are not so simple. The following list is probably not ...


43

When a page is created, the assigned template to that page is saved as custom post meta in the same way as custom fields. The meta_key is _wp_page_template and the meta_value will be the page template You can simply make use of get_pages to retrieve all pages which have a meta_value of the specified template $pages = get_pages(array( 'meta_key' => '...


36

You can simply do, $page_id = get_queried_object_id();


33

Try with this: <?php echo get_the_title( $ID ); ?>


32

Just in Wordpress 4.9 there's this bug: https://core.trac.wordpress.org/ticket/42573 causing the template files to only be rescanned once every hour. To fix (until they release a new WP version with this changed), download the patch on that bug ticket and make the changes from the patch to wp-includes/class-wp-theme.php. Hope this saves someone the 2 hours ...


31

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' ...


29

You can use the wp hook and check the global $wp_query object or any conditional. add_action( 'wp', 'wpse69369_special_thingy' ); function wpse69369_special_thingy() { if ( 'special_cpt' === get_post_type() AND is_singular() ) return print "Yo World!"; return printf( '<p>Nothing to see here! Check the ...


26

So if you want to send that the reset password link and you have access to the code base you can use the following snippet and you can modify it further, actually that code is slightly modified version of wp-login.php /** * Handles sending password retrieval email to user. * * @uses $wpdb WordPress Database object * @param string $user_login User Login ...


26

Go to Screen Options at the top, and change the number to something like 200. I don't think it will survive much more than that.


25

I usually use this approach: wrong approach <?php query_posts( array( 'category_name' => 'news', 'posts_per_page' => 3, )); ?> <?php if( have_posts() ): while ( have_posts() ) : the_post(); ?> <?php the_excerpt(); ?> <?php endwhile; ?> <?php else : ?> <p><?php __('No News'); ?></p> &...


24

<?php // would echo post 7's content up until the <!--more--> tag $post_7 = get_post(7); $excerpt = $post_7->post_excerpt; echo $excerpt; // would get post 12's entire content after which you // can manipulate it with your own trimming preferences $post_12 = get_post(12); $trim_me = $post_12->post_content; my_trim_function( $trim_me ); ?&...


23

You can use is_page_template to check if you template is being used and load your scripts based on that ex: Add this code to your functions.php: add_action('wp_enqueue_scripts','Load_Template_Scripts_wpa83855'); function Load_Template_Scripts_wpa83855(){ if ( is_page_template('custom-page.php') ) { wp_enqueue_script('my-script', 'path/to/script....


23

I'm using two loops. First loop is to show the page content, and the second loop is to show the queried post contents. I commented into the codes where necessary. I emphasized into the loops, as Deckster0 said in WordPress support that, the_content() works only inside a WordPress Loop. I'm placing these code into a my own template: <?php /* * Template ...


22

Here you go ! <?php $my_id = 5369; $post_id_5369 = get_post($my_id); $content = $post_id_5369->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content; ?>


20

Incorrect: as of wordpress 3 you need something akin to: $args = array( 'post_type' => 'page', 'meta_query' => array( array( 'key' => '_wp_page_template', 'value' => 'my_template.php' ) ) );


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); ...


19

post_excerpt is actually the caption attribute. Here is the correct answer: $title = get_post(get_post_thumbnail_id())->post_title; //The Title $caption = get_post(get_post_thumbnail_id())->post_excerpt; //The Caption $description = get_post(get_post_thumbnail_id())->post_content; // The Description


19

A faster solution is: $post = array( 'ID' => $post_id, 'post_status' => $status ); wp_update_post($post); This way you don't have to get the post.


19

Create a file install.php in your wp-content directory. In that file you declare the wp_install_defaults() function. If you just leave that function empty no default categories, links, posts or pages will be created. The default (pluggable) wp_install_defaults() function can be found in wp-admin/includes/upgrade.php. Also see: https://stackoverflow.com/...


19

Best practice method Have a look at wp_localize_script, which is meant to do exactly that. But it does require previous usage of wp_enqueue_scripts, hence you will need to move your JS to a separate file indeed. It will be worth those few minutes of effort though, for sure. function wpse_96370_scripts() { if ( is_single() ) { ...


18

THE ONLY RELIABLE WAY: if( is_wplogin() ){ ... } code: function is_wplogin(){ $ABSPATH_MY = str_replace(array('\\','/'), DIRECTORY_SEPARATOR, ABSPATH); return ((in_array($ABSPATH_MY.'wp-login.php', get_included_files()) || in_array($ABSPATH_MY.'wp-register.php', get_included_files()) ) || (isset($_GLOBALS['pagenow']) && $GLOBALS['...


18

Use template_redirect which is the action hook that fires before rendering the template; add_action('template_redirect', 'hooker'); function hooker(){ //I load just before selecting and rendering the template to screen }


17

Here's a way to support pagination titles of the form: <!--nextpage(.*?)?--> in a simlar way as the core supports <!--more(.*?)?-->. Here's an example: <!--nextpage Planets --> Let's talk about the Planets <!--nextpage Mercury --> Exotic Mercury <!--nextpage Venus--> Beautiful Venus <!--nextpage Earth --> Our Blue ...


17

Problem Description: Let me rephrase the question first. You want to: Set new title to all post and page type from a meta field. You want this to happen everywhere (home page, single page, widgets etc.) However, you don't want this title change to happen if the title is on the Navigation Menu. Solution: Before I give you the CODE, let me explain a few ...


16

You can try following code: get_post_field( 'menu_order', $post_id);


15

If you have the post with an $id: $thispost = get_post($id); $menu_order = $thispost->menu_order; WordPress itself does not provide a function to get the menu_order, so you have to query the post-Object. If you are outside the loop, you can use the above function, however inside the loop you could also achieve this by: global $post; $menu_order = $post-...


15

The first thing to do to implements such task is to be able to recognise which page an user can edit. There are different ways to do it. It could be a user meta, some configuration value... For the sake of this answer, I will assume that a function lile this exists: function wpse_user_can_edit( $user_id, $page_id ) { $page = get_post( $page_id ); //...


14

You need your own helper function which will return you what you need. It could be like this one: function wpse8170_loop() { global $wp_query; $loop = 'notfound'; if ( $wp_query->is_page ) { $loop = is_front_page() ? 'front' : 'page'; } elseif ( $wp_query->is_home ) { $loop = 'home'; } elseif ( $wp_query->...


14

In WordPress 4.2 the editor was removed on whichever page was assigned to show Latest Posts for whatever reason. The following function below ( original solution found here by crgeary ) will re-add the editor and remove the notification: You are currently editing the page that shows your latest posts. Here's some information on the hooks used: ...


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