I am attempting to make a custom query in Wordpress. I want to have it pull up any posts that are tagged relevant to the page that people are on. IE - If they are on the Bay 198 Skatepark page, they also see a list of all Bay 198 Skatepark posts.
I've gotten the custom query to work, but I need this to be dynamic so that if you are on the Bay 198 page, you see those posts, but if you are on the Precision Skateboards page, you see those specific posts.
The easiest way I could think to accomplish this was to create a custom field on each page that would be the same as the tag and that way I would just fill in the tag for each page as a custom field and it would return the posts.
The issue is that I can't figure out how to implement the custom key call into the custom query. Here's my code so far, this one being the one that works:
<?php $second_query = new WP_Query(' tag=bay-198&posts_per_page=500 '); while( $second_query->have_posts() ) : $second_query->the_post(); ?>
I just need to figure out how to replace that "tag=bay-198" and have it dynamically pull in the tag based on the page.
Hopefully that makes sense, I appreciate any help.
You can use a variable that containts the value of your custom field. Then add that variable into the query like so:
<?php
$mykey_values = get_post_custom_values('my_key');
$tag = $mykey_values['your-custom-field'];
$second_query = new WP_Query(' tag=' . $tag . '&posts_per_page=500 '); while( $second_query->have_posts() ) : $second_query->the_post(); ?>
Related
I have a WordPress theme with 100s of different template parts - ideally I'd like to be able to specify which template part to load based upon the value of a post's custom field. i.e. if post's field value is ABC then get_template_part ABC-template.php. I'm using Advanced Custom Fields to specify the fields.
To get the field value you normally use:
<?php the_field('field_name'); ?>
I've tried something along the lines of
<?php $var = get_post_meta($post->ID, 'field_name', true);
if ($var == '')
{ }
else { get_template_part( "subdirectory/' . $var . '", "form" );} ?>
but this didn't work. Any ideas on retrieving the custom field value and then loading the relevant template part using get_template_part?
I'm aware I could in theory achieve this using the in_category function - but thought that having to cycle through 100's of "if post in_category ABC load ABC template, if post in_category DEF load DEF template" each time a post loads might slow the website down a bit?
Thank you so much in advance, really appreciate any help you can offer :)
Edit: I've since worked it out (trial and error, I'm rubbish at PHP!)
You need the following:
<?php
$fieldname = get_field('fieldname');
get_template_part('subdirectory/' . $fieldname ); ?>
If you have ABC-DEF stored in the custom field, this will then load the template in subdirectory/ABC-DEF.php
Hope this helps someone :)
G'day folks, I'm attempting to integrate Ajax Availability Calendar into a WordPress site via PHP.
I have multiple calendars set up, and need to display a different one on each property page.
The problem I'm having is that I need to be able to specify the ID e.g. $_GET["id_item"]=2 of the calendar before the <?php require_once 'pathtofile' ?> statement which is in the template.
The result I need to achieve is...
<?php $_GET["id_item"]=2; require_once 'path\to\required.file'; ?>
If I put this code in the template with the appropriate ID, it works, but I need the ID number to change on each page, otherwise I'll always be displaying the calendar with ID number 2.
I currently have custom shortcode (below) with which I can specify the ID in the page content, but I am at a loss as to how to access that ID before the statement in the template.
So what I currently have is...
In Functions.php
function ajaxcalendar_shortcode($atts) {
$args = shortcode_atts(
array(
'id_item' => 1
),
$atts
);
return '<?php $_GET["id_item"]='.$id_item.'; ?>';
}
add_shortcode('cal_display', 'ajaxcalendar_shortcode');
with the shortcode [cal_display id_item = "2"] in the content.
Is it even possible to do this or should I try another approach?
My research suggests that I may be able to use do_shortcode() to achieve this, but I haven't figured out how to use it in this situation, or I may be misinterpreting it's purpose.
I've tried putting this in the page template file:
<?php $content = the_content(); echo do_shortcode($the_content); ?>
<?php require_once 'C:\xampp\htdocs\MooreRiver\availability\ac-includes\cal.inc.php'; ?>
But it continues to display the calendar with ID 1.
Any tips would be appreciated :)
I have a page named "Shop". On that page I display a custom menu, which contains links to categories like "Books", "Shoes" etc. Whenever I click one of those links/categories it takes me to a relevant category page, for example /category/books/.
I do not want it to redirect me to a category page, I want it to display posts on the same page ("Shop") that the menu item was clicked on. The only problem I have encountered trying to accomplish this, is the fact that I do not know how to change the custom menu behavior. I don't want it to redirect me, but instead send a GET value to the same page ("Shop"). Then the shop page would take that GET value and display relevant posts.
I know how to display posts from a category etc. I just don't know how to change the behavior of the custom menu.
Could anybody help me? I would be grateful.
Original Answer:
Use wp_get_nav_menu_items:
$menu_slug = 'YOUR_MENU_SLUG';
$menu_id = get_nav_menu_locations()[$menu_slug];
$menu_items = wp_get_nav_menu_items($menu_id);
foreach($menu_items as $item){
if($item->object == 'category'){
print('<p>Title: ' . $item->title . '<br>ID: ' . $item->object_id . '</p>');
}
}
wp_get_nav_menu_items returns an array of menu items. You get several information on the item, like type (post, category, ...), id, title and so on.
See http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items for a full list of properties.
Hope this helps :)
Update:
Assuming your permalink setting is set to default, the following code will print a modified menu that uses GET.
$menu = wp_nav_menu(array(
'theme_location'=>'YOUR_MENU_LOCATION',
'echo'=>0,
));
$new_url = $_SERVER['SCRIPT_NAME'] . '?shop_page=$1';
$menu = preg_replace('/href="[^"]*cat=(.+)"/', 'href="'.$new_url.'"', $menu);
print($menu);
The regex may not be the best as it ignores other GET values and I'm not experienced on them. If your permalink setting is different and every time you change it, you will have to edit the regex.
My current setup includes having the category of each post. But whenever I mouse over the category, it shows the default message of Show all posts in [category].
I have looked around and have seen some pretty similar questions that require multiple lines of php code. This surely cannot be that complicated. Is there a way I can perhaps tweak the following to get it to do what I want?
<?php the_category(' '); ?>
This is the code that returns the above. I am looking through the codex and found category_description() and get_the_category(). I am kind of new to arrays and am having problems figuring them out. I was kind of hoping that something like this would work:
<?php the_category('title=category_description()'); ?>
but it is not. I know that is a pretty hilarious way of solving this to most of you but I am completely lost. I just want the description of the category to be the title attribute. Is there a simple way that I can do this?
wordpress codex
using get_the_category(); you can save all of the categories into a variable.
Ex:
$categories = get_the_categories();
There is however an optional parameter that you can pass; the category ID.
Ex:
$categories = get_the_categories(THE_CATEGORY_ID);
Whether you present a category ID or not you must run $categories through a loop.
Ex:
foreach($categories as $category){
}
Only then, will you be able to customize your title attribute without modifying the wordpress code.
Ex:
foreach($categories as $category){
echo ''.$category->cat_name.'';
}
In wordpress (ideally, with out the use of a plug-in) on a tag page I would like to show the number of posts that are tagged with the current tag.
Example:
There are 8 posts with the tag "baseball"
when you are on the tag page for baseball it says, "There are 8 posts about baseball"
It should dynamically know which tag page your on, get the count and print it. I have found several options for statically entering the tag name or ID and returning the count but my attempts to get them to work dynamically have been unsucessful.
This is what I was working with:
$taxonomy = "post_tag"; // can be category, post_tag, or custom taxonomy name
// Using Term Name
$term_name = single_cat_title;
$term = get_term_by('name', $term_name, $taxonomy);
// Fetch the count
echo $term->count;
Any help would be greatly appreciated!
You can just print the found_posts property of the WP Query object
echo $wp_query->found_posts;
Open the file in your template named "tag.php" and enter the desired code in it.
Here is another possible solution from WP Recipes. They were kind enough to post this the day after I got an answer here. :)
<?php
$feat_loop = new WP_Query( 'showposts=12&category_name=featured' );
echo "Query returned ".$feat_loop->post_count." posts.";
?>
It has different application but still very useful!
http://www.wprecipes.com/get-how-many-posts-are-returned-by-a-custom-loop?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Wprecipes+%28WpRecipes.com%3A+Daily+recipes+to+cook+with+WordPress%29