New answers tagged

1

First you will need the slug of the current category: $cat = get_query_var('cat'); $currentcat = get_category ($cat); Then you can use the same loop. Just add the following to $query_args to limit the outcome to the current category: 'category_name' => $currentcat->slug,


0

I found the problem here. It was with the $this_page variable, it was referencing the homepage still and kept the about page from working. I had to simply modify how it was pulling the page ID and storing it. Here's my amended code. <?php /* Template Name: Page- Home */ get_header(); ?> <?php $this_page=get_the_ID(); $loop = new WP_Query( ...


0

You Need to hook into the template_include filter e.g. add_filter('template_include', 'my_function_name'); function my_function_name( $template ) { if ("example" == $name){ $template = dirname( __FILE__ ) . '/my-template.php'; } return $template; } I asked this on here a couple of years ago and have used it a few times since for projects :)


0

Well, from my side is better to use WP_query() for this and define all in variables. Reason for that is because you can define sparate loops foreah WP_query. Also you need to clean all prevous query when you get info.


-2

Really the_content filter is most of what you need to do to be like the_content, but not require all those other variables the_content() expects.


2

There are many globals that are being set in a loop, not only $post, and you need to set all of them to get exactly the same behavior. To get this it is not enough to set $post but you also need to use setup_postdata


0

