Wordpress check if you're on the current page (not menu) - php

I am creating a little shortcode that acts like the custom menu widget, but I'm choosing the pages from a dropdown list, instead of creating a menus inside the wordpress (I'm also adding a color to it, so I can't just use regular wordpress menus widget).
So far so good, it's just one thing that's bothering me. I want to check if the page I'm on matches the one in my list (on the real page). I googled and searched a bit and found that with
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
I can get the current page I'm on (the link I see in my browser). Perfect!
So if I want to add a class called current page, all I need to see if my $actual_link matches with my selected link from the dropdown list.
I'm working this within a page bulder plugin The Creator, so I know how to create a working shortcode in it. My pages dropdown is created by making an array that will have page url as key and page name as a value. My loop is:
$args = array (
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$pages = get_posts($args);
$forms = array();
if(is_array($pages)){
foreach ($pages as $page) {
$forms[$page->url] = $page->post_title;
}
}
And it works perfectly. Except that the url I get from this is of the form:
http://www.example.com/?page_id=150
Whereas I set my permalinks to be nice so the actual link in my browser is
http://www.example.com/my_page_name
The id is correct, and if I click on my 'menu' link I'll get to that page (desired result). But now I cannot just go and say:
$current = ($actual_link == $link) ? 'current_page' : '';
where $link is the variable that holds the link to the page from the dropdown, so that I can append this to my list to check if I'm on the current page (adds a current_page class). I need this class for the styling purposes - if I'm on the page that has this menu shortcode, next to the link that matches this page I'll get chevron (>).
So my question is, how to get the matching urls, no matter what permalink setting I use? Is there a way to specify this in the get_posts() query, or with the $_SERVER[] variable?
I am trying to avoid javascript with this one, and do everything server side.

You need to place get_the_ID() inside get_permalink() it will return the link which you have to match with $actual_link
$link = get_permalink(get_the_ID());
something like this.

Ok, found the answer.
I changed my query to
$args = array (
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$pages = get_posts($args);
$forms = array();
if(is_array($pages)){
foreach ($pages as $page) {
$forms[$page->ID] = $page->post_title;
}
}
So that I can get page ID, then use
$link_out = get_permalink($link);
$current = ($actual_link == $link_out) ? 'current_page' : '';
And it works! :D

Related

Get a list of child pages via a function in wordpress

I am trying to get a list of child pages to display on the parent page via wordpress. The best way I can describe what I am trying to do is if you are on the about page (/about) but you want to go to the meet the team page (/about/meet-the-team) when you are on the about page it should then display a link to the meet the team page.
I have this code at the moment
function getPagesHierarchy() {
$pagesQuery = new WP_Query(array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => 0,
'order' => 'ASC',
'post_status' => 'publish'
)) ;
if ($pagesQuery->have_posts()) {
while($pagesQuery->have_posts()) {
$pagesQuery->the_post() ;
the_title() ;
echo '<br>' ;
}
}
}
What this is doing is it is getting a structure of the parent pages like this image shows below
This is currently getting all the parent pages which is fine however I also need it to display that parent pages children under it. so for example it would be something like this;
Any help with this would be much appreciated if I can give anything else to assist in what I am trying to achieve please do let me know.
You can use wp_list_pages() and pass in the parent page ID with child_of to get its children. You can refer to the WP docs for more info.
So that'd look something like
wp_list_pages(array(
'child_of' => $parentId
));
Probably best to use get_pages()
function getPagesHierarchy() {
$pages = get_pages(array('parent' => 0));
foreach ( $pages as $page ) {
echo $page->post_title;
echo '<br>' ;
$child_pages = get_pages(array( 'child_of' => $page->ID));
foreach ( $child_pages as $child_page ) {
echo $child_page->post_title;
echo '<br>' ;
}
}
}

Query all pages including subpages of child pages in Wordpress

I have following structure
Services
-Page_1
-Page_2
--Page_3
--Page_4
I use Wp-query to get pages. This is my args:
<?php $args = array(
'post_type' => 'page',
'post_parent' => '45'
); ?>
45 is ID of Services page.
But I only get first level Page_1 and Page_2. How do I get all pages? I'm using Advanced Custom Fields plugin so using get_pages() is not a good option, is it?
As mentioned in your comments, the easiest solutions is to actually use get_pages(). You can still grab meta data of posts if you are able to get the the ID of the page (which you can with get_pages()) so you should still be able to ACF's custom fields.
Here's an example of get_pages():
$args = array(
'child_of' => 45,
'post_type' => 'page',
);
$pages = get_pages($args);
The main difference between get_pages() and WP_Query that we want to focus on here is the 'child-of'=>45 paramater vs the 'post_parent'=>45.
The child-of argument will grab ALL children throughout the hierarchy, i.e. children and children's children etc.
In contrast, the post-parent argument of WP_Query will only grab direct children of the page.
Using in conjunction with ACF
If you need to grab custom fields from ACF, you will still be able to use get_post_meta().
If your custom field includes an array of values, you will need to unserialize it first and then loop through the values like this:
$meta = get_post_meta( $post->ID, 'acf_meta_key', true ); //grab the post meta using WordPress core function
$array = unserialize( $meta ); //unserialize the field into an array
foreach ( $array as $value ) { //loop through the array
echo $value .'<br>';
}

Redirect child term to first page under it in Wordpress

Is it possible to do the following in a functions.php file?
I have the following URL structure:
hxxp://domain.com/custom-post-type-slug/parent-term-slug/child-term-slug/page-slug/
Directly under the parent term there are no pages, only child terms.
Is there a way to redirect the 'root' of the child term to the first post belonging to that same child term?
hxxp://domain.com/custom-post-type-slug/parent-term-slug/child-term-slug/
needs to redirect to:
hxxp://domain.com/custom-post-type-slug/parent-term-slug/child-term-slug/page-slug/
Ok my friend what you want is possible... quite strange request but possible.
add_action('wp', 'get_first_child');
function get_first_child() {
global $wp_query;
if($wp_query->queried_object_id){
$args = array(
'post_parent' => $wp_query->queried_object_id,
'numberposts' => -1,
'order'=> 'ASC',
'post_status' => 'published'
);
$post = get_children($args);
//here I test if there is more than one child.. if yes I stop here if you want it to keep going just remove this
$post = (count($post) > 1) ? null : reset($post);
if($post->guid){
wp_redirect( $post->guid, 301 );
exit;
}
}
}
So what I do here?
All I do is get all children related to the main post that you are and reset it to the first on the line if there are more than 1 child.
Then I just get his guid and redirect it using 301.
Tadã... magic was done!
Hope that is what you wanted :)

