Creating a WordPress Loop for Pages Instead of Posts - php

On my WordPress site, I'm using a plugin called "Vera Meta Boxes" that lets me add custom meta boxes to pages (not referring to custom post types).
So now every page has meta box that says, "Show on Home page" with a checkbox, that when selected has the value of "yes".
Now on my homepage, I want to show the titles and featured image of any page that has "Show on Homepage" checked.
The vera meta box plugin says to use:
<?php get_post_custom_values('your_custom_field_key_here'); ?>
So I would use:
<?php get_post_custom_values('show_on_homepage'); ?>
But how do I do the rest? Conceptually, it would be something like:
LOOP Query Pages > if <?php get_post_custom_values('show_on_homepage'); ?> = yes show title and featured image and repeat loop until all pages with "show_on_homepage" are shown.

Well, I haven't tested this, but according to the docs you should be able to use this query:
array('post_type'=>'page', 'meta_query' => array( array('key' => 'show_on_homepage') ) )
Note that meta_query is an array of arrays
This is 3.1 code, the 3.0 version should look like this:
array('post_type'=>'page', 'meta_key' => 'show_on_homepage')

Related

Custom taxonomy images - output to single product pages

I'm trying to display images attached to custom taxonomies on the individual product pages. I've used the Custom Taxonomy plugin (https://wordpress.org/plugins/taxonomy-images/) and added the images using Advanced Custom Fields. I have 2 custom taxonomies called 'range_logo' and 'features', each with images assigned to them. I need to output the images to the single product page using the hook 'woocommerce_before_add_to_cart_form'.
As you can see below, I have tried outputting a line of text to test if the hook is working correctly, and it is, but the taxonomy image isn't shown when I add the code which is supplied with the Taxonomy Images plugin.
add_action(
"woocommerce_before_add_to_cart_form","product_taxonomy_image");
function product_taxonomy_image () {
echo 'Taxonomy image here';
print apply_filters( 'taxonomy-images-queried-term-image', '' );
}
Expected results are that the appropriate taxonomy image for the product would be displayed. Instead I got this error:
Notice: term_taxonomy_id is not a property of the current queried object. This usually happens when the taxonomy-images-queried-term-image-id filter is used in an unsupported template file. This filter has been designed to work in taxonomy archives which are traditionally served by one of the following template files: category.php, tag.php or taxonomy.php. Learn more about template hierarchy. in /var/sites/a/mysite.com/public_html/wpnew/wp-content/plugins/taxonomy-images/legacy/includes/public-filters.php on line 398
I'm using the Avada theme - maybe the layout of the theme's template files could be the reason for the error? I think I may need to use archive.php instead, but I don't have much experience with PHP.
I've tried a few possible solutions and have looked elsewhere for help, but I've hit a wall with getting this to work and I'm not sure what I should be trying next.
welcome to the community. First off, I would like to mention that you have written a bit confusing detail about the taxonomy images as you have mentioned using the plugin Taxonomy Images as well as Advanced custom fields
If you are using the former plugin, you can do something as shown below:
add_action("woocommerce_before_add_to_cart_form","product_taxonomy_image");
function product_taxonomy_image () {
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="my-custom-class-name">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'taxonomy' => 'product_cat',
) );
}
The above code will give you the images from the category associated with that product. If it is another custom taxonomy that you created, please change the value in taxonomy parameter.
If you are using ACF you will have to get product terms first. Let me know if that's the case and I will share that code with you.

How can I display specific posts in a single page in wordpress?

I want to know if it's possible to display specific posts when I click on a link from a specific page to the (ex: gallery page).
Example: Let's say I have 3 posts with 3 different galleries. (post 1=wedding pictures / post 2=food pictures / post 3=dogs pictures) and I also have 3 pages.
When I'm on Page 1 and click on the gallery page link, I want to only show post 1 content (only wedding pictures). When I'm on Page 2 and click on the gallery page link, I want to display only the post 2 content.
If It's possible what would be the most simple solution?
Thank you!
To show specific posts based on type, category, tag or any other property, your best bet is to use a shortcode, which gives you lots of versatility in how to render the items (in a post, page, widget, template file, etc).
The shortcode could be generated by a plugin such as Bill Erickson's Display Posts, Content Views or 10's of other plugins.
Or do it manually by building your own "show some posts" shortcode and get a much better understanding of WP. You'll have to build your own html output, filters and paging, but you will end up with less plugins gumming up your installation. There's lots of tutorials for this, search for "Wordpress display posts shortcode functions.php".
E.g. Placing the following in your theme's (ideally, a child theme) functions.php file, here is a way to show a specific number of posts from a specific content type:
Place in functions.php:
function show_some_posts($atts) {
$a = shortcode_atts([
'post_type' => 'post',
'posts_per_page' => 3
], $atts);
$the_query = new WP_Query( $a );
if ( $the_query->have_posts() ) {
$string .= '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$string .= '<li>' . get_the_title() . '</li>';
}
wp_reset_postdata();
$string .= '</ul>';
} else {
$string .= 'no posts found';
}
return $string;
}
function shortcodes_init()
{
add_shortcode('get-posts','get_some_posts');
}
add_action('init', 'shortcodes_init');
And to render the actual list, place this shortcode in your page or post:
[get-posts posts_per_page="3"]
You can first assign each photo (or post containing a photo) a category e.g. Wedding, food etc.
Once that is done, create a custom menu and add the categories to it. Place this menu on an area of your page and give it a try. It should only show the required images/posts.
For more info, you can have a look at the link below:
https://en.support.wordpress.com/category-pages/
Note: You may need a plugin to allow you to place categories on images. Have a look at an example at the link below:
https://wordpress.org/plugins/categories-images/