$args = array( 'taxonomy' => 'portfolio_type', 'exclude' => '28,30', ); $tax = get_terms($args); foreach($tax as $t){ echo '<div class="span3 produto-lista">'; $args = array( ...


4

to hook in the <head> you would use wp_head. You can read the documentation here. add_action( 'wp_head', 'meta_example' ); function meta_example() { echo '<meta name="description" content="Meta Description for this Page." />'; } // End meta_example() This assumes that your theme has <?php wp_head(); ?> in the <head>.


0

I cannot think of a way of getting the latest 5 posts plus a specific one in a single query without using SQL. If you don't mind using two queries, I think this would be an easy way to do what you intend: //Get the latest 5 posts (which happens to be get_posts default 'posts_per_page' value) $posts = get_posts(); //Build up an array with the IDs ...


0

One way would be to use the built in counter to tell how many times you've been through the loop, then use get_post on the fixed posts id once you've been through 3 times... <?php $my_query = new WP_Query( 'posts_per_page=5' ); // get the latest 5 posts while ( $my_query->have_posts() ) : $my_query->the_post(); ...


2

1 If you look at the source code of get_the_title you will see that there is no difference between the two ways of writing the same thing, because if you call it without a parameter it is supposed to be the current ID. 2 There is no substantial difference between the two loops you use. All go through the same amount of items and do the same thing with it. ...


0

The WP_Query() custom field (i.e. meta) query can handle arrays for field values. You just need to add the compare key to your array: $args = array( 'numberposts' => -1, 'post_type' => 'post', 'meta_query' => array ( array ( 'key' => 'my_key', 'value' => 'target_value', ...


3

You simply need the is_sticky() conditional check and then act on that. You would probably still need the is_home() and !is_paged() conditional check to only target stikies on the first page of the home page if ( have_posts() ) : while ( have_posts() ) : the_post(); if ( is_sticky() && is_home() ...


1

You'll have to move the part that says $do_not_duplicate = $post->ID; to the conditional statement that leads to the first get_template_part(). Now you have stored the ID of that sticky post. if( is_home() && !$paged && $c == 1) { $do_not_duplicate = $post->ID; get_template_part( 'includes/sticky-post', 'page' ); } Next, ...


2

I was able to figure this out and make it dynamic. I created a variable $template that I put inside the loop, in which I stored the page template. $template = get_post_meta( $post->ID, '_wp_page_template', true ); Then, I utilize this where I need the child pages to show up. <?php include(locate_template($template)); ?> This is working for me ...


1

The question you linked to doesn't quite do what you're wanting - it allows you to get the template assigned to a page you're querying, but what you're actually wanting is to get a page assigned to a particular template. You can do this using the get_pages() function by querying a meta key of the page. Pages/posts can have a range of fields assigned to ...


1

Try this code global $myOffset; $myOffset = 1; $paged = get_query_var('paged') ? get_query_var('paged') : 1; //Use 'page' instead of 'paged' if you are on home page $args = array( 'post_type' => 'post', 'cat'=> 1, //Selecting post category by ID to show 'posts_per_page' => 4, //No. of posts to show 'offset' => $myOffset, ...


3

Whenever the main query is available, which is for every front end page load regardless of which page/archive is loaded, you should use pre_get_posts to alter the main query's query vars before the SQL query is build and executed. This goes for each and every page where you need to alter the main query. This is the RECOMMENDED way to alter the main query. ...


4

query_posts() is useful in cases when there is no main query: calls to wp-admin/admin-ajax.php, wp-admin/admin-post.php or wp-login.php for example. Yes, you can achieve the same results there without query_posts() and slightly less compact code instead. But if you don't have to care about side effects, using query_posts() is acceptable.


0

You need to setup postdata in order to set the $post global in order to make template tags available for this to work foreach ( $myposts as $post ) { setup_postdata( $post ); get_template_part( 'content' ); } wp_reset_postdata();


2

Now you can simply use the get_the_excerpt( $postID ) function. Since: WordPress 4.5.0 introduced the $post parameter.


3

In general, the query performed on the homepage, query_posts( 'posts_per_page=get_option( 'posts_per_page' ) and $q = new WP_Query( 'posts_per_page=get_option( 'posts_per_page' ) should have the same exact performance with very very little to no difference between them as all of the above are exactly the same by default (that is, have the same query ...


6

You ask: is query_posts really slower than some secondary query... The fact is that if you're calling query_posts() from a theme then it already is a secondary query. WordPress has already queried the database once to get a certain page then it hits your query_posts() function and queries the database again creating a second query and overwriting the ...


-1

Can you please try below code? $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $woo_home_query = array( 'post_type' => 'product', 'posts_per_page' => $top_selling_products_nr, 'meta_key' => 'total_sales', 'paged' => $paged, 'orderby' => 'meta_value_num' ...


6

If you just want posts from today, it's super easy and right in the WP codex: <?php $today = getdate(); $args = array( 'date_query' => array( array( 'year' => $today['year'], 'month' => $today['mon'], 'day' => $today['mday'], ), ), ); $custom_query = new WP_Query( $args ); ?> ...


0

i solved my own problem by giving up and using wordpress's fetch_feed function https://codex.wordpress.org/Function_Reference/fetch_feed this works well for me because i dont have to worry about timing when calling parameters.


0

You can get users by user meta, using the WP_User_Query class. There's a great explanation at https://tommcfarlin.com/get-user-by-meta-data/


1

The wp_get_attachment_image_src() function expects you to also pass some kind of attachment ID plus it doesn't grab the image HTML that we need so instead we should use wp_get_attachment_image(). IF a post has a post thumbnail, grab it.ELSE IF the post has any attached images, grab the first one.ELSE maybe show a placeholder? I've defined the else case at ...


1

In your function.php you can add a custom size, for example: add_image_size ('custom_thumbail', 200, 200); Then once you regenerate thumbnails (recommended plugin by wordpress) it will create these, or any new uploaded images will have this size. Then you can call them in your post. Echo wp_get_attachment_url('your post id', 'custom_thumbail'); ...


1

I use the Aqua Resizer in my theme development. https://github.com/syamilmj/Aqua-Resizer It's pretty easy to implement, and it should do exactly what you want. This function will allow you to resize any existing WordPress image. The below example would create a 200 x 200 image from the WP Medium image, and hard crop it to 200 x 200. $thumb = ...


2

set_post_thumbnail_size() (and other API functions which add/change sizes) applies to generation while it's active. So existing generated image sizes won't be retroactively affected by it. There are plenty of tools around (plugins, wp-cli) which regenerate files with current sizes configuration.


2

Try this in your functions.php file: add_theme_support( 'post-thumbnails' ); set_post_thumbnail_size( 200, 200 ); Read More: http://codex.wordpress.org/Function_Reference/set_post_thumbnail_size


4

That's because the main query is being discarded and replaced with your custom query. You've not told your custom query to look for that tag, so why would it? You might also notice your pagination is broken for the same reason, you've not told the new query which page you're on, so why would it pull the right page? What's more, that main query is expensive! ...


1

You can use current_user_can() and is_user_logged_in() to validate current user: <?php if ( is_user_logged_in() && current_user_can('role') ) : ?> <span style="font-size:16px"> <strong>Currently reading:</strong> <?php the_field('book_name'); ?> (<?php the_field('book_year'); ?>)<br/> ...


1

If you want the WordPress framework without the querying, use wp-load.php: require '/path/to/wordpress/wp-load.php';


0

I am not sure this will be suitable to your case but you can try: define('SHORTINIT',true); before including wp-blog-header.php and many things will not load - perhaps the redirect will be one of them? (But, it does mean you may have to include a lot of wp-includes files manually too for many of the WordPress functions to be able to run without fatal ...


0

Here's the fix. All the attachment info is now being called inside the php using wp_prepare_attachment_for_js and the info will load for each image in the loop, to display in the photo gallery. It works now. <?php $the_query = new WP_Query(array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'category_name' => 'arch' ...


3

Loosely what you have should work already. However few things are off. Calling these function without time format will produce values like 1:36 pm (depending on your site's settings), which are not exactly comparable. Post modified time can be less than published in some cases, like scheduled posts. So I would write it along the lines of: if ( ...


0

The best way to do this is get the post format and then use the switch statement, like this: $my_format = get_post_format (); switch ($my_format) { case 'quote': $htmlcontent = ... ; break; case 'aside': $htmlcontent = ... ; break; and so on } The function that builds up $htmlcontent starts with declaring some building blocks that are used ...


0

Looking at your list, you are really only changing the default result for excerpts for the image and quote formats (at the moment), you can remove those using the_excerpt filter... add_filter('the_excerpt','custom_post_format_excerpt',99); function custom_post_format_excerpt() { $noexcerptformats = array('quote','image'); if ...


0

You don't have to merge the arrays to use them together in a loop. You just need a similar key that you can use as a hash lookup. That's where wp_list_pluck comes in handy. // Image Data $instagrams = array ( array ( 'post_id' => 1, 'image' => 'http://placekitten.com/200/300', ), array ( 'post_id' => 2, ...


1

Ok, thanks to The Maniac for all the help troublshooting. Looks like I had to take this completely outside the loop and call the WP_Query class on a new variable to make it happen. Here's what worked: <?php $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) ); while ( $query->have_posts() ) : $query->the_post(); ...


0

I think you just need to add a call to setup_postdata. Otherwise your code looks like it should work as intended: <?php foreach($posts as $post): setup_postdata($post); ?> <a href="<?php the_permalink(); ?>" title="Random Post from Our Blog" style="float:right;" class="random-widget"><span class="fa-random" ...


2

Technically you can merge things into loop, by manipulating its posts field (such as $wp_query->posts for main query). WP has it in public for visibility and access, as it tends to. However it's not that common technique, less so if you are considering injecting something that is not actually posts. It is more common for such output to check and output ...


0

Currently from your code I can see you are looping for post IDs from 1 to 1000 that isn't good. WordPress have lot of functions by which you can fetch post IDs, content and other information. One of them (according to your need) is get_pages() Check the documentation of get_pages() there are lot of examples. See the Example from above code:- $all_pages = ...


2

For reference, before: 45q, after: 42q The code is very similar to the code used by @birgire function _tomjn_home_cancel_query( $query, \WP_Query $q ) { if ( !$q->is_admin() && !$q->is_feed() && $q->is_home() && $q->is_main_query() ) { $query = false; $q->set( 'fields', 'ids' ); } return ...


4

Skimming through the WP_Query we find this part of interest: if ( !$q['suppress_filters'] ) { /** * Filter the completed SQL query before sending. * * @since 2.0.0 * * @param array $request The complete SQL query. * @param WP_Query &$this The WP_Query instance (passed by reference). */ $this->request ...


2

Here is a neat trick I learned from @birgire, we can halt the main query by appending AND where 0=1 to the WHERE clause of the SQL query. This might still result in one db query, but it will surely stop the main query from querying posts add_filter( 'posts_where', function ( $where, \WP_Query $q ) { if ( $q->is_home() && ...


0

Have you tried wp_reset_query(); after each loop? Please provide complete code for your template then we may can help.


2

First, go to Settings->Reading->Blog pages show at most->25. Or change the posts_per_page in main query to 25. Then, try this code in your template: if (have_posts()) : $count = 0; $paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1; while (have_posts()) : the_post(); $count++; if ($count <= 3 && ...



Top 50 recent answers are included