What's the best way to approach this problem:
I have a list of navigation links in the sidebar, which must all have different, specific colours when hovered over, they are all the same colour the rest of the time. They are also nested (manually, is there an easy way to do hierarchical pages too?). The links all point to Wordpress pages. I want to be able to set the link to the current page to be permanently colourful (not just on hover).
Other than hard-coding this checking page-ids, how could I tackle this problem?
Found this helpful when I was doing it. From the Wordpress Codex.
if ( is_page('Page One') ) { $current
= 'one'; } elseif ( is_page('Page Two') ) { $current = 'two'; } elseif (
is_page('Page Three') ) { $current =
'three'; } elseif ( is_page('Page
Four') ) { $current = 'four'; }
Wordpress already has page identification built in. The is_page function accepts the page name as the variable. Just toss that in your id or class and you're golden.
Related
I want to get the permalinks of all published pages and export it to an excel file.
What is the best solution?
Should I go to wp-admin panel and copy-paste the permalinks from the on Page editor?
Can I get the export data using a mysql query?
It is possible but very difficult to do via a SQL query, because WordPress allows you to change your Permalink structure - so the SQL query would need to regard all permalink options to build the correct permalink when the query is executed.
And I guess you are looking for a way that does not require you to copy paste links from the admin page ;-)
By far the best and easiest thing to do is write a small script that runs the export for you within WordPress and uses the functions get_pages and get_permalink:
// Get a list of all published pages from WordPress.
$pages = get_pages( 'post_status=publish' );
// Loop through all pages and fetch the permalink for each page.
foreach ( $pages as $page ) { //fixed this too
$permalink = get_permalink( $page->ID );
// Do something with the permalink now...
}
Note: This code will only run inside WordPress, i.e. as as Plugin or inside a Theme. Also how to do the excel export is beyond the scope of my answer...
Personally I'd create a WordPress plugin (there are many guides out there on how this works, like this one).
In the plugin you could simply check for a URL param, and if this param is present then export the data. Additionally I would check if the user that requests the export has admin permissions before exporting the data.
A bit like this:
/**
* The WordPress plugin header...
*/
$allowed = false;
$requested = false;
// Check if the current user has admin capabilies.
if ( current_user_can( 'manage_options' ) ) { $allowed = true; }
// Check if the user requested the export.
// i.e. by calling URL http://yoursite.com?export_permalinks=1
if ( 1 == $_GET['export_permalinks'] ) { $requested = true; }
if ( $requested && $allowed ) {
// 1. Get a list of all published pages from WordPress.
$pages = get_pages( 'post_status=publish' );
// 2. Build the export data.
$export = array();
foreach ( $pages as $page ) {
$permalink = get_permalink( $page->ID );
$export[] = array( $page->ID, $permalink );
}
// 3. Export the data (I just var_dump it as example).
var_dump( $export );
// We're done, so don't execute anything else.
exit;
}
Please note that this code should only explain my suggested workflow. It does not use best practices and I don't recommend to use it like this on a live site
I am building a website in WordPress and have a set of photos that display along the top of the page (in the background) and have them to where the set of photos can change depending on where the visitor is on the website. For example, if they are looking at the FAQ page, then photos pertaining to asking questions are shown. If they are looking at the news page, then photos pertaining to news are shown. The code I have works great, however, I'd like to now show photos based on the parent of the page they are on; for instance, if they are looking at a child page under a parent.
Here's my code as it stands now:
<?php if ( is_front_page() ) { include'media/home.php'; }
elseif ( is_404() ) { include'media/404s.php'; }
elseif ( is_page('news') ) { include'media/home.php'; }
elseif ( is_page('faqs') ) { include'media/faqs.php'; }
elseif ( is_category('africa-2') ) { include'media/faqs.php'; }
else { include'media/general.php'; }
?>
How can I tell WordPress, I want it to show a certain php file if it is a parent page and all of it's children?
If you're in the loop you can just use
if( is_page('faqs') && $post->post_parent ){
//include template here
}
If you're outside the loop you'll need to include global $post before the conditional.
global $post
You can reference this link http://codex.wordpress.org/Conditional_Tags#Testing_for_sub-Pages
Is it possible to check if a page is a parent or if it's a child page?
I have my pages set up like this:
-- Parent
---- Child page 1
---- Child page 2
etc.
I want to show a certain menu if it's a parent page and a different menu if it's on the child page.
I know I can do something like below but I want to make it a bit more dynamic without including specific page ID's.
<?php
if ($post->post_parent == '100') { // if current page is child of page with page ID 100
// show image X
}
?>
You can test if the post is a subpage like this:
*(from http://codex.wordpress.org/Conditional_Tags)*
<?php
global $post; // if outside the loop
if ( is_page() && $post->post_parent ) {
// This is a subpage
} else {
// This is not a subpage
}
?>
Put this function in the functions.php file of your theme.
function is_page_child($pid) {// $pid = The ID of the page we're looking for pages underneath
global $post; // load details about this page
$anc = get_post_ancestors( $post->ID );
foreach($anc as $ancestor) {
if(is_page() && $ancestor == $pid) {
return true;
}
}
if(is_page()&&(is_page($pid)))
return true; // we're at the page or at a sub page
else
return false; // we're elsewhere
};
Then you can use it:
<?php
if(is_page_child(100)) {
// show image X
}
?>
I know this is an old question but I was searching for this same question and couldn't find a clear and simple answer until I came up with this one. My answer doesn't answer his explanation but it answers the main question which is what I was looking for.
This checks whether a page is a child or a parent and allows you to show, for example a sidebar menu, only on pages that are either a child or a parent and not on pages that do not have a parent nor children.
<?php
global $post;
$children = get_pages( array( 'child_of' => $post->ID ) );
if ( is_page() && ($post->post_parent || count( $children ) > 0 )) :
?>
For Wordpress, you can simply check:
<?php
if (wp_get_post_parent_id(get_the_ID())) {
echo "I am a child page";
}
?>
You can use the get_pages() function. it takes an associative array as an argument. you can give that array 'child_of' => get_the_ID() to get the children of the current page, and if it hasn't any children the whole get_pages() function will return false, otherwise it will return a value that evaluates to true, which can be assigned to a variable to use as a conditional in an if statement.
$iAmParent = get_pages(array('child_of' => get_the_ID()));
I simply want a link to my blog archives. They are just normal posts, but I cannot seem to find the right url for them to add to my menu.
right url for them to add to my menu.
http://yourwebsite.com/?post_type=post
You don't have to necessarily use a category to every post.
Actually the file to list all posts of any category and date is the index.php. You just write 'the loop' as told on codex.
So if you changed your index.php to make it as a fancy page and not the post list only, now you're trying to create another page to do that for you.
So, if you follow me, you're doing this the wrong way. What you should do is to create a separate page and assign it as the home page of your blog. It would then free the index.php file for you to use it as the blog list, as it is by default.
Assuming that you did it the correct way (as mentioned by Guilherme), you should have a page designated as the blog list page.
The URL for the blog list if using the page 'My Blog' to display posts and pretty links for the url should be something like http://mywebsite.com/my-blog.
<?php if ( is_front_page() && is_home() ) {
// Default homepage
echo "Default homepage";
} elseif ( is_front_page()){
echo "Static homepage";
// Static homepage
} elseif ( is_home()){
echo "Blog page";
// Blog page
} elseif ( is_page( 'cart' ) || is_cart()){
echo "cart";
// Blog page
} elseif (is_single()){
echo "is_single";
// Blog page
} elseif (is_product_category()){
echo "is_product_category";
}
else {
echo "Everything else";
// Everything else
} ?>
nearly finished my wp plugin for an estate agent,
I spidered the site for 404's etc, i noticed that my property details pages were spider'd in which all 45 page titles were : ( details | sitename ) (page titles are shown dynamicly from an ID being passed via querystring)
now I've got my nice urls fixed, urls look like this...
wpsite.com/details/20043/property+for+sale+in+this+area
In Which...
propid = 20043
propname = property+for+sale+in+this+area
both of these are querystring values which are used to re-write the urls.
'query_vars' => array('propid', 'propname'),
'rules' =>
array( '(.+?)/([^/]+)/([^/]+)/?$' => 'index.php?pagename=$matches[1]&propid=$matches[2]&propname=$matches[3]' )
);
Now when the property details page is loaded im trying to hook into the wordpress filter
wp_title but this isnt working the way i expected..
this is the code im using to generate the titles
function wp_myplugin_property_title()
{
$wp_acquaint_id = get_option("wp_system_id");
$propid = get_query_var('propid');
if(isset($propid)){
$seotitle = wp_myplugin_seo_title($propid);
}else{
$seotitle = "TEST Title";
}
return $seotitle;
}
if( is_page('details') ){
add_filter('wp_title', wp_myplugin_property_title, 100);
}
the function used within that function: wp_myplugin_seo_title($propid) generates the actual title i want to use...
function wp_myplugin_seo_title($propid)
{
$wp_acquaint_id = get_option("wp_acquaint_id");
$xml = wp_myplugin_get_property($propid);
foreach($xml->PropertiesDataSet->Properties as $node) {
include('xml_loop.php');
if($bedrooms==0){ }else{ $seo_title.= $bedrooms." bedroom "; }
$seo_title.= wp_myplugin_get_property_type($type_id)." "; //ie:flat
$seo_title.= str_replace("(","",$street);
$seo_title.= " ".$town." | ".get_bloginfo('name');
}
return $seo_title;
}
Im finding that with the if(is_page()) in place around the filter the page title dosnt change and if i remove the is_page, the prop details page title works fine but!!!
while on the property listing page the page title is looping through all the properties on that page and producing a page title around 1000 char long..!
I have been hunting around for a better way to deal with this but any help would be great..
Cheers
Marty
ps: currently running wordpress seo by Yoast!
thats why ive set the priority as 100 in the add_filter just to see if it would overwrite the title..
Using is_page in functions.php doesn't work, as it runs before wp knows what page it's going to render, or even if it's a page to begin with. Stick the is_page() inside the function, and it should work. Like:
function wp_myplugin_property_title()
{
if( is_page('details') ){
$wp_acquaint_id = get_option("wp_system_id");
$propid = get_query_var('propid');
if(isset($propid)){
$seotitle = wp_myplugin_seo_title($propid);
}else{
$seotitle = "TEST Title";
}
return $seotitle;
}
}
add_filter('wp_title', wp_myplugin_property_title, 100);