48

Came here as this post is among the top hits on the search engine when looking for WordPress image alt and title. Being rather surprised that none of the answers seem to provide a simple solution matching the question's title I'll drop what I came up with in the end hoping it helps future readers. // An attachment/image ID is all that's needed to retrieve ...


28

Your problem is that you are not providing the correct attachment's ID to get_post_meta() and get_the_title() functions. This is your code to get the alt of the image: $image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true); And it is correct, but $attachment->ID is not defined in your code, so, the function does not return ...


21

My preferred 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['pagenow'] === ...


15

<?php if( is_front_page() ) : ?> is_home relates to the posts page according to your reading settings. is_front_page always returns true on the front page.


14

To be honest, the function in_the_loop() is what you are looking for: add_filter( 'the_content', 'custom_content' ); function custom_content( $content ) { if ( in_the_loop() ) { // My custom content that I add to the_content() } return $content; } What in_the_loop does is to check if global for $wp_query (that is the main query ...


12

you can use this for <?php global $post; if( $post->ID == 346) { ?> <!-- do your stuff here --> <?php } ?> you can use this anywhere either in header or anywhere else.


12

I actually ran into the same problem myself and my solution was this code: if(basename(get_page_template()) === 'page.php'){ } basename(get_page_template()) gets the filename of the page template (according to https://codex.wordpress.org/Function_Reference/get_page_template) and we then check if it is equal to 'page.php'


9

You have a basic mistake in your code. The following code if ( !is_page('my-page') ) { means that if you are not on that page, deregister the scripts. Have a look at the php operators ! -> Not !$x True if $x is not true Also, why not exclude this specific page when you initially enqueue these scripts, something like add_action( 'wp_enqueue_scripts', ...


8

If you want a wild card check for all your custom post types: if( ! is_singular( array('page', 'attachment', 'post') ) ){ // echo 'Imma custom post type!'; } This way you don't need to know the name of your custom post. Also the code still work even if you change the name of your custom post later.


8

Do not create functions on the fly. This is hard to read and to debug. Implement separation of concerns instead, and separate the registration of callbacks from their execution (business logic). Setting the conditional checks logically in front of the callback registration is dead simple now. Wait for the action template_redirect to instantiate that ...


8

WordPress coding standards for PHP does not state anything about this, and there is no other standard, so it's up to developers to choose one way or another. I have to say that the 2 methods have different approaches; while the first contains conditional logic, the second is a conditional function declaration, which means that if you try to call the ...


8

if ( !has_term('term', 'taxonomy', $post->ID )) { Use the NOT (!) operator


7

Did you check what part of is_front_page() is causing it to return false? I could reproduce the problem by following the setup from the trac ticket. In my case inside this function the call to is_page() was returning false. I guess this is due to using $wp_query->set() for page_id and is_page is only causing the query_vars to be changed, you don't ...


7

A simpler solution will be to pass the title or the slug as argument in is_page(). You won't have issues if you duplicate that page on another server. <?php if (is_page( 'Page Title' ) ): # Do your stuff endif; ?>


7

You have two useful functions: is_page() and is_page_template(). The is_page() function will return true when you're on a page, and the is_page_template() function will return false if the current post (can be a page) is not using a custom template. So, you just need to use both of these functions together: if ( is_page() && !is_page_template() ) {...


7

This is merely an addition to @Otto's answer. Just to make it a little bit better understandable. Basically what @Otto is saying, you have to reverse the logic, that means: if you can reliably determine the main query, then you can add - and remove - your hooking into the the_content filter. For example the main query can reliably be recognized at the ...


7

Just a few notes on your code You should dequeue and deregister a script to remove it completely from the $wp_scripts global You should not be using wp_print_scripts, this is the wrong hook. You should be using wp_enqueue_scripts Don't wrap your action in a conditional. Your conditional tag might either be set to early or to late and might cause unexpected ...


6

An AJAX request is a new request to the server, and it is a request to admin-ajax.php. That is not a single post page. Any logic that depends on is_single() or any other page level template tags like it, won't work. If your AJAX callback needs that sort of information you must pass it to that callback in your AJAX request-- something like: function ...


6

There is no conditional tag for no results on a search page, but you can create yourself one. You basically just have to check the value of $wp_query->found_posts, if it is 0, returns false, any other value, returns true function is_search_has_results() { return 0 != $GLOBALS['wp_query']->found_posts; }


6

I use a quick function in all my themes to get image attachment data: //get attachment meta if ( !function_exists('wp_get_attachment') ) { function wp_get_attachment( $attachment_id ) { $attachment = get_post( $attachment_id ); return array( 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),...


6

Use get_the_tag_list() instead of the_tags(), since the_tags() prints the results while get_the_tag_list() returns it. See documentation: https://developer.wordpress.org/reference/functions/get_the_tag_list/


5

More modern way to do that, it should work even when the wp-login URL is changed by plugins and when WP is in a subfolder, etc: if(stripos($_SERVER["SCRIPT_NAME"], strrchr(wp_login_url(), '/')) !== false){ /* ... */ }


5

Hooks such as init will not work at all. You have to hook at least on parse_query. Everything bellow will work: is_page(198); # ID (int) is_page('198'); # ID (string) is_page('Some Title'); # Title, case-sensitive is_page('some-title'); # Slug But it must be hooked at least in parse_query or any other hook after it. You can see WordPress hook order here: ...


5

You're calling it on the wrong hook, and you're calling the wrong conditional function to test. When the after_setup_theme hook is fired, WordPress hasn't finished processing wether you're on a page or a homepage yet. You should also never pass in hardcoded post IDs. In this case is_home works, as might is_front_page. If you really must reference a ...


5

You're using is_main_query() incorrectly. The global is_main_query() function returns true unless the global $wp_query variable has been redefined. There is probably no 100% reliable way to tell, from inside a the_content filter, whether or not the current Loop you're in is the main query or not. The content filter just filters content. It doesn't have any ...


5

Look at has_tag(). It seems that is what you are looking for You can use it like follow if( has_tag( 'NAME OF TAG' ) ) { // do something if has tag }else{ // do something else if not have tag }


5

Regarding the posts_orderby, posts_where, posts_join and posts_clauses hooks, the current \WP_Query object is available through the second input argument. These are the relevant parts from the \WP_Query class: $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) ); $where = apply_filters_ref_array( 'posts_where', array( $...


5

get_header has an action hook called, (surprise!) get_header which gets a single parameter - the name of the header file. So what we do is check that name and if it matches our name we add a style. So something like this: add_action( 'get_header', 'wpse_260353_add_css_for_header' ); function wpse_260353_add_css_for_header( $name ){ if( 'modern' === $...


5

The code is tested and working fine. Place this code <?php $posttags = get_the_tags(); if( ! empty( $posttags ) ) : $tags = array(); foreach($posttags as $key => $value){ $tags[] = $value->name; } ?> <meta name="keywords" content="<?php echo implode( ',', $tags ); ?>,test"/> <?...


5

You just need an else block: //templating syntax if($condOne): //do stuff if $condOne is true elseif($condTwo): //do stuff if $condOne is false and $condTwo is true else: //do stuff if both $condOne and $condTwo are false endif; //braces syntax if($condOne){ //do stuff if $condOne is true }elseif($condTwo){ //do stuff if $condOne is ...


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