I need to have a index page and it can pull out the title (with link), meta key description and meta key image (meta keys from custom fields) of its child pages, but only one level down.
e.g. I have a index page call WORK, and few child index PRINT, WEB. there will be some pages under PRINT and WEB as well, but I don't want them to be shown on the WORK index. I only need to have PRINT and WEB to be listed and with the meta keys.
Anybody can help please? many thanks!
Try putting:
query_posts("cat=1,-2,-3");
...Before the loop. Replace the numbers after cat= with the category IDs in question; using the negative symbol (such as "-2,-3") excludes a category.
To do this more programmatically:
$parent = 1; //catID of parent
$children = get_categories("child_of=".$parent);
foreach ($children as $child) {$excluded .= "-".$child->term_id.","; //add minus sign}
$excluded = rtrim($excluded, ","); //remove last comma
query_posts("cat=".$parent.",".$excluded);
Related
I’ve developed a new theme for a website, but i seem to have a problem related to widget Filter By Attribute.
When i use the filter, that comes with the gutenberg block editor for widgets, the filter attribute widget doesn’t filter anything. (i guess it uses ajax to filter, but its not filtering anything)
When i use the add_filter('use_widgets_block_editor', '__return_false'); to remove the gutenberg and use a new filter by attribute (which is a different widget, than the one that comes with Gutenberg), the widget works, because it filters the query by passing some params on the url, but has a problem. It shows variations that are out of stock, which is something that have been fixed with the new widget, using the product lookup tables, and it's exactly what i'm trying to achieve here.
I strongly believe this is related to the AJAX call it uses to filter the shop page, so i guess i'm missing an ID or Class, to the product wrapper or something.
Does anyone also had this problem?
If you want to see the page in question, is this one https://www.hiima-store.com/bch/shop/
When you go into Filters, you can see both widgets there. (there are two for sizes. the first one uses query params and the second one, is the one that is not filtering).
I did a custom script regarding that.
It might help someone with the same problem.
It will hide the product if the variation is out of stock.
add_action('woocommerce_before_shop_loop_item_title', 'out_of_stock_variations_loop');
function out_of_stock_variations_loop()
{
global $product;
if (isset($_GET["filter_size"])) { // check if the attribute is in the url
if ($product->product_type == 'variable') {
$available = $product->get_available_variations();
if ($available) foreach ($available as $instockvar) {
if (isset($instockvar['attributes']['attribute_pa_' . __('size', 'hiima')])) {
var_dump('entrei');
if (($instockvar['attributes']['attribute_pa_.' . __('size', 'hiima')] == $_GET['filter_size']) && (!$instockvar['max_qty'] > 0)) {
global $product;
$id = $product->get_id();
echo "<style>.post-$id{display: none}</style>";
}
}
}
}
if (!$product->is_in_stock()) {
global $product;
$id = $product->get_id();
echo "<style>.post-$id{display: none}</style>";
}
}
}
I’m new to Advanced Custom Fields and a novice in WP, I'm interested in creating a random slider in my homepage where each post has many images.
I’m not 100% sure how to combine the wp_query with the ACF repeater, where multiple posts are involved, I did succeed in doing this in a single post page.
I am less interested in specs on how to do this, nor PHP functions, i’m well versed in both, the issue is the WP functions and conventions
If someone already done something like this and can advise how to begin this with combining the ACF repeater functions together with the wp_query, from there i'd know how to shuffle the images of each post with array_rand.
if a Gist/fiddle exists, would be even better.
You could use the shuffle PHP function to randomise the array the repeater field outputs then slice out the amount of slides that you want. Something like this:
$rows = get_field('repeater_field_name'); // Get row array
shuffle($rows); // Shuffle the array in a random order
$rows = array_slice($rows, 0, 5); // Slice out the first 5 elements of the array
if($rows)
{
echo '<ul>';
foreach($rows as $row)
{
echo '<li>sub_field_1 = ' . $row['sub_field_1'] . ', sub_field_2 = ' . $row['sub_field_2'] .', etc</li>';
}
echo '</ul>';
}
If you're just using the repeaters for images, I would suggest using the "Gallery" element rather than the repeater, and doing the same thing.
I've seen this on other themes but can't work out how it has been done. In my grouped product list, the title lists as Parent product title --> Child product title. I need it only to list the Child product title.
I can see the code to alter is:
' . $child_product['product']->post->post_title . '
Either the post_title needs to overridden, or the code altered to...what?
(Although this is old, it's still a common question with a prominent google ranking)
Woocommerce defines a filter, woocommerce_product_title, which allows you to pass a product's title through a function that modifies the way it will display.
Add a filter, probably in your theme's functions.php
add_filter('woocommerce_product_title', 'clean_up_title');
This is the function I'm currently using to accomplish this, no promises that it's the best way possible:
function clean_up_title($title){
// check to see if there is an html arrow in the title
if (strpos($title, '→')){
$separator = '→';
}
// otherwise assume it's the character
else {
$separator = '→';
}
// split the title into multiple parts at the arrows
$prog_array = explode($separator, $title);
// get the last part, the actual product name
$prog_name = end($prog_array);
// slice off any leading or trailing whitespace
return trim($prog_name);
}
This would be a bit cleaner to do. Just 'return' the product title rather than 'edit' it.
function clean_product_title($title, $product) {
return $product->post->post_title;;
}
add_filter('woocommerce_product_title', 'clean_product_title', 10, 2);
basically I have a three-level menu in wordpress and I've got the following code in the front-end to call the third-level menu:
$children = get_pages('child_of='.$include_page_ids[$i]);
if (count($children) > 1) {
$sub = "<ul>";
foreach ($children as $child){
$sub .= "<li><a href='#$child->post_title'>";
$sub .= $child->post_title;
$sub .= "</a></li>";
}
$sub .="</ul>";
echo $sub;
}
This calls a list for the children of a certain page and also makes the the anchors (which I also need). The problem is that right now they are being displayed in an alphabetical order, but I need to be able to set the right order myself (ie to be the same as in the backend menu). Please hepl me with it, how can I achieve this? For example this is the page http://www.eboxlab.net/transbeam/support/support/, youcan see the third level-menu as the box right next to the banner (Acceptable Use Policy to Terms & Conditions). THe order of the blocks which it corresponds to is right, but the menu is alphabetically ordered.
Help really appreciated.
PS: if you need I can provide the template code
It's pretty simple actually. Here is what your $children call should look like:
$children = get_pages('child_of=' . $include_page_ids[$i] . '&orderby=menu_order&sort_order =ASC');
That's all you need to add - this tells the query to order pages in ascending fashion by their "menu_order" column(or the "Order" field under Page attributes). You can see more details on the get_pages function at Function Reference/get pages
wp_nav_menu to the rescue! Assuming you're using WP 3 or newer, it will let you wrap elements in whatever markup you'd like, and will correspond to however you've configured the menu in the admin Dashboard.
How can I specify in my posts query that I only want to have posts without any tags at all? Pretty simple question, don't think I need to elaborate much more than this.
So to get posts WITH a tag I would do query_posts('tag=this').
Use tag__not_in(/* array of tag id values */).
If you wanted to exclude all tags, you would need to build up an array using the full list of tags retrieved using get_tags(). That method will return an array of tag objects, where the term_id property is the id for that tag. Example:
$tags = get_tags();
for ($i=0; $i < count($tags); $i++)
{
$tag_id_array[$i] = $tags[$i]->term_id;
}
For more info on query parameters, see here.