Display custom registered posts in the custom query new WP_Query

I'm building a custom plugin for WordPress to display some custom posts.
So far so good, it worked (I had to re-save the permalinks), and now I'm able to see it in my archive page and, since I added the slug in the array for the register_post_type function, I can see them adding the slug in the url (for example: mywebsite.com/coolposts:
'rewrite' => array( 'slug' => 'coolposts' ),
So far so good but I have a problem and I don't know how to solve it.
I want to display my custom posts "coolposts" in my custom query:
$the_query = new WP_Query('orderby=title&order=asc&posts_per_page=50');
while ($the_query->have_posts()) :
$the_query->the_post();
if ((($the_query->current_post+1) % 3 == 0) && ($the_query->current_post+1 !== count($the_query->posts))):
echo "</div><div class='row'>";
endif;
endwhile;
Because, for some reason, my custom posts are excluded from the main query.
Am I doing something wrong?
Just to be clear, I don't want to display ONLY my customs posts, I want to display all of my posts.
I assume you registered custom post type with the name coolposts when registered with register_post_type function. If so then just add another param post_type=coolposts. To avoid unexpected situation you can add one more param post_status=publish, this param will ensure only published posts are displayed.
$the_query = new WP_Query('orderby=title&order=asc&posts_per_page=50&post_type=coolposts');

Create custom menu in php based on pagenames

im trying to make a custom hardcoded menu in a wordpress page-template. The site has 3 pages (1, 2, 3) and at page 1 i wish to loop trough all pages that has page1 as pagetemplate and 2 for 2 etc.
How can i loop trough and get the names of all pages so i can put them in a menu?
if for example you visit page 1 and the pages that has page1 as template (parent) are "visit us" and "read more" the menu would look like this:
-visit us
-read more
but if i go to page 2 the menu might look like this:
-funpark
-foobar
Now if I add a new page with "1" as parent and name the page "about" the new menu will now show
-visit us
-read more
-about
Cheers,
Emil
You can query for pages and their template value. The codex says:
The filename of a Page's assigned custom template is stored as the value of a Custom Field named '_wp_page_template' (in the wp_postmeta database table). (Custom fields starting with an underscore do not display in the Edit screen's Custom Fields module.)
This means you can build a page query like this:
$args = array(
'post_type' => 'page',
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => '[Your template goes here]'
)
)
);
All you have to do is loop through the pages, get the permalink and build your menu.

wordpress multiple pages on one page

i am developing a CMS site in wordpress ...
the final web page should some what look like :
Website
I want just one page (front page) and on that i want to display subpages titles in it. (About,Home,People)...
So when the subpage title is clicked it should expand and display the the page content in the panel,on same page and at same place.
any ideas regarding same?
Thanks in advance...:)
EDIT1
[accordions]
[accordion title="Accordion 1"]
Accordion 1 Content here
[/accordion]
[accordion title="Accordion 2"]
Accordion 2 Content here
[/accordion]
[accordion title="Accordion 3" last="last"]
Accordion 3 Content here
[/accordion]
[/accordions]
after puting the above code in the page content looks like this:
but on click of ACCORDION 1 its not getting expanded..
You can write your own template, which does a query (checks for a subpage), and adds its content between div tags.
When it's done, then it's time for JS/jQuery . Search for tab jQuery scripts, so you get some information.
Anyway, you will mostly need to use hide() / show() functions to reach something like that.
Also, did you made that template, and are you familiar with programming in WP?
<?php
$args = array(
'numberposts' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'post_status' => 'publish',
'orderby' => 'menu_order,title',
'order' => 'ASC'
);
$my_pagelist = get_children($args);
?>
Above is code for displaying subpage content, below is the jQuery tutorial for tabs.
http://jqueryui.com/demos/tabs/
EDIT:
Also, check this site - http://www.learningjquery.com/2006/09/slicker-show-and-hide it will show you some info about show/hide functions. Also more information about WP get_children function is found here - http://codex.wordpress.org/Function_Reference/get_children
If I understand your question correctly, in the latest version of WordPress.
Login to your admin panel.
Click on the 'Settings' tab, then the 'Reading' sub-tab.
Change the 'Front page displays' option to 'A static page'.

Categories