Trying to get property of non-object when using $post->post_parent

@kaiser answer gives you the solution. I see from your comment that you don’t understand, so I upvoted his answer and translate it in code: <?php $bannerimg = ‘home.jpg’; $post = is_singular() ? get_queried_object() : false; if ( ! empty($post) && is_a($post, ‘WP_Post’) ) { if ( ‘about’ == $post->post_name || ‘2’ == $post->post_parent ) … Read more

Get post id by attachment id?

get_post_ancestors can give you the ID of the object an attachment is associated with: $attachment_id = 42; $parent = get_post_ancestors( $attachment_id ); echo $parent[0]; // $parent will be an array

Create an “All Posts” or “Archives” Page with WordPress 3.0?

Create a new template file and do this as the loop: query_posts( array( ‘posts_per_page’ => -1, ‘post_status’ => ‘publish’ ) ); if( have_posts() ): echo ‘<ul>’; while( have_posts() ): the_post(); echo ‘<li><a href=”‘; the_permalink(); echo ‘”>’; the_title(); echo ‘</a></li>’; endwhile; echo ‘</ul>’; endif; wp_reset_query(); Then just use that template for a page and it’ll automatically … Read more

Adding a div class or id inside the_content()

You have to put your advertisement block just before <?php the_content(); ?> in a separate div-layer and add some css to it. E.g. single.php <div class=”entry entry-content”> <div class=”advertisement”> <p>Your advertisement</p> </div> <?php the_content(); ?> </div> CSS div.advertisement { float: left; width: 150px; padding: 0px 10px 10px 0px; }

Display all search results

The quick and dirty way to do it would be to use query_posts again, doubling the number of database calls. <?php if (have_posts()) : ?> <?php query_posts(‘showposts=999’); ?> Better would be to add this to functions.php, altering the original query before it is executed: function change_wp_search_size($query) { if ( $query->is_search ) // Make sure it … Read more

Custom SQL query to get List of posts with featured image url

Worked on similar problem recently. Here is the SQL query to get the post with Featured Image. global $wpdb; $perpage = 10; $page = 1; // Get the current page FROM $wp_query $counter = $perpage * $page; $uploadDir = wp_upload_dir(); $uploadDir = $uploadDir[‘baseurl’]; $sql = ” SELECT post.ID, post.post_title, post.post_date, post.category_name, post.category_slug, post.category_id, CONCAT( ‘”.$uploadDir.”‘,”https://wordpress.stackexchange.com/”, … Read more

Pull Two Posts Into Custom Post Type `single-cpt.php`

Try this: <?php $current_id = get_the_ID(); $next_post = get_next_post(); $next_id = $next_post->ID; $cpt = get_post_type(); $cpt_array = array($current_id, $next_id); $args = array( ‘post_type’ => $cpt, ‘post__in’ => $cpt_array, ‘order_by’ => ‘post_date’, ‘order’ => ‘ASC’, ); $the_query = new WP_Query($args); if($the_query->have_posts()): while($the_query->have_posts() ): $the_query->the_post(); echo ‘<h2>’.the_title().'</h2>’; endwhile; endif; wp_reset_postdata(); ?> Tested locally and seems to work … Read more

How to display posts by current user/author in a custom page template?

this should work for you: if ( is_user_logged_in() ): global $current_user; wp_get_current_user(); $author_query = array(‘posts_per_page’ => ‘-1′,’author’ => $current_user->ID); $author_posts = new WP_Query($author_query); while($author_posts->have_posts()) : $author_posts->the_post(); ?> <a href=”https://wordpress.stackexchange.com/questions/23829/<?php the_permalink(); ?>” title=”<?php the_title_attribute(); ?>”><?php the_title(); ?></a> <?php endwhile; else : echo “not logged in”; endif;

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)