Wordpress - Imported pages appear, but lost data

I exported just the pages from one WP install to another and it was successful. When I go to the pages list, it displays them all and shows parent/child relationships, but doesn't seem to be recognized anymore when using some custom code in the template. It works on the original site just fine and I compared WP versions; they're the same.
Here is the code I'm using:
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'orderby' => 'date', 'order' => 'ASC'));
// Get the page as an Object
$products = get_page_by_title('Products');
// Filter through all pages and find Products' children
$products_children = get_page_children($products->ID, $all_wp_pages);
// echo what we get back from WP to the browser
echo '<ul id="product-nav">';
foreach($products_children as $child){
echo '<li><a href="'.get_page_link($child->ID).'" title="'.$child->post_title.'">';
echo get_the_post_thumbnail($child->ID).'<span>'.$child->post_title.'</span>';
echo '</a></li>';
}
echo '</ul>';
When I performed a var_dump() on $all_wp_pages, they aren't being displayed there either. It's as if they don't exist, but show up on the back end as expected. Finally, I tried changing one of the child page's parent, then back again in hopes that would reset it, but had the same result.
Any idea why this would be happening?

How to fix this WordPress function so that it doesn't return a 404 page?

I have the following function that I've added to my functions.php file in WordPress. The idea is that it gathers all of the titles of 'fsmodel' posts (a custom post type that I've created). It then returns these as an array, which I then use to populate a select tag in the custom meta fields for a second custom post type.
Basically, 'fsmodel' will have posts with a boat model, and the 'fsboat' post type will have a drop-down with the names of each of the models to select from.
Now, this appears to works fine in the Dashboard - the drop-down is populated as expected. When I save, however, the post doesn't show up in the Edit list. Also on the website, all pages output as the 404 error page when this function is active.
I'm certain that the problem lies within the following code - does anyone have any idea what I might have done wrong?
function fs_model_array() {
$models_array = array();
$loop = new WP_Query(array(
'post_type' => 'fsmodel',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish'
));
while ( $loop->have_posts() ) : $loop->the_post();
$models_array[] = get_the_title();
endwhile;
return $models_array;
};
OK, I've come up with a solution (I hope - it's holding up for now).
Instead of creating a loop, I've just used the $wpdb->get_results to search the database for the column with a WHERE filter for the custom post type.
Then run an array builder:
$models_array = array();
$model_db = $wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE post_type='fsmodel' AND post_status = 'publish'");
foreach ($model_db as $model_db) {
$models_array[] = $model_db->post_title;
}
Thanks again for your time, hsatterwhite! :-)
I think you might find that adding wp_reset_query() to the end of your function will solve your problems :)
I like your solution, but I'd be inclined to say that you need to call the global variable of $post whenever you use the loop like this in a function, as it assigns it to that variable.
function fs_model_array(){
global $post;
$models_array = array();
$loop = new WP_Query(array(
...

Categories